如何解决计算一个句子中同时出现的两个单词的出现频率
我有一个pandas数据框,其中一栏中有经过修饰的文本。
我想计算两个给定单词在同一句子中同时出现的频率,并计算这些单词在文档中出现多少次。例如,给定“ I”和“ have”,计算文档“ I”和“ have”在同一句子中一起出现多少次。
理想情况下,我想创建一个新的DataFrame,其结果是在一栏中我将两个单词放在一起,在另一栏中将两个单词一起出现在一个句子中,而在第三栏中则是原始文本。
我的结果需要像这样:
text,given_words,frequency_in_sentence
text1 | "I have " | 2 times in same sentence
text2 | "I have " | 3 times in same sentence
text3 | "I have " | 1 times in same sentence
解决方法
这是伪代码,但可以用于任何语言:
word1="whatever"
word2="yes"
for (text:texts)
sentances=text.getSentances()
count=0
for (sentance:sentances)
if ( sentance.contains(word1,word2) )
count++
print ( "text " + text.name + " " + word1 + " " + word2 + " appears in same sentances " + count + " times" )
然后您将需要以下方法来“确定”
boolean contains (String ... words){
int args = words.length;
int matchCount=0;
for (word : words)
if (this.text.match(word)
matchCount++ && continue
if matchCount==args
return true
return false
}
,
您可以使用count
并通过数据框上的apply函数使用它:
def count(sentence,pattern):
""" count pattern occurence """
return word.count(sentence)
df['frequency_in_sentence'] = df.apply(lambda row:count(row['text'],row['given_words']),axis = 1)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。