2017-12-01 27 views
0

그래, 그 paulryan.txt 파일의 각 라인이 얼마나 긍정적인지 음수인지를 알려주는 프로그램을 만들려고합니다. opinion_lexicon을 사용하고 있는데 파일이 '_io.TextIOWrapper'입니다.Python .words 문제가 있습니까?

.words 대신 사용할 수있는 것이 있습니까?

기타 덜 중요한 문제 : 줄 단위로 토큰을 유지하면서 내 전체 paulryan.txt 파일을 소문자로 만드는 방법은 무엇입니까? opinion_lexicon에 소문자 단어 만 있기 때문에 전체를 소문자로 만들지 않으면 정확한 양수 또는 음수 점수를 얻지 못할 것이라고 생각합니다.

import nltk 
from nltk.corpus import opinion_lexicon 
from nltk.tokenize.simple import (LineTokenizer, line_tokenize) 

poswords = set(opinion_lexicon.words("positive-words.txt")) 
negwords = set(opinion_lexicon.words("negative-words.txt")) 


f=open("paulryan.txt", "rU") 
raw = f.read() 
token= nltk.line_tokenize(raw) 

print(token) 

def finddemons(): 
    for x in token: 
     y = token.words() 
     percpos = len([w for w in token if w in poswords ])/len(y) 
     percneg = len([w for w in token if w in negwords ])/len(y) 
     print(x, "pos:", round(percpos, 3), "neg:", round(percneg, 3)) 

finddemons() 

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "<stdin>", line 3, in finddemons 
AttributeError: 'list' object has no attribute 'words' 
+0

당신이 NLTK와 'spacy' 인터페이스를 혼동하는 것 같습니다.)'nltk import word_tokenize; y = word_tokenize (토큰) ' – alvas

+0

흠. 글쎄, 나는 그것을 시도했지만 모든 것을 하나의 큰 것으로 합쳤다. :/줄 단위로 구분하기 위해 필요하다. 줄 단위로 짹짹으로 가득 찬 텍스트 문서 – rlavalla

답변

0

파일을 한 줄씩 읽으시기 바랍니다. 그런 다음, 사용 word_ 토큰 화 :

for line in f: 
    tokens = word_tokenize(line) 

당신은 사전에 검색을위한 텍스트를 소문자에 대한 권리입니다

for line in f: 
    tokens = word_tokenize(line.lower()) 

의견의 사전이기 때문에 당신도, 워드 넷을 사용하여 토큰을 lemmatize을 시도 할 수 있습니다 그 어휘가 풍부하지 않습니다. 특히 단어를 다른 형태로 사용하는 트윗을 사용하는 경우 특히 그렇습니다.