写一个代码,要求:对文本文件内容进行jieba分词和词性标注,并将词性标注结果写入文本文件中
时间: 2024-06-05 19:06:20 浏览: 101
【jieba分词】必练代码
import jieba.posseg as pseg
# 打开文本文件
with open("input.txt", "r", encoding="utf-8") as f:
text = f.read()
# 对文本进行分词和词性标注
words = pseg.cut(text)
# 将词性标注结果写入输出文件
with open("output.txt", "w", encoding="utf-8") as f:
for word, flag in words:
f.write("%s\t%s\n" % (word, flag))
阅读全文