文章目录
目录
前言
数据中蕴含的信息和知识有时候被埋没在海量的文字中,而要从中提取出有用的信息,就需要进行文本挖掘。接下来,我们将一步步学习文本挖掘的基本概念。我会尽可能用通俗易懂的语言解释复杂的技术术语,保证你不会迷失在专业名词堆中。从文本清洗,到特征提取,再到文本分类和情感分析,我会为你提供一系列的实用技巧和案例,让你从零开始掌握文本挖掘的技能。
首先,需要将文本数据进行预处理
在数据预处理的过程中,我们需要进行以下一些操作:
第一步,去除特殊字符和标点符号
文本数据中常常包含一些特殊字符和标点符号,例如@、#、$等。这些符号对于文本分析可能没有实际意义,因此需要将其去除。
代码如下:
在这里我使用的是python的内置库re,使用正则表达式进行文件处理,在最后的四步总结中会使用更加方便和更加容易利用的方法
# 去除特殊字符和标点符号
import re
def remove_special_characters(text):
# 使用正则表达式去除特殊字符和标点符号
text = re.sub('[^a-zA-Z]', ' ', text) # sub函数查找匹配某个正则表达式的字符,替换为空格
return text
# 对文件地址的填写
input_filename = 'input.txt' # 输入文本文件地址
output_filename = 'output.txt' # 输出文本文件地址
# 使用with可以避免忘记关闭文件
with open(input_filename, 'r') as input_file:
text = input_file.read()
# 调用函数进行数据处理
processed_text = remove_special_characters(text)
# 将处理后的数据进行保存
with open(output_filename, 'w') as output_file:
output_file.write(processed_text)
print("文本处理完成!")
第二步,去除停用词
停用词是指那些在文本中频繁出现但对于文本分析没有实际意义的词语,例如“的”、“是”、“在”等。我们可以使用预定义的停用词表,将这些词语从文本中删除。
代码如下:
# 导入NLTK库和停用词列表
import nltk
from nltk.corpus import stopwords
nltk.download('stopwords')
# 去除停用词函数,接收一个字符串参数text
def remove_stopwords(text):
# 使用NLTK库提供的停用词列表
stop_words = set(stopwords.words('english'))
tokens = text.split()
filtered_tokens = [word for word in tokens if word.lower() not in stop_words]
return ' '.join(filtered_tokens)
# 打开文本文件进行处理
input_filename = 'input.txt' # 输入文本文件地址
output_filename = 'output.txt' # 输出文本文件地址
# 打开输入文件并读取文本内容
with open(input_filename, 'r') as input_file:
text = input_file.read()
# 调用remove_stopwords函数去除停用词
processed_text = remove_stopwords(text)
# 打开输出文件并写入处理后的文本内容
with open(output_filename, 'w') as output_file:
output_file.write(processed_text)
# 输出处理完成的提示信息
print("文本处理完成!")
第三步,小写化
为了统一文本的格式,我们通常将所有的字母转换为小写。这样可以避免同一个词在不同位置被当作不同的词语来处理,例如在统计词频时会出现问题。
代码如下:
1def lowercase_file():
# 打开文本文件进行处理
input_filename = 'input.txt' # 输入文本文件地址
output_filename = 'output.txt' # 输出文本文件地址
# 打开输入文件进行处理
with open(input_filename, 'r') as input_file:
text = input_file.read()
# 转为小写字母
lowercased_text = text.lower()
# 将处理后的文本写入输出文件
with open(output_filename, 'w') as output_file:
output_file.write(lowercased_text)
print("文本处理完成!")
# 调用函数进行文本处理
lowercase_file()
第四步,词干化或词形还原
词干化是指将单词转换为它的词干形式,例如将“running”转换为“run”。词形还原是指将单词转换为它的原始形式,例如将“went”转换为“go”。这样可以减少词语的变形,使得模型能够更好地理解文本。
代码如下:
# 导入NLTK库和相关模块
from nltk.stem import PorterStemmer, WordNetLemmatizer
import nltk
# 文件处理函数,接收输入文件名、词干化输出文件名和词形还原输出文件名作为参数
# 分别执行 stemming 和 lemmatization,并将结果写入输出文件
# 最后打印处理完成的提示信息
# 词干化函数,接收一个字符串参数text,并返回词干化后的字符串
def stemming(text):
# 使用Porter词干提取算法进行词干化
stemmer = PorterStemmer()
tokens = text.split()
stemmed_tokens = [stemmer.stem(word) for word in tokens]
return ' '.join(stemmed_tokens)
# 词形还原函数,接收一个字符串参数text,并返回词形还原后的字符串
def lemmatization(text):
# 使用WordNet词形还原进行词形还原
lemmatizer = WordNetLemmatizer()
tokens = text.split()
lemmatized_tokens = [lemmatizer.lemmatize(word) for word in tokens]
return ' '.join(lemmatized_tokens)
# 下载WordNet词形还原的资源
nltk.download('wordnet')
def process_file(input_filename, stemming_output_filename, lemmatization_output_filename):
# 打开输入文件进行处理
with open(input_filename, 'r') as input_file:
text = input_file.read()
# 进行 stemming(词干化) 和 lemmatization(词形还原)
stemmed_text = stemming(text)
lemmatized_text = lemmatization(text)
# 将 stemming(词干化) 结果写入输出文件
with open(stemming_output_filename, 'w') as stemming_output_file:
stemming_output_file.write(stemmed_text)
# 将 lemmatization(词形还原) 结果写入输出文件
with open(lemmatization_output_filename, 'w') as lemmatization_output_file:
lemmatization_output_file.write(lemmatized_text)
print("文本处理完成!")
# 调用 process_file 函数,传入对应的参数进行文件处理
process_file('input.txt', 'stemming_output.txt', 'lemmatization_output.txt')
第五步,代码进行整合
代码如下:
在这里没有使用re库而是使用了nltk库中更加方便的模块,使的处理数据变得更加容易
from nltk.stem import PorterStemmer, WordNetLemmatizer
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import string
import nltk
# 下载所需的数据
nltk.download('punkt')
nltk.download('wordnet')
nltk.download('stopwords')
def process_text(input_file, output_file):
# 使用nltk的词干提取器
stemmer = PorterStemmer()
# 使用nltk的词形还原器
lemmatizer = WordNetLemmatizer()
# 获取停用词集合
stop_words = set(stopwords.words('english'))
# 打开文件进行读取
with open(input_file, 'r') as file:
text = file.read()
# 分词
tokens = word_tokenize(text.lower())
# 去除标点符号和停用词
tokens = [token for token in tokens if token not in string.punctuation and token not in stop_words]
# 进行 stemming 和 lemmatization
stemmed_tokens = [stemmer.stem(token) for token in tokens]
lemmatized_tokens = [lemmatizer.lemmatize(token) for token in tokens]
# 将处理后的结果写入输出文件
with open(output_file, 'w') as file:
file.write('Original Text:\n')
file.write(text)
file.write('\n\nStemmed Tokens:\n')
file.write(' '.join(stemmed_tokens))
file.write('\n\nLemmatized Tokens:\n')
file.write(' '.join(lemmatized_tokens))
# 指定输入和输出文件的路径
input_file = 'input.txt'
output_file = 'output.txt'
# 调用处理函数
process_text(input_file, output_file)
总结
通过以上的数据预处理的一步步的步骤,我们可以清洗并转换原始的文本数据,为后续的文本挖掘任务做好准备。当然,在实际应用中,数据预处理的具体步骤还可能会有所不同,需要根据具体的文本任务和数据特点进行调整。
因为在数据预处理阶段使用的nltk库是一个非常好用的库,后期我会出一期nltk库的详细介绍,方便大家学习和使用,希望我的博客能为您带来一些有用的信息,如果您对文本挖掘和数据预处理还有其他问题,欢迎留言讨论,祝您阅读愉快!
如果对你有帮助的话,不妨给个三连!!!
求三连!!! 求三连!!! 求三连!!!