2017-02-20 2 views
1

私はNews Paperの見出しの感情分析のためにNaive Bayesアルゴリズムを実装しようとしています。私は、この目的のためにTextBlobを使用していると私はそれが困難な「A」などのストップワードを削除するには見つけることだ、「」、など以下の「中」のpythonでの私のコードの抜粋です:テキストの感情分析のためにtextblobでストップワードを削除する効率的な方法はどれですか?

from textblob.classifiers import NaiveBayesClassifier 
from textblob import TextBlob 

test = [ 
("11 bonded labourers saved from shoe firm", "pos"), 
("Scientists greet Abdul Kalam after the successful launch of Agni on May 22, 1989","pos"), 
("Heavy Winter Snow Storm Lashes Out In Northeast US", "neg"), 
("Apparent Strike On Gaza Tunnels Kills 2 Palestinians", "neg") 
     ] 

with open('input.json', 'r') as fp: 
cl = NaiveBayesClassifier(fp, format="json") 

print(cl.classify("Oil ends year with biggest gain since 2009")) # "pos" 
print(cl.classify("25 dead in Baghdad blasts")) # "neg" 

答えて

0

最初にjsonをロードしてから、置換えでタプル(テキスト、ラベル)のリストを作成することができます。

デモンストレーション:

[ 
    {"text": "I love this sandwich.", "label": "pos"}, 
    {"text": "This is an amazing place!", "label": "pos"}, 
    {"text": "I do not like this restaurant", "label": "neg"} 
] 

が次にあなたが使用することができます:

from textblob.classifiers import NaiveBayesClassifier 
import json 

train_list = [] 
with open('input.json', 'r') as fp: 
    json_data = json.load(fp) 
    for line in json_data: 
     text = line['text'] 
     text = text.replace(" is ", " ") # you can remove multiple stop words 
     label = line['label'] 
     train_list.append((text, label)) 
    cl = NaiveBayesClassifier(train_list) 

from pprint import pprint 
pprint(train_list) 

出力:

[(u'I love this sandwich.', u'pos'), 
(u'This an amazing place!', u'pos'), 
(u'I do not like this restaurant', u'neg')] 

input.jsonファイルはこのようなものであると仮定

関連する問題