自定义词典和停用词的引入
自定义词典是我们在分词的时候避免把我们需要的词组分成小词而导入的,而停用词,则是我们在分词过程中,将对我们分词过程中的干扰词排除在外的词典。
import re
import jieba
import sqlite3
import pandas as pd
from zhon.hanzi import punctuation
jieba.load_userdict('data/userdict.txt')
def get_custom_stopwords(stop_words_file):
'''
#导入停用词库
'''
with open(stop_words_file) as f:
stopwords = f.read()
stopwords_list = stopwords.split('\n')
custom_stopwords_list = [i for i in stopwords_list]
return custom_stopwords_list
def pd_search(sql="select * from 'asks_answer'" ,DB_name='***.sqlite3'):
connection = sqlite3.connect(DB_name)
df = pd.read_sql_query(sql, connection)
return df
def chinese_word_cut(mytext):
"""
对文本进行切词,并过滤掉中文字符和停用词。
"""
stop_words_file = 'stopwords.txt'
stopwords = get_custom_stopwords(stop_words_file)
cutted_word = re.sub(r'[%s]+'%punctuation, '',
" ".join([i for i in jieba.cut_for_search(mytext) if i not in stopwords]))
return cutted_word
mytext = "语言模型是把查询和文档分别表示成语言模型,通过两个语言模型之间的KL距离来估计两者的相似度。"
test_word = chinese_word_cut(mytext)
test_word
>>>'语言 模型 查询 文档 成 语言 模型 两个 语言 模型 之间 KL 距离 估计 相似度'