3
$\begingroup$

I have a database of sentences which are about different topics. I want to automatically classify each sentence with the one or more relevant tags based on the context of the sentence as shown below:

Sentence: The area of a circle is pi time the radius squared

Expected tags: mathematics, geometry

Is there any python library or pre-trained model to generate such tags?

$\endgroup$
1
  • 2
    $\begingroup$ If you're willing to make your own model, you could tag training data based on its source and/or the tags/categories/etc. that that source gives it. $\endgroup$ Jan 18, 2023 at 4:03

4 Answers 4

9
$\begingroup$

To my knowledge, there is no such library or pre-trained model.

Imho there is an important issue in the task as defined in the question, more exactly in the example: these tags seem natural for a human in the sense that they represent the general topics of the sentence. But technically one could find many other tags which are semantically relevant, for example ellipse, surface, calculation, formula, sciences, knowledge, classes, exercise... The correct granularity (the level of specificity/genericity) of the tags is intuitive for a human, not for a machine.

So the task is possible: one can calculate all the semantically more general concepts, for instance with WordNet, but this would often return too many concepts like in my example. A standard method in this case would be too take the top N according to some measure of semantic similarity.

Notes: "classify" is not a good term for this, because classification is a supervised task where classes are known. And it's not really based on the "context" of the sentence ("context of X" usually means "information around X" in NLP), it's based on its content or meaning.

$\endgroup$
12
$\begingroup$

Me: Please give 2 semantic tags for the sentence "The area of a circle is pi time the radius squared"
ChatGPT: 1. Mathematics. 2. Geometry

I'm not sure it's a robust and scalable solution but nevertheless.

$\endgroup$
1
  • 1
    $\begingroup$ You could also use this to generate some "silver standard" training data, for use in training your own model. Then do error analysis of what your model gives you to track down the mistakes that ChatGPT (or whatever) gave you, and iterate. $\endgroup$ Jan 23, 2023 at 19:01
2
$\begingroup$

Extending Solomon Ucko's comment, I would propose trying to label a few thousand examples and fine tune one of the available large language models, for example, the Google's T5 or BERT using your training set. (You will need to have or rent a machine with a GPU for that).

$\endgroup$
2
$\begingroup$

If you don't have training data, "classification" may not be the best name. But it could be if you consider this task as like in zero-shot learning. There are some pretrained models in huggingface for that. You may have to fine tune them with training data to get some actual reliability.

An alternative idea would be to use pre-trained embeddings to calculate vectors for your input and for the tags that you expect to identify. Then you could compute the similarity between the input vector and the tag vectors, and use the similarities to suggest possibly relevant tags.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.