簡體   English   中英

我怎樣才能從這個列表中創建一個 bi_grams?

[英]How can I create a bi_grams from this list?

我怎樣才能從這個列表中創建一個 bi_grams?

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

對於此任務,您可以使用 nltk 庫,如下所示。

    import nltk
    text = ['to','be',',','or','not','to','be',',','that','is','the','question',':']
    print(list(nltk.bigrams(text)))
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}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM