如何比较两个字符串的含义?

问题描述 投票:-1回答:1

我希望我的node.js应用程序的用户写下想法,然后将其存储在数据库中。到目前为止,一切都很好,但是我不希望该表中有多余的条目,所以我决定使用此表来检查相似性:https://www.npmjs.com/package/string-similarity-js

您知道一种方式,可以通过含义比较两个字符串吗?就像在“使用公共交通工具”与“乘火车驾驶”中获得较高的相似性得分一样,后者在上述方面的表现非常差。

javascript node.js nlp ibm-watson
1个回答
0
投票

比较两个字符串的含义仍在进行中。如果您确实想解决问题,则应考虑获得博士学位。

[我找到了这个Github存储库,该存储库实现了Google的BERT模式,并用它来嵌入两个句子。从理论上讲,如果嵌入相似,则两个句子具有相同的含义。https://github.com/UKPLab/sentence-transformers

# the following is simplified from their README.md
embedder = SentenceTransformer('bert-base-nli-mean-tokens')

# Corpus with example sentences
S1 = ['A man is eating a food.']
S2 = ['A man is eating pasta.']

s1_embedding = embedder.encode(S1)
s2_embedding = embedder.encode(S2)

dist = scipy.spatial.distance.cdist([s1_embedding], [s2_embedding], "cosine")[0]
Example output (copied from their README.md)

Query: A man is eating pasta.
Top 5 most similar sentences in corpus:
A man is eating a piece of bread. (Score: 0.8518)
A man is eating a food. (Score: 0.8020)
A monkey is playing drums. (Score: 0.4167)
A man is riding a horse. (Score: 0.2621)
A man is riding a white horse on an enclosed ground. (Score: 0.2379)
© www.soinside.com 2019 - 2024. All rights reserved.