【发布时间】:2020-07-20 17:04:27
【问题描述】:
我有兴趣找出一组单词(以 n_grams 为单位)在句子中出现的频率(百分比)。
example_txt= ["order intake is strong for Q4"]
def find_ngrams(text):
text = re.findall('[A-z]+', text)
content = [w for w in text if w.lower() in n_grams] # you can calculate %stopwords using "in"
return round(float(len(content)) / float(len(text)), 5)
#the goal is for the above procedure to work on a pandas datafame, but for now lets use 'text' as an example.
#full_MD['n_grams'] = [find_ngrams(x) for x in list(full_MD.loc[:,'text_no_stopwords'])]
您可以在下面看到两个示例。第一个有效,最后一个无效。
n_grams= ['order']
res = [find_ngrams(x) for x in list(example_txt)]
print(res)
Output:
[0.16667]
n_grams= ['order intake']
res = [find_ngrams(x) for x in list(example_txt)]
print(res)
Output:
[0.0]
如何使 find_ngrams() 函数处理二元组,所以上面的最后一个示例有效?
编辑:还有其他想法吗?
【问题讨论】:
-
什么是 n_gram ???
-
这是我感兴趣的单词/单词,以找出它在 example_txt 中被提及的频率。
-
道歉,我现在添加了一些有问题的解释。