Background: The following code works to perform a toy example of bigram analysis:
import nltk
from nltk import bigrams
from nltk.tokenize import word_tokenize
text = "some nice words go here"
tokens = word_tokenize(text)
bi_tokens = bigrams(tokens)
bi_count = {}
for token in bi_tokens:
if token not in bi_count:
bi_count[token] = 1
else:
bi_count[token] += 1
Output:
print(bi_count)
{('go', 'here'): 1,
('nice', 'words'): 1,
('some', 'nice'): 1,
('words', 'go'): 1}
Problem: I would like to use the key
name (e.g.('go', 'here')
) to get the corresponding value
(e.g. 1
) .
I have tried searching http://www.nltk.org/api/nltk.html?highlight=freqdist and also How to access specific element of dictionary of tuples but I have not been able to find the answer.
Question: Is there a way to solve my problem by using an nltk
method or by any other means?