I am a new user to Python and am trying to loop the following:
text = open('filename.txt', 'rU').read()
splitter = Splitter()
postagger = POSTagger()
splitted_sentences = splitter.split(text)
pos_tagged_sentences = postagger.pos_tag(splitted_sentences)
dicttagger = DictionaryTagger([ 'dicts/positive.yml', 'dicts/negative.yml'])
dict_tagged_sentences = dicttagger.tag(pos_tagged_sentences)
scoreposneg = sentiment_score(dict_tagged_sentences)
dicttagger = DictionaryTagger([ 'dicts/positive.yml', 'dicts/negative.yml', 'dicts/inc.yml', 'dicts/dec.yml', 'dicts/inv.yml'])
dict_tagged_sentences = dicttagger.tag(pos_tagged_sentences)
scoretotal = sentiment_total(dict_tagged_sentences)
print scoretotal
Prior to this, I have set up class for Splitter, POSTagger, DictionaryTagger, sentiment_total and also created 5 different dictionaries. This works when I run it on Python for 1 filen(I got a number from print scoretotal). However, when I tried to create a loop and print all the outputs (I have 650 files in my directory), it didn't work (nothing was printed) and the scoretext.txt file had 0.0 in them.
path = '/mydirectory'
files = glob.glob(path)
for file in files:
text = open(file, 'rU').read()
splitter = Splitter()
postagger = POSTagger()
splitted_sentences = splitter.split(text)
pos_tagged_sentences = postagger.pos_tag(splitted_sentences)
dicttagger = DictionaryTagger([ 'dicts/positive.yml', 'dicts/negative.yml'])
dict_tagged_sentences = dicttagger.tag(pos_tagged_sentences)
scoreposneg = sentiment_score(dict_tagged_sentences)
dicttagger = DictionaryTagger([ 'dicts/positive.yml', 'dicts/negative.yml', 'dicts/inc.yml', 'dicts/dec.yml', 'dicts/inv.yml'])
dict_tagged_sentences = dicttagger.tag(pos_tagged_sentences)
scoretotal = sentiment_total(dict_tagged_sentences)
print scoretotal
scoretotal = np.zeros((1,650))
scoretotal_no = 0
scoretotal_no = scoretotal_no + 1
np.savetxt("scoretext.txt", scoretotal, delimiter=" ", fmt="%s")
Would really appreciate if someone can provide some insights on this. Thank you!