How can I create a bi_grams from this list ?
text = ['to','be',',','or','not','to','be',',','that','is','the','question',':']
CodePudding user response:
text = ['to','be',',','or','not','to','be',',','that','is','the','question',':']
bi_grams = [(text[t1], text[t1 1]) for t1 in range(len(text)-1)]
bg = [(x, bi_grams.count(x)) for x in list(set(bi_grams))]
bg.sort(key=lambda x:x[1], reverse=True)
result = {x[0]: x[1] for x in bg}
print(result)
output
{('to', 'be'): 2, ('be', ','): 2, ('that', 'is'): 1, ('question', ':'): 1, ('is', 'the'): 1, ('the', 'question'): 1, (',', 'or'): 1, ('not', 'to'): 1, ('or', 'not'): 1, (',', 'that'): 1}
CodePudding user response:
For this task you can use nltk library as follows.
import nltk
text = ['to','be',',','or','not','to','be',',','that','is','the','question',':']
print(list(nltk.bigrams(text)))