【问题标题】:List all sentences containing specific word and similar words with word2vec使用 word2vec 列出所有包含特定单词和相似单词的句子
【发布时间】:2020-10-31 13:21:34
【问题描述】:

我有一张如下表:

data = {'text':  ['The scent is nice','I like the smell', 'The smell is awesome', 'I find the scent amazing', 'I love the smell']}

df = pd.DataFrame (data, columns = ['text'])

我想列出所有包含“气味”这个词的句子

word = 'smell'
selected_list = []
for i in range(0, len(df)):
    if word in df.iloc[i,0]:
        selected_list.append(df.iloc[i,0])
selected_list

我得到的输出是:

['I like the smell', 'The smell is awesome', 'I love the smell']

但是,我还想列出包含与“气味”相似的单词的句子,例如“气味”,并且我想使用谷歌的预训练 word2vec 并设置一个条件,如果相似度高于 0.5 到也列出句子。因此,期望的输出是:

['The scent is nice', 'I like the smell', 'The smell is awesome', 'I find the scent amazing','I love the smell']

如何将 word2vec 添加到上述代码中,以便它不仅可以扫描"smell",还可以扫描所有相似的单词?

【问题讨论】:

  • 你可以创建可能的令牌,df.loc[df.text.str.contains("smell|scent"), "text"].tolist()
  • 您是否已经训练了 word2vec 模型,或者决定重用其他地方的词向量?
  • @gojomo 我正在使用 Google 的预训练 word2vec 模型

标签: python pandas nlp word2vec


【解决方案1】:

听起来您需要将候选文本中的每个字词与查询字词进行比较,然后查看是否有一个(或多个)最相似的字词超过了您的阈值.

这需要将原始文本标记为适合根据您的词向量集查找的词,然后比较/排序结果,然后根据您的阈值检查它们。

您需要做的核心可能是以下函数,依赖于 Python 库 gensim 中的词向量支持:

def rank_by_similarity(target_word, list_of_words, word_vectors):
    """Return ranked list of (similarity_score, word) of words in
    list_of_words, by similarity to target_word, using the set of
    vectors in word_vectors (a gensim.models.KeyedVectors instance)."""

    sim_pairs = [(word_vectors.similarity(target_word, word), word) 
                 for word in list_of_words]
    sim_pairs.sort(reverse=True)  # put largest similarities first
    return sim_pairs

在上下文中使用:

from gensim.models import KeyedVectors

all_sentences = [
    'That looks nice',
    'The scent is nice',
    'It tastes great', 
    'I like the smell',
    'Wow that\'s hot',
]
query_word = 'smell'
threshold = 0.5

goog_vecs = KeyedVectors.load_word2vec_format('GoogleNews-vectors-negative300.bin', binary=True)

selected_sentences = []
for sentence in all_sentences:
    tokens = sentence.split()
    ranked_tokens = rank_by_similarity(query_word, tokens, goog_vecs)
    if ranked_tokens[0][0] > threshold:  # if top match similarity > threshold...
        selected_sentences.append(sentence)  # ...consider it a match
print(selected_sentences)

【讨论】:

    猜你喜欢
    • 2013-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-02
    • 1970-01-01
    相关资源
    最近更新 更多