Nói tóm lại:
from nltk import precision
Trong dài:
Đây là khéo léo. Vấn đề xảy ra vì NLTK đã được đóng gói như thế nào. Nếu chúng ta nhìn vào dir(nltk.metrics)
, không có gì bên trong nó, trừ alignment_error_rate
>>> import nltk
>>> dir(nltk.metrics)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'alignment_error_rate']
BTW, trong phiên bản cạnh chảy máu của NLTK là, alignment_error_rate
đã được chuyển đến nltk.translate.metrics
, xem https://github.com/nltk/nltk/blob/develop/nltk/translate/metrics.py#L10. Gói nltk.translate
hơi không ổn định vì nó vẫn chưa được phát triển.
Trở lại với gói số liệu, từ https://github.com/nltk/nltk/blob/develop/nltk/metrics/__init__.py
, chúng tôi thấy điều này:
from nltk.metrics.scores import (accuracy, precision, recall, f_measure,
log_likelihood, approxrand)
from nltk.metrics.confusionmatrix import ConfusionMatrix
from nltk.metrics.distance import (edit_distance, binary_distance,
jaccard_distance, masi_distance,
interval_distance, custom_distance,
presence, fractional_presence)
from nltk.metrics.paice import Paice
from nltk.metrics.segmentation import windowdiff, ghd, pk
from nltk.metrics.agreement import AnnotationTask
from nltk.metrics.association import (NgramAssocMeasures, BigramAssocMeasures,
TrigramAssocMeasures, ContingencyMeasures)
from nltk.metrics.spearman import (spearman_correlation, ranks_from_sequence,
ranks_from_scores)
về cơ bản, điều này có nghĩa rằng các chức năng từ các gói số liệu đã được mã hóa bằng tay và đẩy lên nltk.metrics.__init__.py
. Vì vậy, nếu hàng nhập dừng ở đây, dir(metrics)
, sẽ liệt kê tất cả các số liệu được nhập tại đây.
Nhưng vì ở tầng cao hơn, ít nltk.__init__.py
https://github.com/nltk/nltk/blob/develop/nltk/__init__.py#L131
, các gói được nhập khẩu sử dụng:
from nltk.metrics import *
Bây giờ tất cả các số liệu điểm số đã được nhập khẩu vào cấp cao nhất có nghĩa là bạn có thể làm:
>>> from nltk import precision
>>> from nltk import spearman_correlation
>>> from nltk import NgramAssocMeasures
Nhưng bạn vẫn có thể truy cập bất kỳ mô-đun cấp trung gian nào trong phạm vi nltk.metrics
không được nhập trong nltk.metrics.__init__.py
. Nhưng bạn phải sử dụng đúng không gian tên như cách các hàm được lưu trong thư mục tương ứng của chúng.Lưu ý rằng những sẽ không hiển thị trong dir(nltk.metrics)
nhưng cách hợp lệ để nhập khẩu một chức năng:.
>>> from nltk.metrics import spearman
>>> from nltk.metrics import paice
>>> from nltk.metrics import scores
<function precision at 0x7fb584a34938>
>>> scores.precision
>>> spearman.spearman_correlation
<function spearman_correlation at 0x7fb5842b3230>
>>> from nltk.metrics.scores import precision
>>> precision
<function precision at 0x7fb584a34938>
thử thêm và tập tin rỗng tên '__init py' __ cho' /usr/local/lib/python2.7/dist-packages/nltk/metrics' để python có thể nhận ra nó như là một module nhập từ nó. – Cyrbil
Bạn đang sử dụng hệ điều hành nào? Làm thế nào bạn cài đặt NLTK (pip hoặc cái gì khác)? Bạn đang sử dụng phiên bản NLTK và python nào? Bạn đã nhập nltk như thế nào? Bạn đã thử 'từ nltk.metrics.score nhập chính xác'? – alvas
Lưu ý lỗi đánh máy trong bình luận trước của tôi, tôi muốn nói, bạn đã thử 'từ nltk.metrics.scores import precision' chưa? – alvas