(2

您所在的位置:网站首页 分词处理的作用 (2

(2

2024-07-10 17:53| 来源: 网络整理| 查看: 265

2.3  去除停用词(Stopword Removal)

去除停用词(Stop Words)是自然语言处理中的一个常见任务,它旨在去除文本中的常见、无实际语义的词语,以便更准确地进行文本分析和处理。停用词通常包括像“a”、“an”、“the”、“in”、“on”等常见的词汇。

2.3.1  什么是停用词

停用词(Stop Words)是自然语言处理中的一类常见词汇,通常是一些在文本中频繁出现但通常被认为没有实际语义或信息价值的词汇。这些词汇通常包括常见的连接词、介词、冠词、代词和一些常见的动词等。

停用词的存在是因为它们在文本中广泛出现,但通常对文本分析和处理任务没有太多的信息价值,因为它们在不同的文本中都会出现。因此,去除这些停用词可以减少文本中的噪声,使文本处理更加准确和有效。

在现实应用中,一些常见的停用词包括:

冠词:a, an, the介词:in, on, at, by连接词:and, or, but代词:I, you, he, she, it助动词:is, am, are, have, has, do, does

停用词的具体列表可以根据不同的自然语言处理任务和语言而有所不同。去除停用词通常是文本预处理的一部分,以净化文本并减少在文本分析中的干扰。去除停用词后,文本分析算法可以更关注那些具有更高信息价值的词汇,从而提高文本处理的效率和准确性。

2.3.2  基于词汇列表的去除

最简单的去除停用词方法是使用预定义的停用词列表,将文本中包含在列表中的词汇去除。这些列表通常包括常见的连接词、介词、冠词等。例如下面是一个基于词汇列表的去除停用词例子。

实例2-13:基于词汇列表的去除停用词(源码路径:daima/2/qu01.py)

(1)首先,准备一个包含停用词的列表,例如:

stop_words = ["a", "an", "the", "in", "on", "at", "by", "and", "or", "but"]

(2)编写实例文件qu01.py,使用上面的停用词列表来去除文本中的停用词,具体实现代码如下所示。

# 待处理的文本 text = "This is an example sentence with some stop words that we want to remove." # 将文本分词 words = text.split() # 去除停用词 filtered_words = [word for word in words if word.lower() not in stop_words] # 将处理后的单词列表重建为文本 filtered_text = " ".join(filtered_words) # 显示原始文本和去除停用词后的文本 print(f"原始文本: {text}") print(f"去除停用词后: {filtered_text}")

在上述代码中,首先定义了停用词列表 stop_words,然后将文本分词,并使用列表推导式去除其中包含在停用词列表中的词汇。最后,我们将处理后的单词列表重新组合成文本。执行后会输出:

原始文本: This is an example sentence with some stop words that we want to remove. 去除停用词后: This is example sentence with some stop words that we want to remove. 2.3.3  基于词频的去除

基于词频的停用词去除方法旨在去除在文本中频率最高的词汇,因为这些词汇通常是停用词,对文本分析任务没有太多的信息价值。通常,文本中出现频率最高的词汇很可能是停用词,因此去除它们可以降低文本中的噪声。请看下面的例子,演示了使用基于词频的方法去除停用词的过程。

实例2-14:使用基于词频的方法去除停用词(源码路径:daima/2/qu02.py)

实例文件qu02.py的具体实现代码如下所示。

from collections import Counter # 待处理的文本 text = "This is an example sentence with some stop words that we want to remove. This is a simple example." # 将文本分词 words = text.split() # 计算词汇的词频 word_freq = Counter(words) # 按词频降序排序 sorted_word_freq = sorted(word_freq.items(), key=lambda x: x[1], reverse=True) # 确定频率最高的词汇 most_common_words = [word for word, freq in sorted_word_freq[:5]] # 假设保留前5个频率最高的词汇 # 去除频率最高的词汇 filtered_words = [word for word in words if word not in most_common_words] # 将处理后的单词列表重建为文本 filtered_text = " ".join(filtered_words) # 显示原始文本和去除停用词后的文本 print(f"原始文本: {text}") print(f"去除停用词后: {filtered_text}")

在上述代码中,首先将文本分词并计算词汇的词频。然后,我们按词频降序排序词汇,并选择保留前5个频率最高的词汇作为停用词。最后,我们使用列表推导式去除文本中包含在停用词列表中的词汇,然后将处理后的单词列表重新组合成文本。执行后会输出:

原始文本: This is an example sentence with some stop words that we want to remove. This is a simple example. 去除停用词后: with some stop words that we want to remove. a simple example. 2.3.4  TF-IDF算法去除

使用TF-IDF(Term Frequency-Inverse Document Frequency)算法来确定文本中词汇的重要性。根据TF-IDF值,可以去除在多个文档中频繁出现的词汇,因为这些词汇可能是停用词。请看下面的例子,演示了scikit-learn库使用TF-IDF算法去除停用词的过程。

实例2-15:使用TF-IDF算法去除停用词(源码路径:daima/2/qu03.py)

实例文件qu03.py的具体实现代码如下所示。

from sklearn.feature_extraction.text import TfidfVectorizer # 假设这是一个文档集合,每个文档是一个字符串 documents = [ "This is an example document with some stop words that we want to remove.", "Another document with stop words.", "One more example document.", ] # 定义停用词列表 stop_words = ["this", "is", "an", "with", "some", "that", "we", "to", "and", "one", "more"] # 使用TF-IDF向量化器 tfidf_vectorizer = TfidfVectorizer(stop_words=stop_words) # 训练TF-IDF模型并进行转换 tfidf_matrix = tfidf_vectorizer.fit_transform(documents) # 获取特征词汇 feature_names = tfidf_vectorizer.get_feature_names() # 将TF-IDF矩阵转换为文本 filtered_text = [] for i, doc in enumerate(documents): tfidf_scores = list(zip(feature_names, tfidf_matrix[i].toarray()[0])) filtered_words = [word for word, tfidf in tfidf_scores if tfidf > 0.2] # 通过阈值选择要保留的词汇 filtered_text.append(" ".join(filtered_words)) # 显示原始文本和去除停用词后的文本 for i, (original, filtered) in enumerate(zip(documents, filtered_text)): print(f"原始文本 {i+1}: {original}") print(f"去除停用词后 {i+1}: {filtered}") print()

在上述代码中,使用scikit-learn的TF-IDF向量化器来将文档集合转化为TF-IDF特征矩阵。我们定义了一个停用词列表 stop_words,并在TF-IDF向量化器中使用它。然后,我们通过设置一个TF-IDF阈值来选择要保留的词汇,这可以根据文本特性进行调整。执行后会输出:

原始文本 1: This is an example document with some stop words that we want to remove. 去除停用词后 1: document example remove stop want words 原始文本 2: Another document with stop words. 去除停用词后 2: another document stop words 原始文本 3: One more example document. 去除停用词后 3: document example

同上面的执行结果可知,已成功去除了停用词。原始文本中的停用词已被去除,留下了具有较高TF-IDF值的词汇。这个过程可以帮助减少文本中的噪声,提高文本分析的准确性。

2.3.5  机器学习方法去除

利用机器学习技术,可以训练模型来自动识别和去除停用词。这种方法需要标记文本中哪些词汇是停用词,然后使用分类器或聚类算法进行去除。使用机器学习方法去除停用词通常涉及训练一个二元分类器(停用词 vs. 非停用词),然后使用训练好的模型来预测文本中的词汇是否为停用词。下面是一个使用scikit-learn库的一个简单例子,使用朴素贝叶斯分类器来去除停用词。

实例2-16:使用机器学习方法去除停用词(源码路径:daima/2/qu04.py)

实例文件qu04.py的具体实现代码如下所示。

from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.naive_bayes import MultinomialNB # 准备训练集 training_samples = [ "this is a stop word", "machine learning is fun", "remove these stop words", "text analysis with ML", "use ML to remove stopwords", ] # 对应的标签,0表示停用词,1表示非停用词 training_labels = [0, 1, 0, 1, 0] stop_words = ["this", "is", "an", "with", "some", "that", "we", "to", "and", "one", "more"] # 待处理的文本 text = "this is an example text with some stop words that we want to remove using ML." # 使用TF-IDF向量化器 tfidf_vectorizer = TfidfVectorizer() X_train = tfidf_vectorizer.fit_transform(training_samples) # 训练朴素贝叶斯分类器 classifier = MultinomialNB() classifier.fit(X_train, training_labels) # 将待处理文本转化为TF-IDF特征向量 X_test = tfidf_vectorizer.transform([text]) # 使用分类器来预测词汇是否为停用词 predicted_label = classifier.predict(X_test) # 如果预测标签为1(非停用词),则保留词汇 if predicted_label == 1: print("Original Text:", text) print("Processed Text:", text) else: print("Original Text:", text) print("Processed Text:", " ".join([word for word in text.split() if word.lower() not in stop_words]))

在上述代码中,使用了一个简单的训练集,包括一些标记的停用词和非停用词样本。使用TF-IDF向量化器将文本转化为特征向量,然后使用朴素贝叶斯分类器进行训练。最后,我们使用训练好的分类器来预测待处理文本中的词汇是否为停用词,如果预测为停用词,则从文本中去除。执行后会输出:

Original Text: this is an example text with some stop words that we want to remove using ML. Processed Text: example text stop words want remove using ML. 未完待续


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3