NLP清洗函数的矢量化形式

2024-04-19 02:32:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用以下函数来清除数据集的文本注释:

import spacy
nlp = spacy.load("en")
def clean(text):
    """
    Text preprocessing for english text
    """
    # Apply spacy to the text
    doc=nlp(text)
    # Lemmatization, remotion of noise (stopwords, digit, puntuaction and singol characters)
    tokens=[token.lemma_.strip() for token in doc if 
            not token.is_stop and not nlp.vocab[token.lemma_].is_stop # Remotion StopWords
            and not token.is_punct # Remove puntuaction
            and not token.is_digit # Remove digit
           ]
    # Recreation of the text
    text=" ".join(tokens)

    return text.lower()

问题是,当我想清除所有的数据集文本时,需要一个小时又一个小时。(我的数据集是70k行,每行100到5000字)

我尝试使用swifter在multiplethread上运行apply方法,如下所示:data.note_line_comment.swifter.apply(clean)

但这并没有真正改善,因为它花了将近一个小时。你知道吗

我想知道是否有任何方法可以使我的函数向量化,或者也许还有其他方法来加速这个过程。你知道吗?你知道吗


Tags: and数据方法函数text文本cleantoken
1条回答
网友
1楼 · 发布于 2024-04-19 02:32:17

简短的回答

这类问题本身就需要时间。你知道吗

冗长的回答

  • 使用正则表达式
  • 更换空间管线

做决定所需的字符串信息越多,花费的时间就越长。你知道吗

好消息是,如果文本的清理相对简化,几个正则表达式就可以了。你知道吗

否则,您将使用空间管道来帮助删除文本位,这是非常昂贵的,因为默认情况下它会执行许多操作:

  1. 标记化
  2. 柠檬化
  3. 依赖关系分析
  4. 内尔
  5. 分块

或者,您可以再次尝试您的任务,并关闭您不想要的空间管道方面,这可能会加快它相当多。你知道吗

例如,可以关闭命名实体识别、标记和依赖项分析。。。你知道吗

nlp = spacy.load("en", disable=["parser", "tagger", "ner"])

然后再试一次,它会加速的。你知道吗

相关问题 更多 >