[英]How to get the se semantic meaning of a word/phrase
大家好,我需要帮助,目前我正在做一个项目,我必须找到单词/短语的语义。 例如你好,你好,早上好应该回复问候等......有什么建议吗? 提前致谢
你的问题有点含糊,但这里有两个想法可能会有所帮助:
WordNet是一个词汇数据库,提供同义词、分类以及在某种程度上提供英语单词的“语义意义”。 这是浏览数据库的Web 界面。 以下是如何通过 NLTK使用它。
例子:
from nltk.corpus import wordnet as wn
# get all possible meanings of a word. e.g. "welcome" has two possible meanings as a noun, three meanings as a verb and one meaning as an adjective
wn.synsets('welcome')
# output: [Synset('welcome.n.01'), Synset('welcome.n.02'), Synset('welcome.v.01'), Synset('welcome.v.02'), Synset('welcome.v.03'), Synset('welcome.a.01')]
# get the definition of one of these meanings:
wn.synset('welcome.n.02').definition()
# output: 'a greeting or reception'
# get the hypernym of the specific meaning, i.e. the more abstract category it belongs to
wn.synset('welcome.n.02').hypernyms()
# output: [Synset('greeting.n.01')]
HuggingFace Transformers 和零样本分类:您还可以使用预训练的深度学习模型对文本进行分类。 在这种情况下,您需要为您在文本中寻找的所有可能的不同含义手动创建标签。 例如:[“问候”、“侮辱”、“祝贺”]。 然后您可以使用深度学习模型来预测哪个标签(广义上讲“语义”)最适合您的文本。
例子:
# pip install transformers==3.1.0 # pip install in terminal
from transformers import pipeline
classifier = pipeline("zero-shot-classification")
sequence = "Hi, I welcome you to this event"
candidate_labels = ["greeting", "insult", "congratulation"]
classifier(sequence, candidate_labels)
# output: {'sequence': 'Hi, I welcome you to this event',
# 'labels': ['greeting', 'congratulation', 'insult'],
# 'scores': [0.9001138210296631, 0.09858417510986328, 0.001302019809372723]}
=> 你的每个标签都会得到一个分数,得分最高的标签将是你的文本的“语义意义”。
这是一个交互式Web 应用程序,可查看该库无需编码即可执行的操作。 这是一个Jupyter notebook ,它演示了如何在 Python 中使用它。 您只需从笔记本中复制粘贴代码即可。
您还没有表现出任何努力来编写自己的代码,但这里有一个小例子。
words = ['hello','hi','good morning']
x = input('Word here: ')
if x.lower() in words:
print('Regards')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.