Skip to main content
 首页 » 编程设计

python之更快地计算字符串中的短语数

2025年05月04日51luoye11

我有以下代码计算字符串中所需的短语:

from nltk.util import ngrams 
from nltk import word_tokenize 
import pandas as pd 
 
def count_words(convo,df_search): 
    for i in range(0,len(df_search)): 
        word = df_search['word'][i] #set the word 
        a=tuple(word.split(' ')) 
        print word, len([i for i in ngrams(word_tokenize(convo),n=len(a)) if i==a])  
 
convo="I see a tall tree outside. A man is under the tall tree. Actually, there are more than one man under the tall tree" 
 
df_search=pd.DataFrame({'word':['man','tall tree','is under the']}) 
 
count_words(convo,df_search) 

代码的问题在于它真的很慢,它每次都“重新”ngrams 来寻找新的短语。而且那部分的短语是动态的,所以我不知道长度有多长。需要帮助更改代码以加快速度。

请您参考如下方法:

如果你不介意使用re:

import re 
input_string = "I see a tall tree outside. A man is under the tall tree. Actually, there are more than one man under the tall tree" 
word = ['man','tall tree','is under the'] 
for i in word: 
    print i + ': ' + str(sum(1 for _ in re.finditer(r'\b%s\b' % re.escape(i), input_string)))