【问题标题】:Pipeline for text cleaning / processing in pythonpython中文本清理/处理的管道
【发布时间】:2018-07-29 15:04:33
【问题描述】:

我对 python 环境(jupyter notebook)很陌生,我正在尝试处理相对庞大的文本数据。我想通过应用以下步骤并以相同的顺序来处理它:

去除空格, 小写, 词干, 删除标点符号但保留字内破折号或连字符, 删除停用词, 删除符号, 去除空格,

我希望我可以获得一个可以执行任务的函数,而不是单独执行它们,是否有任何单个库和/或函数可以提供帮助?如果没有,定义一个函数来执行它们的最简单方法是什么?

【问题讨论】:

  • 所有这些任务都很简单,可以使用 NLTK、正则表达式和 Python 内置方法的组合来完成。您可以编写自己的方法,一次获取一大块文本数据并一个接一个地应用任务。如果你想要更整洁的东西,你可以像本教程一样创建自己的管道nlpforhackers.io/building-a-nlp-pipeline-in-nltk

标签: python-3.x nlp nltk jupyter-notebook text-processing


【解决方案1】:

或者,您也可以使用我最近完成的文本数据的管道创建器类。在 github 中找到heredemo_pipe.py 几乎涵盖了您想做的事情。

【讨论】:

    【解决方案2】:

    正如评论中提到的,它可以使用 Python 中的多个库的组合来完成。一个可以执行所有操作的函数可能如下所示:

    import nltk
    import re
    import string
    from nltk.tokenize import word_tokenize, sent_tokenize
    from nltk.corpus import stopwords
    from nltk.stem import PorterStemmer # or LancasterStemmer, RegexpStemmer, SnowballStemmer
    
    default_stemmer = PorterStemmer()
    default_stopwords = stopwords.words('english') # or any other list of your choice
    def clean_text(text, ):
    
        def tokenize_text(text):
            return [w for s in sent_tokenize(text) for w in word_tokenize(s)]
    
        def remove_special_characters(text, characters=string.punctuation.replace('-', '')):
            tokens = tokenize_text(text)
            pattern = re.compile('[{}]'.format(re.escape(characters)))
            return ' '.join(filter(None, [pattern.sub('', t) for t in tokens]))
    
        def stem_text(text, stemmer=default_stemmer):
            tokens = tokenize_text(text)
            return ' '.join([stemmer.stem(t) for t in tokens])
    
        def remove_stopwords(text, stop_words=default_stopwords):
            tokens = [w for w in tokenize_text(text) if w not in stop_words]
            return ' '.join(tokens)
    
        text = text.strip(' ') # strip whitespaces
        text = text.lower() # lowercase
        text = stem_text(text) # stemming
        text = remove_special_characters(text) # remove punctuation and symbols
        text = remove_stopwords(text) # remove stopwords
        #text.strip(' ') # strip whitespaces again?
    
        return text
    

    使用(Python2.7,但也应该在 Python3 中工作)对其进行测试:

    text = '  Test text !@$%$(%)^   just words and word-word'
    clean_text(text)
    

    结果:

    u'test text word word-word'
    

    【讨论】:

    • Lemme 又来了:谢谢 Vlad,它确实成功了,但部分符合我的预期。完成任务后,我期望它可以取出单独的数字和所有标点符号,除了单词内的破折号或连字符。但是有些人仍然在那里,或者如果它不会危及这个过程,那需要我单独做。例如这样的结果; “删除 11 - 103 kb 重叠外显子 5 补充图 1b 在线使用 xq28-target”,其中“1 - 103 kb”和“5”不相关,但“xq28-target”是
    • 我建议你阅读正则表达式 HOWTO (docs.python.org/3.6/howto/regex.html)。它将帮助您了解如何使用正则表达式从文本中删除某些模式。要删除由单词边界(空格或非字母数字字符)包围的数字,请使用 re.sub(r"\b\d\b", '', text)。
    • 过滤“删除 11-103 kb 重叠外显子 5 补充图 1b 在线使用 xq28-target”后您的预期输出是什么?
    • 当然,我认为这些材料会有所帮助。我的意思是过滤;该行的“1-103 kb”和“5”
    猜你喜欢
    • 1970-01-01
    • 2015-06-08
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    相关资源
    最近更新 更多