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)
结果如下: / O k1 l3 R! ^$ |7 C* I$ O/ ?: z
' y+ ^1 H2 V' C6 c 2、删除或者提取文本中出现的数字 - D0 z6 j- t4 d% P1 x# |9 J 如果文本中的数字与文本分析无关的话,那就删除这些数字。 . n9 o+ Q9 F4 g: d
# 从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