Forming Bigrams of words in list of sentences with Python

前端 未结 10 1493
遇见更好的自我
遇见更好的自我 2020-12-24 02:16

I have a list of sentences:

text = [\'cant railway station\',\'citadel hotel\',\' police stn\']. 

I need to form bigram pairs and store the

10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-24 03:00

    from nltk import word_tokenize 
    from nltk.util import ngrams
    
    
    text = ['cant railway station', 'citadel hotel', 'police stn']
    for line in text:
        token = nltk.word_tokenize(line)
        bigram = list(ngrams(token, 2)) 
    
        # the '2' represents bigram...you can change it to get ngrams with different size
    

提交回复
热议问题