【问题标题】:How to only return actual tokens, rather than empty variables when tokenizing?标记化时如何只返回实际标记,而不是空变量?
【发布时间】:2019-04-12 10:10:07
【问题描述】:

我有一个函数:

def remove_stopwords(text):
     return [[word for word in simple_preprocess(str(doc), min_len = 2) if word not in stop_words] for doc in texts] 

我的输入是一个带有标记语句的列表:

input = ['This', 'is', 'an', 'example', 'of', 'my', 'input']

假设stop_words包含单词:'this'、'is'、'an'、'of'和'my',那么我想得到的输出是:

desired_output = ['example', 'input']

但是,我现在得到的实际输出是:

actual_output = [[], [], [], ['example'], [], [], ['input']]

如何调整我的代码以获得此输出?

【问题讨论】:

    标签: python apply tokenize gensim


    【解决方案1】:

    您的问题有两种解决方案:

    解决方案 1:

    您的remove_stopwords 需要一组文档才能正常工作,因此您可以像这样修改您的输入

    input = [['This', 'is', 'an', 'example', 'of', 'my', 'input']]
    

    解决方案 2:

    您将 remove_stopwords 函数更改为处理单个文档

    def remove_stopwords(text):
         return [word for word in simple_preprocess(str(text), min_len = 2) if word not in stop_words]
    

    【讨论】:

      【解决方案2】:

      如果没有特定理由使用您的代码,您可以使用以下代码删除停用词。

      wordsFiltered = []
      def remove_stopwords(text):
          for w in text:
              if w not in stop_words:
                  wordsFiltered.append(w)
          return wordsFiltered
      
      input = ['This', 'is', 'an', 'example', 'of', 'my', 'input']
      
      stop_words = ['This', 'is', 'an', 'of', 'my']
      
      print remove_stopwords(input)
      

      输出:

      ['example', 'input']
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-18
        • 1970-01-01
        相关资源
        最近更新 更多