【问题标题】:NLP Generate CollectionNLP 生成集合
【发布时间】:2020-09-02 00:00:57
【问题描述】:

我正在动手做,预期的输出是

[('fans', 3), ('car', 3), ('disciplines', 1)]

['跑车','体育迷']

我的代码如下。我能够获得第一个预期输出,但无法正确获得第二个输出。谁能帮我看看这里出了什么问题

    from nltk.tokenize import RegexpTokenizer
    text='Thirty-five sports disciplines and four cultural activities will be offered during seven days of competitions. He skated with charisma, changing from one gear to another, from one direction to another, faster than a sports car. Armchair sports fans settling down to watch the Olympic Games could be for the high jump if they do not pay their TV licence fee. Such invitationals will attract more viewership for sports fans by sparking interest among sports fans. She barely noticed a flashy sports car almost run them over, until Eddie lunged forward and grabbed her body away. And he flatters the mother and she kind of gets prissy and he talks her into going for a ride in the sports car.'
    word='sports'
    tokenizedword = nltk.tokenize.regexp_tokenize(text, pattern = '\w*', gaps = False)
    #Step 2
    tokenizedwords = [x.lower() for x in tokenizedword if x != '']

    tokenizedwordsbigram=list(nltk.bigrams(tokenizedwords))
    stop_words = set(stopwords.words('english')) 
    filteredwords = []
    for x in tokenizedwordsbigram:
       if x not in stop_words:
          filteredwords.append(x)
     
    tokenizednonstopwordsbigram = nltk.ConditionalFreqDist(filteredwords)  
    print(tokenizednonstopwordsbigram[word].most_common(3))
    gen_text=nltk.Text(tokenizedwords)
    print(gen_text.collocations())

【问题讨论】:

    标签: nlp


    【解决方案1】:

    替换

    print(gen_text.collocations())
    

    print(gen_text.collocation_list())
    

    你的程序可以正常运行

    【讨论】:

      【解决方案2】:

      我通过添加所需的导入 nltk importfrom nltk.corpus import stopwords 运行代码并得到以下输出。

      import nltk
      from nltk.corpus import stopwords
      from nltk.tokenize import RegexpTokenizer
      
      # use to find bigrams, which are pairs of words
      
      text = \
          'Thirty-five sports disciplines and four cultural activities will be offered during seven days of competitions. He skated with charisma, changing from one gear to another, from one direction to another, faster than a sports car. Armchair sports fans settling down to watch the Olympic Games could be for the high jump if they do not pay their TV licence fee. Such invitationals will attract more viewership for sports fans by sparking interest among sports fans. She barely noticed a flashy sports car almost run them over, until Eddie lunged forward and grabbed her body away. And he flatters the mother and she kind of gets prissy and he talks her into going for a ride in the sports car.'
      word = 'sports'
      tokenizedword = nltk.tokenize.regexp_tokenize(text, pattern='\w*',
              gaps=False)
      
      # Step 2
      tokenizedwords = [x.lower() for x in tokenizedword if x != '']
      
      tokenizedwordsbigram = list(nltk.bigrams(tokenizedwords))
      stop_words = set(stopwords.words('english'))
      filteredwords = []
      
      for x in tokenizedwordsbigram:
          if x not in stop_words:
              filteredwords.append(x)
      
      tokenizednonstopwordsbigram = nltk.ConditionalFreqDist(filteredwords)
      print tokenizednonstopwordsbigram[word].most_common(3)
      
      gen_text = nltk.Text(tokenizedwords)
      print gen_text.collocations()
      

      这是输出:

      [('car', 3), ('fans', 3), ('disciplines', 1)]
      sports car; sports fans
      None
      

      【讨论】:

      • 谢谢。我得到的输出与您在 jupyter note 中得到的输出相同,但我的动手操作是在运行hackerrank,并且出现以下错误--> collectionwords=gen_text.collocations() File "/var/ml/python3/lib/python3.7/ site-packages/nltk/text.py",第 444 行,搭配 w1 + " " + w2 for w1, w2 in self.collocation_list(num, window_size) File "/var/ml/python3/lib/python3.7/ site-packages/nltk/text.py", 第 444 行,在 w1 + " " + w2 for w1, w2 in self.collocation_list(num, window_size) ValueError: too many values to unpack (expected 2)跨度>
      【解决方案3】:

      gen_text = nltk.Text(tokenizedwords).collocation_list()

      b=[i[0]+" "+i[1] for i in gen_text]

      返回 b

      您将输出为:

      ['跑车','体育迷']

      【讨论】:

      • 欢迎来到 Stack Overflow!请花点时间阅读editing help 中的help center。 Stack Overflow 上的格式与其他网站不同。
      【解决方案4】:
      from nltk.corpus import stopwords
      
      def performBigramsAndCollocations(textcontent, word):
          stop_words=set(stopwords.words('english'))
          pattern =r'\w+'
          tokenizewords=nltk.regexp_tokenize(textcontent,pattern)
          tokenizewords=[word.lower() for word in tokenizewords]
          tokenizewordsbiagrams=nltk.bigrams(tokenizewords)
          tokenizednonstopwordbigrams=[(w1,w2) for w1,w2 in tokenizewordsbiagrams if w1 not in stop_words and w2 not in stop_words]
          cfd_bigrams=nltk.ConditionalFreqDist(tokenizednonstopwordbigrams)
          cfd_bigrams=cfd_bigrams[word]
          mostfrequentwordafter=cfd_bigrams.most_common(3)
          collocationwords=nltk.Text(tokenizewords)
          collocationwords=collocationwords.collocation_list()
          collocationwords=[i[0]+" "+i[1] for i in collocationwords]
          
          return mostfrequentwordafter,collocationwords
      

      你可以试试这个。它对我有用!

      【讨论】:

      • 请在您的答案中添加一些解释,以便其他人可以从中学习
      猜你喜欢
      • 1970-01-01
      • 2017-05-25
      • 1970-01-01
      • 2011-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多