試圖編寫簡單的python腳本,它將使用NLTK在txt文件中查找和替換同義詞。使用WordNet和NLTK替換語料庫中的同義詞 - python
下面的代碼給我錯誤:
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)
這是行不通的。不知道什麼是錯的 – Tester 2011-03-01 10:25:57