samedi 29 novembre 2014

Lambda parameter function in python


Vote count:

0




I am supposed to analyze shakespeare text using NLTK ( sentiment analysis) using python 3.4. But I get this error message:



**Traceback (most recent call last):
File "C:\Users\HAMIMOUNE\AppData\Roaming\nltk_data\shakespeare.py", line 8, in <module>
[ line.split('\t') for line in open("AFINN-96.txt") ]))
TypeError: <lambda>() missing 1 required positional argument: 'v'**


here is my code:



from nltk.corpus import shakespeare
from collections import defaultdict
import json
hamlet = shakespeare.xml('hamlet.xml')
speeches = hamlet.findall('.//SPEECH')
prevSpeaker = speeches[0].find('SPEAKER').text
afinn = dict(map(lambda k,v: (k,int(v)),
[ line.split('\t') for line in open("AFINN-96.txt") ]))

dict = defaultdict(lambda: defaultdict(lambda: 0))
for speech in speeches:
speaker = speech.find('SPEAKER').text
lines = "\n".join(line.text for line in speech.findall('LINE') if line.text is not None)
sentiment = sum(map(lambda word: afinn.get(word, 0), lines.lower().split()))
dict[speaker][prevSpeaker] += sentiment
prevSpeaker = speaker
print(json.dumps(dict, sort_keys=True, indent=4))


asked 36 secs ago







Lambda parameter function in python

Aucun commentaire:

Enregistrer un commentaire