今天使用jieba分词的td-idf进行关键词提取,如果使用jieba.analyse.set_idf_path('textbook_idf.txt')设置自己的idf文件时,会报如下错误:
word, freq = line.split(' ')
ValueError: too many values to unpack (expected 2)
发现是自己的IDF文件格式的问题,每一行的词语和IDF值之间应当是空格,而不应该是其他字符。
使用下面的代码将中间的\t替换成空格即可。为了保险起见,另存为一个文件。
fo = open("idf_result.txt", "r")
content = ""
while True:
line = fo.readline()
if not line:
break
temp = line.strip().split("\t")
content += temp[0]+" " + temp[1] +"\r\n"
wo = open("idf.txt","w")
wo.write(content)