您的问题有点含糊,但这里有两个想法可能会有所帮助:
1。词网
WordNet 是一个词汇数据库,提供同义词、分类以及在某种程度上提供英语单词的“语义”。这是探索数据库的web interface。这是via 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')]
2。零样本分类
HuggingFaceTransformers 和零样本分类:您还可以使用预训练的深度学习模型对文本进行分类。在这种情况下,您需要为您在文本中寻找的所有可能的不同含义手动创建标签。例如:[“问候”、“侮辱”、“祝贺”]。
然后,您可以使用深度学习模型来预测哪个标签(广义上的“语义”)最适合您的文本。
示例:
# 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 application,可以查看库在没有编码的情况下做了什么。这是一个Jupyter notebook,它演示了如何在 Python 中使用它。您可以从笔记本中复制粘贴代码。