如何从BoW向量中获取字符串?

时间:2019-03-21 08:02:49

标签: python pandas nlp string-matching

我为名为tech_raw_data['Product lower']的pandas dataframae列生成了BoW。

count_vect = CountVectorizer()
smer_counts = count_vect.fit_transform(tech_raw_data['Product lower'].values.astype('U'))
smer_vocab = count_vect.get_feature_names()

接下来要测试与此BoW向量的字符串相似性,我为数据帧中的列中的一个条目(玩具['ITEM NAME'])创建了BoW。

 toys = pd.read_csv('toy_data.csv', engine='python')
 print('-'*80)
 print(toys['ITEM NAME'].iloc[0])
 print('-'*80)
 inp = [toys['ITEM NAME'].iloc[0]]

 cust_counts = count_vect.transform(inp)
 cust_vocab = count_vect.get_feature_names()

检查相似性:

def similar(a, b):
    return SequenceMatcher(None, a, b).ratio()

for x in cust_counts[0].toarray():
    for y in smer_counts.toarray():
        ratio = similar(x, y)
        #print(ratio)
        if ratio>=0.85:
            should print the string corresponding to BoW y

现在,只要匹配率超过0.85,我就需要在smer_counts数据帧中打印与tech_raw_data['Product lower']对应的字符串。

1 个答案:

答案 0 :(得分:1)

for x in cust_counts[0].toarray():
    for i, y in enumerate(smer_counts.toarray()):
        ratio = similar(x, y)
        #print(ratio)
        if ratio>=0.85:
            print (tech_raw_data.loc[i, 'Product lower'])

枚举smer_counts.toarray()返回的numpy数组,并在ratio>=0.85时使用索引来获取tech_raw_data数据框中的相应文本。

这是有效的,因为保留了len(smer_counts.toarray()) == len(tech_raw_data)以及数据框中的记录顺序。