尝试编写简单的python脚本,该脚本将使用NLTK查找和替换txt文件中的同义词。
以下代码给出错误:
Traceback (most recent call last):
File "C:\Users\Nedim\Documents\sinon2.py", line 21, in <module>
change(word)
File "C:\Users\Nedim\Documents\sinon2.py", line 4, in change
synonym = wn.synset(word + ".n.01").lemma_names
TypeError: can only concatenate list (not "str") to list
代码如下:
from nltk.corpus import wordnet as wn
def change(word):
synonym = wn.synset(word + ".n.01").lemma_names
if word in synonym:
filename = open("C:/Users/tester/Desktop/test.txt").read()
writeSynonym = filename.replace(str(word), str(synonym[0]))
f = open("C:/Users/tester/Desktop/test.txt", 'w')
f.write(writeSynonym)
f.close()
f = open("C:/Users/tester/Desktop/test.txt")
lines = f.readlines()
for i in range(len(lines)):
word = lines[i].split()
change(word)
这不是非常有效,而且这不会取代一个同义词。因为每个词可能有多个同义词。你可以从中选择
两件事。首先,可以将文件读取部分更改为:
其次,
.split()
返回一个字符串列表,而您的change
函数似乎一次只对一个单词进行操作。这就是导致异常的原因。你的word
实际上是一个列表。如果你想处理那行的每一个字,让它看起来像:
相关问题 更多 >
编程相关推荐