NLTK软件包包含了电影评论和分类器,可以比较容易实现电影评论的分类:正面评价还是负面评价。本文的算法使用了分句、分词、去停用词、词性标注、词性还原,而不是直接将电影评论分词后直接使用。尽管效果类似,但是这样处理更贴近实际应用情况:
from nltk.corpus import movie_reviews
from nltk import tokenize
from nltk.corpus import stopwords
from nltk import pos_tag
import nltk.classify.util
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import wordnet
from nltk.stem import WordNetLemmatizer
# 获取单词的词性
def get_pos(tag):
if tag.startswith('J'):
return wordnet.ADJ
elif tag.startswith('V'):
return wordnet.VERB
elif tag.startswith('N'):
return wordnet.NOUN
elif tag.startswith('R'):
return wordnet.ADV