计算列的所有行中2个单词组合的频率
我想在列的所有行中计算2个单词组合的频率。
我有一个带有两个列的表 - 第一个是带有句子的列,另一个是该句子的bigram令牌化。
句子 | 单词 |
---|---|
“美好的一天”“美丽 | 的一天” |
“美丽的一天”遭受了“ | 日子”的“美好的一天”, |
“美丽的一天”遭受了 | 苦难 |
忍受 | “遭受“无法 |
泪水” | 的 后背' |
“无法阻止眼泪” | “后泪” |
“灰烬后泪水美丽的一天” | “灰烬后'' |
灰色背泪美丽的一天 | ”“后泪” |
泪”美丽的一天 | ' |
“灰烬背 日“ | 美丽的一天” |
我所需的输出是一列,计算整个df ['句子']列中所有句子中单词的频率。 类似的事情:
句子 | 单词 | 美丽 |
---|---|---|
“美丽的一天”在“ | 的一天” | 2 |
“美丽的一天”中遭受了“遭受的 | “遭受” | 1 |
'美丽的一天,“美丽的一天”遭受 | 了“ | 1 |
'无法阻止眼泪”的“遭受 | 的痛苦”。 | 1 |
'不能阻止眼泪' | “阻止” | 1 |
'不能阻止眼泪' | 返回眼泪' | 2 |
“灰分泪水美丽 | 的日子” “灰分” | 1'sh |
ash aread tears teake | tebiled | ' |
teake '眼泪美丽的一天' | 眼泪美丽的' | 1 |
'灰背泪美丽的一天 | “美丽的一天” | 2 |
等。
我尝试过的代码重复第一个相同的频率,直到句子结束为止。
df.Sentence.str.count('|'.join(df.words.tolist()))
因此,不是我要寻找的东西,也需要很长时间,因为我的原始DF要大得多。
NLTK或任何其他库中是否有任何替代方案或任何功能?
I want to count the frequency of 2 words combination in all the rows of a column.
I have a table with two columns - The first is a column with a sentence while the other is the bigram tokenization of that sentence.
Sentence | words |
---|---|
'beautiful day suffered through ' | 'beautiful day' |
'beautiful day suffered through ' | 'day suffered' |
'beautiful day suffered through ' | 'suffered through' |
'cannot hold back tears ' | 'cannot hold' |
'cannot hold back tears ' | 'hold back' |
'cannot hold back tears ' | 'back tears' |
'ash back tears beautiful day ' | 'ash back' |
'ash back tears beautiful day ' | 'back tears' |
'ash back tears beautiful day ' | 'tears beautiful' |
'ash back tears beautiful day ' | 'beautiful day' |
My desired output is a column counting the frequency of the words in all the sentences throughout the whole df['Sentence'] column.
Something like this:
Sentence | Words | Total |
---|---|---|
'beautiful day suffered through ' | 'beautiful day' | 2 |
'beautiful day suffered through ' | 'day suffered' | 1 |
'beautiful day suffered through ' | 'suffered through' | 1 |
'cannot hold back tears ' | 'cannot hold' | 1 |
'cannot hold back tears ' | 'hold back' | 1 |
'cannot hold back tears ' | 'back tears' | 2 |
'ash back tears beautiful day ' | 'ash back' | 1 |
'ash back tears beautiful day ' | 'back tears' | 2 |
'ash back tears beautiful day ' | 'tears beautiful' | 1 |
'ash back tears beautiful day ' | 'beautiful day' | 2 |
and so on.
The code I have tried repeats the first same frequency until the end of the sentence.
df.Sentence.str.count('|'.join(df.words.tolist()))
So not what I am looking for and it also takes a very long time as my original df is much larger.
Is there any alternative or any function in the NLTK or any other library?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议:
句子的开头和结尾处删除引号和空格
和words
words < /code>作为字符串对象:
words_occur
words
并总结其出现:结果
I suggest:
Sentences
andwords
Sentences
andwords
as string objects:words_occur
words
and sum up their occurrences:Result
我理解的方式是,您希望每个唯一句子中包含的bi-gram数量。单词列中已经存在的答案已经存在。
value_counts()
用于交付。The way I understand it is that you want a bi-gram count as contained in each unique sentence. The answer for that already exists in the words column.
value_counts()
is used to deliver that.