# Entity recognition with context/relation

Is there a way to get a specific entity based on the context where it is found? For example:

The temperature today is 35°C.

Store risperidone tablet at 20°C.

Both are talking about temperature. For the first sentence I would want the temperature to be "WeatherTemperature" entity. The second sentence I would want the temperature to be "DrugTemperature". What model could I use to train for this behavior?

• This is related to the tasks of topic modeling and/or word sense disambiguation. But it completely depends on what kind of data you can use for training. – Erwan Jun 19 '19 at 14:37
• @Erwan How should I first proceed? Is there a particular tool I can use? I have a lots of lines of text that are like this. "Store X at Y°C, Keep X at Y°C, Freeze X at Y°C" and I would like distinct entities for them. – Avinash Prabhakar Jun 19 '19 at 15:43
• If you don't have any annotated data, it's Word Sense Discrimination (WSD): the idea is to clusterize occurrences of "Y°C" based on the context words represented as a vector. There are probably existing tools but I don't have any specific recommendation. – Erwan Jun 19 '19 at 16:07
• @Erwan Thank you! I have some researching to do now. – Avinash Prabhakar Jun 19 '19 at 16:44

With Wolfram Language you may use TextCases or TextContents from the Text Analysis guide. These are both experimental functions so may change a bit before they are finalised in a future version. Both have the TargetDevice option so you can run them on one or more GPUs if you have them.

Reference the Text Content Types guide for the list of entities these functions have been trained on.

I think TextContents is an optimised call to TextCases for multiple cases so I will only use TextContents below.

Use TextContents to locate all trained entities in your sentences.

res1 = TextContents["The temperature today is 35°C.", TargetDevice -> "GPU"]


res2 = TextContents["Store risperidone tablet at 20°C.", TargetDevice -> "GPU"]


TextContents returns a Dataset object that can be Query'ed for specific cases.

res2[ContainsAll[{"Chemical", "Quantity"}], "Type"]

True


and the quantity units

res2[
SelectFirst[#["Type"] == "Quantity" &],
"String" /* SemanticInterpretation /* QuantityUnit]

"DegreesCelsius"


An Entity of type "Chemical" has a wealth of properties so additional information can be obtained if needed. For example,

res2[
SelectFirst[#["Type"] == "Chemical" &],
"String" /* Interpreter["Chemical"] /* (#["MoleculePlot"] &)]


Hope this helps.

• This looks rather promising. What are you using to do this? I tried the Wolfram Client Library for Python but I can't seem to find any of the things (TextContents, TextCases) I need. Are they still in preview? – Avinash Prabhakar Jun 27 '19 at 21:01
• @AvinashPrabhakar The above is in a Wolfram notebook as you didn't specify a platform. For Wolfram Client for Python you need to download the Free Wolfram Engine here. Then you'll be able to use Wolfram Language in Python. You won't have the dynamic interactivity of a Wolfram notebook but you will have access to all the functions. I have a few recent answers here on SE.DS that demonstrate Wolfram Language in Python. – Edmund Jun 27 '19 at 21:27