import jieba
# 停止词库路径
filepathj="C:/Users/Administrator/Desktop/junkwords.txt"
# 语料路径
filepathji="E:/2018_taidibei/code/why2.txt"
## 将停止词库弄成一个列表
def stopwordslist(filepathj):
stopwords = [line.strip() for line in open(filepathj, 'r').readlines()]
return stopwords
#将停止词库列表赋给stopwords,导入停止词库
stopwords = stopwordslist(filepathj)
# 对句子进行分词,返回一个使用停止词库并分完词的列表
def seg_sentence(sentence):
sentence_seged = jieba.cut(sentence.strip())
seg_words=[]
for word in sentence_seged:
if word not in stopwords:
if word != '\t':
seg_words.append(word)
return seg_words
# 对句子进行分词 ,存为字符串形式
def seg_sentence(sentence):
sentence_seged = jieba.cut(sentence.strip())
outstr = ''
for word in sentence_seged:
if word not in stopwords:
if word != '\t':
outstr += word
outstr += " "
return outstr
#示例
#sentence='高速公路上驾驶会扣多少分?'
#未使用停止词库分词:高速公路 上 驾驶 会扣 多少 分
#使用停止词库分词:['高速公路', '驾驶', '会扣', '分']
# 存为字符串 '高速公路 驾驶 会扣 分 '