分词工具的选择:
现在对于中文分词,分词工具有很多种,比如说:jieba分词、thulac、SnowNLP等。在这篇文档中,笔者使用的jieba分词,并且基于python3环境,选择jieba分词的理由是其比较简单易学,容易上手,并且分词效果还很不错。
分词前的准备:
- 待分词的中文文档
- 存放分词之后的结果文档
- 中文停用词文档(用于去停用词,在网上可以找到很多)
分词之后的结果呈现:

去停用词和分词前的中文文档

去停用词和分词之后的结果文档
分词和去停用词代码实现:
import jieba
# 创建停用词列表
def stopwordslist():
stopwords = [line.strip() for line in open('chinsesstoptxt.txt',encoding='UTF-8').readlines()]
return stopwords
# 对句子进行中文分词
def seg_depart(sentence):
# 对文档中的每一行进行中文分词
print("正在分词")
sentence_depart = jieba.cut(sentence.strip())
# 创建一个停用词列表
stopwords = stopwordslist()
# 输出结果为outstr
outstr = ''
# 去停用词
for word in sentence_depart:
if word not in stopwords:
if word != '\t':
outstr += word
outstr += " "
return outstr
# 给出文档路径
filename = "Init.txt"
outfilename = "out.txt"
inputs = open(filename, 'r', encoding='UTF-8')
outputs = open(outfilename, 'w', encoding='UTF-8')
# 将输出结果写入ou.txt中
for line in inputs:
line_seg = seg_depart(line)
outputs.write(line_seg + '\n')
print("-------------------正在分词和去停用词-----------")
outputs.close()
inputs.close()
print("删除停用词和分词成功!!!")
import jieba.analyse
a=jieba.analyse.extract_tags(sentence, topK = 20, withWeight = False, allowPOS = ())
# sentence:待提取的文本。
# topK:返回几个 TF/IDF 权重最大的关键词,默认值为20。
# withWeight:是否一并返回关键词权重值,默认值为False。
# allowPOS:仅包括指定词性的词,默认值为空,即不进行筛选。
如果还想把量词删去,我比较low的想法是靠正则表达式,带数字的或者表示数字的就去掉
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)