如何在Python中使用gensim进行字符串语义匹配?
在Python中,我们如何确定一个字符串与我们的短语是否有语义关系?
示例:
我们的短语是:
'Fruit and Vegetables'
我们想要检查语义关系的字符串是:
'I have an apple in my basket', 'I have a car in my house'
结果:
正如我们所知的第一项I have an apple in my篮子
与我们的短语有关。
how can we determine whether a string has a semantical relation with our phrase or not in python?
Example:
our phrase is:
'Fruit and Vegetables'
and the strings we want to check semantical relation in, are:
'I have an apple in my basket', 'I have a car in my house'
result:
as we know the first item I have an apple in my basket
has a relation to our phrase.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 gensim 库来实现 MatchSemantic 并将这样的代码编写为函数(在此处查看完整代码):
初始化
gensim
和numpy
:代码
注意:
如果第一次运行代码,进程栏将从
0%
变为100%
以下载glove-wiki-gigaword-50
gensim ,之后一切都将被设置,您可以简单地运行代码。例如,用法
中的任何句子或项目匹配,
,我们想要查看
Fruit and Vegetables
是否与documents
Test:因此我们知道第一个项目
我的篮子里有一个苹果
与水果和蔬菜
有语义关系,因此它的分数将为0.189
,而第二个项目则没有关系会被发现,所以它的分数将是0
输出:
You can use
gensim
library to implementMatchSemantic
and write code like this as a function (see full code in here):Initialization
gensim
andnumpy
:Code
Attention:
if run the code for the first time a process bar will go from
0%
to100%
for downloadingglove-wiki-gigaword-50
of thegensim
and after that everything will be set and you can simply run the code.Usage
for example, we want to see if
Fruit and Vegetables
matches any of the sentences or items insidedocuments
Test:
so we know that the first item
I have an apple on my basket
has a semantical relation withFruit and Vegetables
so its score will be0.189
and for the second item no relation will be found so its score will be0
output: