with open('tf_idf.txt', 'w', encoding='utf-8') as f: for word, tf in tf_values.items(): idf = idf_values.get(word, 0) tf_idf = tf * idf f.write('{}\t{}\n'.format(word, tf_idf))将上述代码的结果改为以tf-idf的值降序输出
时间: 2023-12-01 10:03:26 浏览: 27
可以按照以下方式修改代码,使用sorted进行排序:
```
with open('tf_idf.txt', 'w', encoding='utf-8') as f:
for word, tf in tf_values.items():
idf = idf_values.get(word, 0)
tf_idf = tf * idf
f.write('{}\t{}\n'.format(word, tf_idf))
with open('tf_idf.txt', 'r', encoding='utf-8') as f:
lines = f.readlines()
sorted_lines = sorted(lines, key=lambda x: -float(x.split('\t')[1]))
with open('tf_idf_sorted.txt', 'w', encoding='utf-8') as f2:
f2.writelines(sorted_lines)
```
这个代码会将原本输出的文件名为 `tf_idf.txt` 的文件,读取出来按照 tf-idf 的值进行降序排序,然后将排序后的结果输出到文件名为 `tf_idf_sorted.txt` 的新文件中。
相关问题
with open('tf_idf.txt', 'w', encoding='utf-8') as f: for word, tf in tf_values.items(): idf = idf_values.get(word, 0) tf_idf = tf * idf f.write('{}\t{}\n'.format(word, tf_idf))将上述代码的结果改为降序输出
可以将代码中的`tf_idf`值存入一个列表中,然后使用`sorted`函数对列表进行降序排序,最后再将排序后的结果写入文件中。修改后的代码如下:
```
with open('tf_idf.txt', 'w', encoding='utf-8') as f:
tf_idf_values = []
for word, tf in tf_values.items():
idf = idf_values.get(word, 0)
tf_idf = tf * idf
tf_idf_values.append((word, tf_idf))
tf_idf_values = sorted(tf_idf_values, key=lambda x: x[1], reverse=True)
for word, tf_idf in tf_idf_values:
f.write('{}\t{}\n'.format(word, tf_idf))
```
这样就可以将结果按照tf-idf值降序输出到文件中。
def Stop_words(): stopword = [] data = [] f = open('C:/Users/Administrator/Desktop/data/stopword.txt',encoding='utf8') for line in f.readlines(): data.append(line) for i in data: output = str(i).replace('\n','')#replace用法和sub函数很接近 stopword.append(output) return stopword # 采用jieba进行词性标注,对当前文档过滤词性和停用词 def Filter_word(text): filter_word = [] stopword = Stop_words() text = jieba.posseg.cut(text) for word, flag in text: if flag.startswith('n') is False:#用于检测字符串是否以指定的子字符串开始 continue if not word in stopword and len(word) > 1: filter_word.append(word) return filter_word # 对文档集过滤词性和停用词 def Filter_words(data_path =r'C:/Users/Administrator/Desktop/data//corpus.txt'): document = [] for line in open(data_path, 'r',encoding= 'utf8') : segment = jieba.posseg.cut(line.strip()) filter_words = [] stopword = Stop_words() for word, flag in segment: if flag.startswith('n') is False: continue if not word in stopword and len(word) > 1: filter_words.append(word) document.append(filter_words) return document def tf_idf(): tf_dict = {} idf_dict = {} filter_word = Filter_word(text) for word in filter_word: if word not in tf_dict: tf_dict[word] = 1 else: tf_dict[word] += 1 for word in tf_dict: tf_dict[word] = tf_dict[word] / len(text) document = Filter_words() doc_total = len(document) for doc in document: for word in set(doc): if word not in idf_dict: idf_dict[word] = 1 else: idf_dict[word] += 1 for word in idf_dict: idf_dict[word] = math.log(doc_total / (idf_dict[word] + 1)) tf_idf_dict = {} for word in filter_word: if word not in idf_dict: idf_dict[word] = 0 tf_idf_dict[word] = tf_dict[word] * idf_dict[word] return tf_idf_dict tf_idf_dict = tf_idf() keyword = 6 print('TF-IDF模型结果:') for key, value in sorted(tf_idf_dict.items(), key=operator.itemgetter(1),reverse=True)[:keyword]: print(key, end=' ') print('\n')
这段代码实现了一个 TF-IDF 模型,用于计算文本中关键词的权重。其中,Stop_words 函数用于读取停用词表,Filter_word 函数用于对单个文档进行过滤,Filter_words 函数用于对整个文档集进行过滤。tf_idf 函数用于计算 TF-IDF 值,最后输出了权重最高的前 keyword 个关键词。