一尘不染

如何从此列表中创建 bi_grams?

javascript

如何从此列表创建 bi_grams ?

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

阅读 68

收藏
2022-10-08

共1个答案

一尘不染

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)

输出

{('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}
2022-10-08