# How to extract features from long chemical names?

I have an interesting problem that I am uncertain about how to even get started. I am working on a binary classifier that will take a chemical name, encoded as a string, and predict whether it is a 'good' or 'bad' name. I have had quite good success doing this by examining the structure of the chemical directly, but I would like to explore whether I can learn anything about the given name of the chemical itself (as the name can contain some structural information about the molecule that my encoding of the molecular structure is missing). I have been searching around trying to find anything built into sklearn to do text feature extraction. There is quite a bit, but it mostly seems to me like they are used for encoding whole sentences or paragraphs. My input would be very long, single words such as:

1-(aminoiminomethyl)-N'-[2,3,6-tri-O-benzoyl-4-O-(2,3,4,6-tetra-O-benzoyl-α-D-glucopyranosyl)-β-D-glucopyranosyl]-

octahydro-7-hydroxy-1-[[2-O-(4-hydroxybenzoyl)-α-D-allopyranosyl]oxy]-7-methyl-

And as such, I'm not certain a bag of words or one-hot encoding of the strings would work. Could anyone perhaps point me in the right direction on methodologies or algorithms that might possibly be able to extract features from these strings such that I could train a binary classifier on them?

You can train an RNN with character embeddings. This can be done by splitting the name into sequences of chars and vectorize them numerically. If you are working with Keras, you can feed them into an Embedding() layer that will learn how to represent characters. RNN layers will then process their sequence. At the output node, your Network will perform a classification ('good'/'bad').

• Thank you, I don't have much experience with Keras, but I am going to see if I can figure out an approximation with it and go through there using your advice. Jan 23, 2020 at 18:25
• alternatively, if there are some recurrent patterns in chemical names (as I suppose), you might write a script that operationalizes some variable. In this way, you can avoid using embeddings (long and computationally expensive to train) and use some other staistical or ML algorithm. Unfortunately I have no background in chemistry, and I can't help you in this specific domain. Jan 23, 2020 at 19:32
• Right, that is a very good method that has been attempted before at my organization, but with a database containing hundreds of millions of chemical names, it becomes difficult to identify enough of those patterns to write a script that would output a meaningful vector. One first approach I'm going to attempt (before 'deep' diving ;), into keras, is a character ngram vectorization of the name, followed by training a random forest binary classifier on that vector. Not super optimistic it will work, but again, first attempt Jan 23, 2020 at 20:03

The process of dividing a piece into small units is called tokenization.

Most tokenization systems are combinations of hard-coded rules (e.g., string methods or regular expression) and learned rules (e.g., machine learning). Many tokenizations can be solved with hard-coded rules.

Then the tokens can be one-hot encoded.

Here is some code to get started:

import re

words = ["1-(aminoiminomethyl)-N'-[2,3,6-tri-O-benzoyl-4-O-(2,3,4,6-tetra-O-benzoyl-α-D-glucopyranosyl)-β-D-glucopyranosyl]-",