【问题标题】:Calculate number of filtered Bigrams计算过滤的 Bigrams 的数量
【发布时间】:2019-09-22 11:38:51
【问题描述】:

处理 NLP 上的问题并陷入下面给出的 TASK。

以下是需要按顺序执行的语句。

我已完成以下步骤,但壁画平台不接受解决方案。

请让我知道我在以下代码和步骤中做错了什么

任务

1.导入文本语料棕色

  1. 提取与属于 新闻类型。将结果存储在变量 news_words 中。

  2. 将列表 news_words 中的每个单词转换为小写,并存储 结果是 lc_news_words

  3. 计算列表 lc_news_words 的二元组,并将其存储在变量中 lc_news_bigrams

  4. lc_news_bigrams,过滤两个单词都只包含的二元组 字母字符。将结果存储在 lc_news_alpha_bigrams 中。

  5. 提取与语料库停用词相关的单词列表。存储 结果是 stop_words

  6. 将列表 stop_words 中的每个单词转换为小写,并存储 结果是 lc_stop_words

  7. 仅过滤来自 lc_news_alpha_bigrams 中的单词所在的二元组 不属于 lc_stop_words。将结果存储在 lc_news_alpha_nonstop_bigrams

  8. 打印过滤的二元组总数。

下面是我到目前为止所做的代码。但是fresco平台不接受输出。

import nltk

import nltk.corpus

from nltk.corpus import brown

from nltk.util import bigrams

from nltk.corpus import stopwords

news_words = brown.words(categories='news')

lc_news_words  = [w.lower() for w in news_words]

lc_news_bigrams = list(nltk.bigrams(lc_news_words))

lc_news_alpha_bigrams = [(word1, word2) for word1, word2 in lc_news_bigrams if (word1.isalpha() and word2.isalpha()) ]

stop_words = stopwords.words('english')

lc_stop_words = [w.lower() for w in stop_words ]

lc_news_alpha_nonstop_bigrams = [ (w1, w2) for w1, w2 in lc_news_alpha_bigrams if (w1.lower() not in lc_stop_words and w2.lower() not in lc_stop_words) ] 

len((lc_news_alpha_nonstop_bigrams))

【问题讨论】:

    标签: nlp nltk python-3.7


    【解决方案1】:

    你做的一切都是正确的,只需从

    中删除参数'english'
    stop_words = stopwords.words('english')
    
    stop_words = stopwords.words()
    
    

    会起作用

    【讨论】:

      【解决方案2】:
      
      from nltk.corpus import brown
      from nltk.corpus import stopwords
      import nltk
      news_words = [word for word in brown.words(categories = 'news')]
      lc_news_words = [word.lower() for word in news_words]
      len_news_words = [len(word) for word in lc_news_words]
      news_len_bigrams = list(nltk.bigrams(len_news_words))
      cfd_news = nltk.ConditionalFreqDist(news_len_bigrams)
      print(cfd_news[4][6])
      lc_news_bigrams = list(nltk.bigrams(lc_news_words))
      lc_news_alpha_bigrams = [(w1,w2) for w1,w2 in lc_news_bigrams if w1.isalpha() and w2.isalpha()]
      stop_words  = stopwords.words()
      lc_stop_words = [word.lower() for word in stop_words]
      lc_news_alpha_nonstop_bigrams = [(w1,w2) for w1,w2 in lc_news_alpha_bigrams if  not( (w1  in  lc_stop_words) or  (w2  in lc_stop_words ))]
      print(len(lc_news_alpha_nonstop_bigrams))
      

      我已经为 fresco 平台上的 task2 和 task3 添加了代码,但平台不接受它。

      可能是什么问题?

      【讨论】:

      • Logicwise 上面的代码是有效的。可能是 Katacode 的问题。我已经看到实际答案和预期答案有 256 的差异。
      【解决方案3】:

      使用 and 代替 or (lc_stop_words 中的 w1) 或 (lc_stop_words 中的 w2)

      【讨论】:

        【解决方案4】:
        import nltk
        from nltk.corpus import brown
        from nltk.corpus import stopwords
        news_words     = brown.words(categories='news')
        lc_news_words  = [l.lower() for l in news_words]
        len_news_words = [len(w) for w in lc_news_words]
        news_len_bigrams = list(nltk.bigrams(len_news_words))
        cfd_news      = nltk.ConditionalFreqDist(news_len_bigrams)
        cfd_news.tabulate(conditions=[6,4])
        
        lc_news_bigrams = list(nltk.bigrams(lc_news_words))
        lc_news_alpha_bigrams = [(w1,w2) for w1,w2 in lc_news_bigrams if (w1.isalpha() and w2.isalpha())]
        stop_words = stopwords.words()
        lc_stop_words = [l.lower() for l in stop_words]
        lc_news_alpha_nonstop_bigrams = [ (w1, w2) for w1, w2 in lc_news_alpha_bigrams if (w1.lower() not in lc_stop_words and w2.lower() not in lc_stop_words) ]
        print(len((lc_news_alpha_nonstop_bigrams)))
        

        最终的工作代码 - Python 3

        【讨论】:

          猜你喜欢
          • 2023-03-12
          • 1970-01-01
          • 2019-12-15
          • 1970-01-01
          • 2019-03-21
          • 2021-12-22
          • 1970-01-01
          • 1970-01-01
          • 2017-12-11
          相关资源
          最近更新 更多