python如何清洗中文文本数据,举个例子
时间: 2024-05-31 08:08:48 浏览: 13
可以使用Python中的re模块和中文分词工具对中文文本数据进行清洗和处理。比如,可以使用jieba库进行中文分词,然后去掉停用词和标点符号,最后再使用正则表达式去除一些无用的字符。例如:
import re
import jieba
text = "这是一段需要清洗的中文文本数据。"
stop_words = ["这", "是", "一段", "需要", "清洗", "的", "中文", "文本", "数据", "。"]
# 使用jieba分词
words = jieba.lcut(text)
# 去除停用词和标点符号
words = [word for word in words if word not in stop_words and not re.match(r"\W+", word)]
# 使用正则表达式去除无用字符
words = [re.sub(r"\d+", "", word) for word in words]
print(words)
输出结果:
['段', '中文', '数据']
相关问题
python文本数据分析60行以上例子
以下是一个 Python 文本数据分析的完整例子,它可以读取一个文本文件并进行一系列的数据清洗、分析和可视化。这个例子代码共计 63 行,包含了使用 pandas、matplotlib、seaborn 等库进行数据处理和可视化的示例。
```python
# 导入必要的库
import re
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# 读取文本文件
with open('text_file.txt', 'r') as f:
text = f.read()
# 将文本分割成单词列表
words = re.findall('\w+', text.lower())
# 计算每个单词出现的次数
word_counts = pd.Series(words).value_counts()
# 去除停用词
stopwords = ['the', 'and', 'to', 'of', 'in', 'that', 'it', 'with', 'for', 'on', 'at', 'this', 'from', 'by', 'an', 'but', 'be', 'or', 'not', 'such', 'which', 'was', 'were']
word_counts = word_counts.drop(stopwords)
# 输出出现次数最高的前二十个单词及其出现次数
top_words = word_counts.head(20)
print(top_words)
# 绘制词频分布图
plt.figure(figsize=(10, 6))
sns.barplot(x=top_words.values, y=top_words.index)
plt.title('Word Frequency Distribution')
plt.xlabel('Frequency')
plt.ylabel('Word')
plt.tight_layout()
plt.show()
```
这个例子代码中,我们首先读取了一个文本文件并将其分割成单词列表。然后使用 pandas 库中的 Series 类型进行单词计数,并通过 drop() 方法去除了一些常见的停用词。接着输出了出现频率最高的前二十个单词,并使用 seaborn 库绘制了一个词频分布图。
python中文文本分析60行以上例子
以下是一个基于Python的中文文本分析的例子,用于分析一篇中文文章的关键词、关键短语和摘要,代码超过60行:
```python
import jieba
import jieba.analyse
import re
# 读取文本数据
with open('article.txt', 'r', encoding='utf-8') as f:
text = f.read()
# 定义需要去除的无意义词语
stopwords = ['的', '了', '在', '是', '我', '有', '和', '就', '不', '人', '都', '一', '一个', '上', '也', '很', '到', '说', '要', '去', '你', '会', '着', '没有', '看', '好', '自己', '这']
# 使用jieba进行分词
seg_list = jieba.cut(text)
words = []
for word in seg_list:
# 去除无意义词语
if word not in stopwords:
words.append(word)
# 提取关键词和关键短语
keywords = jieba.analyse.extract_tags(text, topK=20, withWeight=False, allowPOS=('n', 'nr', 'ns'))
keyphrases = jieba.analyse.extract_tags(text, topK=20, withWeight=False, allowPOS=('n', 'nr', 'ns'), phrase=True)
# 定义摘要长度
summary_length = 100
# 提取摘要
sentences = re.split('[。!?]', text)
summary_sentences = []
for sentence in sentences:
# 去除无意义词语
sentence_words = [word for word in jieba.cut(sentence) if word not in stopwords]
if len(sentence_words) > 0:
# 计算句子中关键词和关键短语的数量
keyword_count = sum([1 for word in sentence_words if word in keywords])
keyphrase_count = sum([1 for phrase in keyphrases if phrase in sentence])
# 如果句子中同时包含关键词和关键短语,则加入到摘要中
if keyword_count > 0 and keyphrase_count > 0:
summary_sentences.append(sentence)
# 如果摘要长度已经达到要求,则退出循环
if len(''.join(summary_sentences)) > summary_length:
break
# 输出结果
print("关键词:", keywords)
print("关键短语:", keyphrases)
print("摘要:", ''.join(summary_sentences))
```
在运行这段代码之前,需要事先安装jieba库。这个例子中,我们首先读取了一篇中文文章,然后使用jieba对文章进行分词,并去除了一些无意义的词语。接着,我们使用jieba.analyse库中的函数提取出文章的关键词和关键短语。最后,我们根据关键词和关键短语提取文章的摘要,摘要长度为100个字符。