input_str = """
There are some people who think love is sex
And marriage
And six o'clock-kisses
And children,
And perhaps it is,
Miss Lester.
But do you know what I think?
I think love is a touch and yet not a touch
"""
input_str = input_str.lower()
print(input_str)
结果如下: & `4 d9 C9 k" e* Z [7 X 3 v- t, p. I1 V6 |3 D2、删除或者提取文本中出现的数字/ g- M; H v& ]7 P! D* t4 H+ `5 b
如果文本中的数字与文本分析无关的话,那就删除这些数字。 & @: |( n k1 N- u9 V3 V
# 从Github下载停用词数据 https://github.com/zhousishuo/stopwords
import jieba
import re
# 读取用于测试的文本数据 用户评论
with open('comments.txt') as f:
data = f.read()
# 文本预处理 去除一些无用的字符 只提取出中文出来
new_data = re.findall('[\u4e00-\u9fa5]+', data, re.S)
new_data = "/".join(new_data)
# 文本分词 精确模式
seg_list_exact = jieba.cut(new_data, cut_all=False)
# 加载停用词数据
with open('stop_words.txt', encoding='utf-8') as f:
# 获取每一行的停用词 添加进集合
con = f.read().split('\n')
stop_words = set()
for i in con:
stop_words.add(i)
# 列表解析式 去除停用词和单个词
result_list = [word for word in seg_list_exact if word not in stop_words and len(word) > 1]
result_list