Generate bigrams with NLTK

后端 未结 2 2053
-上瘾入骨i
-上瘾入骨i 2021-02-07 07:33

I am trying to produce a bigram list of a given sentence for example, if I type,

    To be or not to be

I want the program to generate

2条回答
  •  春和景丽
    2021-02-07 07:47

    nltk.bigrams() returns an iterator (a generator specifically) of bigrams. If you want a list, pass the iterator to list(). It also expects a sequence of items to generate bigrams from, so you have to split the text before passing it (if you had not done it):

    bigrm = list(nltk.bigrams(text.split()))
    

    To print them out separated with commas, you could (in python 3):

    print(*map(' '.join, bigrm), sep=', ')
    

    If on python 2, then for example:

    print ', '.join(' '.join((a, b)) for a, b in bigrm)
    

    Note that just for printing you do not need to generate a list, just use the iterator.

提交回复
热议问题