Generate bigrams with NLTK

后端 未结 2 2020
-上瘾入骨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:52

    The following code produce a bigram list for a given sentence

    >>> import nltk
    >>> from nltk.tokenize import word_tokenize
    >>> text = "to be or not to be"
    >>> tokens = nltk.word_tokenize(text)
    >>> bigrm = nltk.bigrams(tokens)
    >>> print(*map(' '.join, bigrm), sep=', ')
    to be, be or, or not, not to, to be
    

提交回复
热议问题