How can I create a bi_grams from this list?

Question:

How can I create a bi_grams from this list ?

text = ['to','be',',','or','not','to','be',',','that','is','the','question',':']
Asked By: zari

||

Answers:

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)))
Answered By: YJR
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}
Answered By: sourin
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.