NLTK是python环境下NLP工具包,包含了丰富的文本处理和文本挖掘API。
自然语言处理是计算机科学领域与人工智能领域中的一个重要方向。自然语言工具箱(NLTK,Natural Language Toolkit)
是一个基于Python语言的类库,它也是当前最为流行的自然语言编程与开发工具。在进行自然语言处理研究和应用时,
恰当利用NLTK中提供的函数可以大幅度地提高效率。本文就将通过一些实例来向读者介绍NLTK的使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
from nltk.classify import NaiveBayesClassifier def word_feats(words): return dict([(word, True) for word in words]) # 数据准备 positive_vocab = ['awesome', 'outstanding', 'fantastic', 'terrific', 'good', 'nice', 'great', ':)', 'incredible', 'like'] negative_vocab = ['bad', 'terrible', 'useless', 'hate', ':(', 'motherfucker'] neutral_vocab = ['movie', 'the', 'sound', 'was', 'is', 'actors', 'did', 'know', 'words', 'not'] # 特征提取 positive_features = [(word_feats(pos), 'pos') for pos in positive_vocab] negative_features = [(word_feats(neg), 'neg') for neg in negative_vocab] neutral_features = [(word_feats(neu), 'neu') for neu in neutral_vocab] train_set = negative_features + positive_features + neutral_features # 训练 classifier = NaiveBayesClassifier.train(train_set) # 测试 neg = 0 pos = 0 sentence = "I like the wonderful movie, it is awesome!!!" sentence = sentence.lower() words = sentence.split(' ') for word in words: classResult = classifier.classify(word_feats(word)) if classResult == 'neg': neg = neg + 1 if classResult == 'pos': pos = pos + 1 print('积极: ' + str(float(pos) / len(words))) print('消极: ' + str(float(neg) / len(words))) |