首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python .words问题?

Python .words问题?
EN

Stack Overflow用户
提问于 2017-12-01 10:47:12
回答 0查看 135关注 0票数 0

我正在尝试创建一个程序,告诉我paulryan.txt文件的每一行的正负程度。我使用的是opinion_lexicon,文件是'_io.TextIOWrapper‘

有没有什么我可以用来代替.words的?

其他不太重要的问题:有没有办法让我的整个paulryan.txt文件变得小写,同时保持它的行标记化?如果我不把整个东西变成小写,它就不会给我一个准确的正或负分数,因为在opinion_lexicon中只有小写的单词。

代码语言:javascript
复制
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'
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47585906

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档