Получить биграммы, частоту для каждой строки текстовых данных в фрейме данных.
Попытка генерирования биграмм с использованием приведенного ниже кода, чтобы получить отдельный фрейм данных с биграммами и частотой для фрейма данных с текстовым полем. Я хочу получить биграммы для каждой строки текста, которая у меня есть в кадре данных, чтобы я мог получить доступ к другим связанным полям для дальнейшего анализа.
def get_top_n_bigram(corpus, n=None):
vec = CountVectorizer(ngram_range=(2, 2),
stop_words='english').fit(corpus)
bag_of_words = vec.transform(corpus)
sum_words = bag_of_words.sum(axis=0)
words_freq = [(word, sum_words[0, idx]) for word, idx in
vec.vocabulary_.items()]
words_freq =sorted(words_freq, key = lambda x: x[1], reverse=True)
return words_freq[:n]
common_words = get_top_n_bigram(data,)
mybigrams= pd.DataFrame(common_words, columns = ['Bigrams' , 'count'])
mybigrams is the dataframe with bigrams and their corresponding frequency.
My dataframe had 2 fields Text & V1:
Text V1
Robots.txt is a text file to crawl pages on their website. 10
The robots.txt file is part of the robots exclusion protocol (REP). 12
The REP also includes directives like meta robots as well as page. 22
This is what I have now:
Bigrams count
0 robots txt 3
1 robots crawl 2
2 certain user 2
3 user agents 2
4 robots exclusion 2
5 meta robots 2
I want something like:
Text V1 Bigrams count
Robots.txt is a text ... 10 robots txt 3
The robots.txt file ... 12 robots exclusion 2
The REP also includes ... 22 meta robots 2