当前位置:   article > 正文

利用tf-idf提取关键词_analyse.tfidf

analyse.tfidf

##读取.txt并保存为.txt

from jieba import analyse

f = open("result.txt", "w", encoding="utf-8")
for line in open("text.txt",encoding="utf-8"):
    tfidf = analyse.extract_tags
    text = line
    keywords = tfidf(text, topK=4, withWeight=False, allowPOS=())
    content_text = ' '.join(keywords)
    print(content_text)
    f.write(text)
    f.write(content_text)
    f.write("\n")
f.close()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

##读取.xlsx并保存为.xlsx

from jieba import analyse
from openpyxl import load_workbook   #导入模块
wb = load_workbook(filename="data.xlsx")    # 打开文件, 默认可读写, 若有需要可以指定 write_only和read_only 为True
sheet = wb["Sheet1"]   # 找到工作表
for i in range(3,sheet.max_row+1):
    text = sheet.cell(row=i, column=2).value
    tfidf = analyse.extract_tags
    keywords = tfidf(text, topK=4, withWeight=False, allowPOS=())
    content_text = " ".join(keywords)
    sheet.cell(row=i, column=3, value=content_text)
wb.save('test1.xlsx')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/614151
推荐阅读
相关标签
  

闽ICP备14008679号