繁体   English   中英

找出列表中三个单词在给定文档中共同出现的频率

[英]find the frequency of three words that are in the list co-occured in combination in the given document

我在桌面中有多个.txt 文件作为 python 中的数据框加载。 我在数据框中的 python 中工作,其中“文本”是我感兴趣的列的名称。 “文本”列由多个 .txt 文档组成。

我还想到了三个单词列表:它们是:

credit=['borrow', 'lend'],
policy=['Fed', 'fund rate','zero'],
trade=['deficit', 'surplus'],

我的目标是构建一个索引,通过分别对每个文档应用它来测量文本文件中给定句子中三个列表中的任何单词的频率。 例如,如果给定句子中同时出现“借”、“基金”和“盈余”,则enter code here将被计为 1。

我知道如何使用单个单词进行计数,如下所示:

my_dir_path ='C:/Users/desktop'
results = defaultdict(list)
for file in Path(my_dir_path).iterdir():
    with open(file, "r") as file_open:
        results["file_name"],(file.name)
        results["text"].append(file_open.read())
        df = pd.DataFrame(results)

为了获得跨文档的单词策略的频率,我使用了以下代码:

df['policy']=df['text'].apply(lambda x: len([word for word in x.split() if     word=='policy']))

如何在 python 中做到这一点? 提前感谢您的帮助?

我很想使用正则表达式来匹配句子中的单词,使用lookahead/behind我们可以使用类似的东西:

(?<!\w)borrow(?!\w)

并且会在"can I borrow that""will borrow." 但不是"borrowing" 我不确定你到底想在这里做什么,但我建议学习如何使用正则表达式,因为它们可以让你轻松表达这些选项

为了使以下代码更短,我定义了一个 function 来将“单词”编译为正则表达式 object:

import re

def matcher(word):
    return re.compile(fr'(?<!\w){word}(?!\w)', re.IGNORECASE)

re_credit = [
    matcher('borrow'),
    matcher('fund'),
]

接下来我写了一个 function 把一个字符串分成句子,这样我们就可以计算单词的共现:

from nltk.tokenize import sent_tokenize

def count_sentences_matching_words(text, regexes):
    count = 0
    for sentence in sent_tokenize(text):
        if all(reg.search(sentence) for reg in regexes):
            count += 1
    return count

接下来我们可以用一些文本来测试它:

para = "My goal is to construct the index that measures the frequency of any of the words from the three lists in combination in a given sentence in the text file by applying it for each document separately. For example if 'borrow', 'fund' and 'surplus' co-occurred in a given sentence, it willenter code here be counted as 1."

count_sentences_matching_words(para, re_credit)

如果您想将其与 pandas 一起使用,您可以做的很明显:

df['credit'] = df['text'].apply(lambda x: count_sentences_matching_words(x, re_credit))

重新排列这段代码可能是值得的,例如,每个文件只做一次句子标记。 但这取决于您分享的更多细节

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM