Python。文字问题?

2024-10-03 19:29:46 发布

您现在位置:Python中文网/ 问答频道 /正文

好的,我正在创建一个程序,告诉我每一行的正负值保利安.txt文件是。我在用意见词典,文件是'_io.TEXTIO包装器'

有什么我可以用的来代替文字吗?你知道吗

另一个不太重要的问题是:有没有办法让我的整个保利安.txt文件小写,而保持它标记行?我想如果我不把整件事都写成小写的话,它不会给我一个准确的正面或负面的分数,因为字典里只有小写的单词。你知道吗

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'

Tags: 文件inimporttxttokenforlenline
1条回答
网友
1楼 · 发布于 2024-10-03 19:29:46

我建议你逐行阅读文件。然后,使用单词“tokenize”:

for line in f:
    tokens = word_tokenize(line)

关于在词典中搜索的小写文本,您是对的:

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

您甚至可以尝试使用wordnet来对标记进行柠檬化,因为意见词典的词汇并不丰富。尤其是如果你使用tweet,在tweet中,单词的形式常常不同。你知道吗

相关问题 更多 >