Как я могу решить эту проблему?
from nltk.sentiment.util import demo_sent_subjectivity
sentence='I like her shoes'
demo_sent_subjectivity(sentence)
NameError: имя 'save_file' не определено
demo_sent_subjectivity (text) - это метод внутри nltk.sentiment.utils, который затем вызывает другой метод demo_subjectivity (trainer, save_analyzer = False, n_instances = None, output = None)
Ниже приведены следующие методы:
def demo_sent_subjectivity(text):
"""
Classify a single sentence as subjective or objective using a stored
SentimentAnalyzer.
:param text: a sentence whose subjectivity has to be classified.
"""
from nltk.classify import NaiveBayesClassifier
from nltk.tokenize import regexp
word_tokenizer = regexp.WhitespaceTokenizer()
try:
sentim_analyzer = load("sa_subjectivity.pickle")
except LookupError:
print("Cannot find the sentiment analyzer you want to load.")
print("Training a new one using NaiveBayesClassifier.")
sentim_analyzer = demo_subjectivity(NaiveBayesClassifier.train,
True)
# Tokenize and convert to lower case
tokenized_text = [word.lower() for word in
word_tokenizer.tokenize(text)]
print(sentim_analyzer.classify(tokenized_text))
def demo_subjectivity(trainer, save_analyzer=False, n_instances=None, output=None):
from nltk.sentiment import SentimentAnalyzer
from nltk.corpus import subjectivity
if n_instances is not None:
n_instances = int(n_instances / 2)
subj_docs = [
(sent, "subj") for sent in subjectivity.sents(categories="subj")
[:n_instances]
]
obj_docs = [
(sent, "obj") for sent in subjectivity.sents(categories="obj")
[:n_instances]
]
train_subj_docs, test_subj_docs = split_train_test(subj_docs)
train_obj_docs, test_obj_docs = split_train_test(obj_docs)
training_docs = train_subj_docs + train_obj_docs
testing_docs = test_subj_docs + test_obj_docs
sentim_analyzer = SentimentAnalyzer()
all_words_neg = sentim_analyzer.all_words(
[mark_negation(doc) for doc in training_docs]
)
unigram_feats = sentim_analyzer.unigram_word_feats(all_words_neg,
min_freq=4)
sentim_analyzer.add_feat_extractor(extract_unigram_feats,
unigrams=unigram_feats)
training_set = sentim_analyzer.apply_features(training_docs)
test_set = sentim_analyzer.apply_features(testing_docs)
classifier = sentim_analyzer.train(trainer, training_set)
try:
classifier.show_most_informative_features()
except AttributeError:
print(
"Your classifier does not provide a
show_most_informative_features() method."
)
results = sentim_analyzer.evaluate(test_set)
if save_analyzer == True:
save_file(sentim_analyzer, "sa_subjectivity.pickle")
if output:
extr = [f.__name__ for f in sentim_analyzer.feat_extractors]
output_markdown(
output,
Dataset="subjectivity",
Classifier=type(classifier).__name__,
Tokenizer="WhitespaceTokenizer",
Feats=extr,
Instances=n_instances,
Results=results,
)
return sentim_analyzer
save_file метод вызывается внутри demo_subjectivity метода, но я не понял, где находится исходный код для него. Я заметил, что метод save_file существует в SentimentAnalyzer классе, но почему здесь он вызывается так же, как save_file , а не sentim_analyzer.save_file ?