文本后处理

数据挖掘 数据挖掘 nlp 文本挖掘
2022-02-22 13:35:20

我有一组报纸文章,我使用 TextRank 算法来识别它们的关键字以执行分类。

除了重要的信息关键字之外,我还收到如下垃圾关键字。

"viewed", "flutter", "function", "k", "neighbour"

有没有办法发布处理和删除这些关键字?

2个回答

是的,您可以通过将它们添加到所有此类单词的现有 NLTK 停用词词典/创建自定义停用词词典来做到这一点。

对于自定义停用词词典,您需要在处理之前将所有关键词包含在词典中,请确保检查停用词词典中是否存在这些词。如果是,请将它们从文本中删除,否则无需执行任何操作。

  1. 一组代码用于将新词更新NLTK 停用词词典

    #Custom function to remove the stop-words def removeStopWords(str): #select english stopwords from NLTK cachedStopWords = set(stopwords.words("english")) #add custom words, mentioned above cachedStopWords.update(("viewed", "flutter", "function", "k", "neighbour")) #remove stop words new_str = ' '.join([word for word in str.split() if word not in cachedStopWords]) return new_str

  2. 一组代码用于制作自定义停用词词典

    stopwordList = ["viewed", "flutter", "function", "k", "neighbour"] StopWordsRemover(inputCol="words", outputCol="filtered" ,stopWords=stopwordList)

识别无信息的单词不是一件容易的事,并且取决于领域。例如,停用词或标点符号通常对情感分析有很大的区别。

如果要测试一个词的分类关键性,可以使用选择分数作为chi2、信息增益或互信息。chi2 分数基本上评估了根据出现次数区分两个类别的分数有多好。

因此,您可以使用此技术删除得分最低的单词。可以按照@Toros91 的说明进行后处理,并补充说,例如,如果您认为字符是垃圾,则可以根据字符串长度轻松地事先删除它们。

找到单词的“垃圾”我认为不是一个容易的问题,并且在文本分类中经常被过度简化。

如果您有进一步的疑问,请不要犹豫。

编辑:添加代码示例

使用 scikit-learn :

from sklearn.datasets import load_iris
from sklearn.feature_selection import SelectPercentile
from sklearn.feature_selection import chi2
import numpy

iris = load_iris()
X, y = iris.data, iris.target
selector = SelectPercentile(score_func=chi2, percentile=100)
X_reduced = selector.fit_transform(X, y)
indices = numpy.argsort(selector.scores_)
# array containing the indices of features according to ascendant chi2 score

然后,您可以使用索引根据数据中的卡方去除 k 个最差特征。