我如何在文本示例中查找特定的双字母组 - python?
How can I look for specific bigrams in text example - python?
我想了解一组单词(如 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() 函数处理二元语法,以便上面的最后一个示例有效?
编辑:还有其他想法吗?
行
re.findall('[A-z]+', text)
returns
['order', 'intake', 'is', 'strong', 'for', 'Q'].
因此,字符串 'order intake' 将不会在您的此处匹配:
content = [w for w in text if w.lower() in n_grams]
如果你想让它匹配,你需要从每个 Bigram 中生成一个字符串。
相反,您可能应该使用 this 来查找双字母组。
对于 N-gram,请查看 this 答案。
也许您已经利用了这个选项,但为什么不使用简单的 .count 结合 len:
(example_txt[0].count(n_grams[0]) * len(n_grams[0])) / len(example_txt[0])
或者如果您对计算中的空格不感兴趣,您可以使用以下方法:
(example_txt[0].count(n_grams[0])* len(n_grams[0])) / len(example_txt[0].replace(' ',''))
当然你可以在列表理解中使用它们,这只是为了演示目的
您可以使用 SpaCy Matcher:
import spacy
from spacy.matcher import Matcher
nlp = spacy.load("en_core_web_sm")
matcher = Matcher(nlp.vocab)
# Add match ID "orderintake" with no callback and one pattern
pattern = [{"LOWER": "order"}, {"LOWER": "intake"}]
matcher.add("orderintake", None, pattern)
doc = nlp("order intake is strong for Q4")
matches = matcher(doc)
print(len(matches)) #Number of times the bi-gram appears in text
我想了解一组单词(如 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() 函数处理二元语法,以便上面的最后一个示例有效?
编辑:还有其他想法吗?
行
re.findall('[A-z]+', text)
returns
['order', 'intake', 'is', 'strong', 'for', 'Q'].
因此,字符串 'order intake' 将不会在您的此处匹配:
content = [w for w in text if w.lower() in n_grams]
如果你想让它匹配,你需要从每个 Bigram 中生成一个字符串。
相反,您可能应该使用 this 来查找双字母组。
对于 N-gram,请查看 this 答案。
也许您已经利用了这个选项,但为什么不使用简单的 .count 结合 len:
(example_txt[0].count(n_grams[0]) * len(n_grams[0])) / len(example_txt[0])
或者如果您对计算中的空格不感兴趣,您可以使用以下方法:
(example_txt[0].count(n_grams[0])* len(n_grams[0])) / len(example_txt[0].replace(' ',''))
当然你可以在列表理解中使用它们,这只是为了演示目的
您可以使用 SpaCy Matcher:
import spacy
from spacy.matcher import Matcher
nlp = spacy.load("en_core_web_sm")
matcher = Matcher(nlp.vocab)
# Add match ID "orderintake" with no callback and one pattern
pattern = [{"LOWER": "order"}, {"LOWER": "intake"}]
matcher.add("orderintake", None, pattern)
doc = nlp("order intake is strong for Q4")
matches = matcher(doc)
print(len(matches)) #Number of times the bi-gram appears in text