I have this sentence:
Sentence
Who the president of Kuala Lumpur is?
I am trying to extract all the words (tokenization)
low_case = df['Sentence'].str.lower().str.cat(sep=' ')
words = nltk.tokenize.word_tokenize(low_case)
word_dist = nltk.FreqDist(words)
example = pd.DataFrame(word_dist.most_common(1000),
columns=['Word', 'Freq'])
However, I would like to extract Kuala Lumpur as bi-gram, so I am considering a filter which says "if there are two consecutive words having capital letters, extract them as a unique word. So if I have this list:
Who the president of Kuala Lumpur is?
I would have (using the code above):
Word Freq
who 1
is 1
president 1
of 1
Kuala 1
Lumpur 1
is 1
but I would like to have this:
Word Freq
who 1
is 1
president 1
of 1
Kuala Lumpur 1
is 1
I think to find two consecutive capital letters I should apply the following pattern:
pattern = r"[A-Z]{2}-\d{3}-[A-Z]{2}"
o anche:
re.findall('([A-Z][\w-]*(?:\s+[A-Z][\w-]*)+)', df.Sentence.tolist())
But I do not know how to include this information in my code above.