2017-02-20 1 views
1

Ich versuche, Naive Bayes-Algorithmus für die Sentiment-Analyse von News Paper Schlagzeilen zu implementieren. Ich verwende TextBlob für diesen Zweck und ich finde es schwierig, Stoppwörter wie ‚a‘, ‚der‘, ‚in‘ usw. Im Folgenden zu entfernen, ist der Ausschnitt aus meinem Code in Python:Welches ist der effiziente Weg, um Stoppwörter in Textblob für Sentiment-Analyse von Text zu entfernen?

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" 

Antwort

0

Sie können zuerst den JSON laden und dann eine Liste von Tupeln (Text, Label) mit dem Ersatz erstellen.

Demonstration:

Angenommen, die input.json Datei so etwas wie dieses:

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

Dann können Sie verwenden:

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) 

Ausgang:

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