【问题标题】:Text similarity using WMD within the same time period同一时间段内使用 WMD 的文本相似度
【发布时间】:2021-02-14 02:00:39
【问题描述】:

我有一个数据集

       Title                                                Year
0   Sport, there will be a match between United and Tottenham ...   2020
1   Forecasting says that it will be cold next week                 2019
2   Sport, Mourinho is approaching the anniversary at Tottenham     2020
3   Sport, Tottenham are sixth favourites for the title behind Arsenal. 2020
4   Pochettino says clear-out of fringe players at Tottenham is inevitable.     2018
... ... ...

我想研究同一年内的文本相似度,而不是在整个数据集中。为了找到最相似的文本,我使用了 WM 距离相似度。 对于两个文本将是:

word2vec_model = gensim.models.KeyedVectors.load_word2vec_format('GoogleNews-vectors-negative300.bin', binary=True)
distance = word2vec_model.wmdistance("string 1".split(), "string 2".split())

但是,我需要在同一年遍历句子的距离,以获得每个文本与其他文本的相似性,从而在数据框中的每行创建一个相似文本列表。 能否请您告诉我如何在同一年发表的文本中迭代 wmdistance 函数,以便为每个文本获取同一时期内最相似的文本?

【问题讨论】:

    标签: python pandas gensim word2vec similarity


    【解决方案1】:

    为每个组生成一个距离矩阵,然后选择最小值应该可以工作。这将为您提供给定年份中最近的单个文档索引。如果您想要 n 个文档或其他类似的东西,您应该能够很容易地修改此代码。

    from scipy.spatial.distance import pdist, squareform
    
    def nearest_doc(group):
        sq = squareform(pdist(group.to_numpy()[:,None], metric=lambda x, y:word2vec_model.wmdistance(x[0], y[0])))
    
        return group.index.to_numpy()[np.argmin(np.where(sq==0, np.inf, sq), axis=1)]
    
    df['nearest_doc'] = df.groupby('Year')['Title'].transform(nearest_doc)
    

    结果:

    Title   Year    nearest_doc
    0   Sport, there will be a match between United an...   2020    3
    1   Forecasting says that it will be cold next week     2019    1
    2   Sport, Mourinho is approaching the anniversary...   2020    3
    3   Sport, Tottenham are sixth favourites for the ...   2020    2
    4   Pochettino says clear-out of fringe players at...   2018    4
    

    【讨论】:

      猜你喜欢
      • 2021-04-27
      • 2014-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-21
      • 2011-01-07
      • 2019-10-14
      • 2018-02-19
      相关资源
      最近更新 更多