I'd like to perform a textual/sentiment analysis.

I was able to analyse samples with 3 labels: (positive, neutral, negative) and I used algorithms such as SVM, Random Forest, Logistic Regression and Gradient Boosting.

My script works correctly and with the cross validation I can take the best algorithm among the 4.

I use supervised algorithms with the python function "Countvectorizer"

But my boss typed "NLP" on the internet and looked at some articles.

He told me : "These 3 outputs are not enough, I want a complete semantic analysis that can explain the global meaning of the sentence"

He didn't seem to have a preference between supervised and unsupervised algorithms.

He told me that he wanted an algorithm able to tell that "The company president is behind bars" is equivalent to "the CEO is in jail".

So do you have any idea how one could perform that ? And how to implement it in Python? I guess we need a great database full of words, I know this is not a very specific question but I'd like to present him all the solutions.

What scares me is that he don't seem to know a lot about it, for example he told me "you have to reduce the high dimension of your dataset" , while my dataset is just 2000 text fields.

Thank you very much for your answers :)


1 Answer 1


With your three labels: positive, neutral or negative - it seems you are talking more about sentiment analysis. This answer the question: what are the emotions of the person who wrote this piece of text?

Semantic analysis is a larger term, meaning to analyse the meaning contained within text, not just the sentiment. It looks for relationships among the words, how they are combined and how often certain words appear together.

To gain a deeper insight into your text, you could read about topics such as:

  • Semantic Analysis in general might refer to your starting point, where you parse a sentence to understand and label the various parts of speech (POS). A tool for this in Python is spaCy, which words very nicely and also provides visualisations to show to your boss.
  • Named Entity Recognition (NER) - finding parts of speech (POS) that refer to an entity and linking them to pronouns appearing later in the text. An example is to distinguish between Apple the company, and apple the fruit.
  • Embeddings - finding latent representation of individual words e.g. using Word2Vec. Text is processed to produce a single embedding for individual words in the form of an n-dimensional vector. You can then compute similarity measures (e.g. cosine similarity) between the vectors for certain words to analyse how they are related.
  • Lemmatisation - this method reduces many forms of words to their base forms, which means they appear more regularly and we don't consider e.g. verb conjugations as separate words. As an example, tracking, tracked, tracker, might all be reduced to the base form: track.

Your next step could be to search for blogs and introductions to any of those terms I mentioned.

Here is an example parse-tree from spaCy:

example parse tree

Reducing dimensions

This is something that would then refer to the vectors, which describe each of your words. Generally, the Word2Vec vectors are something like 300-dimensional. You might want to visualise the words, plotting them in 2d space. You can try a method like t-SNE, which will map the 300d vectors to 2d space, allowing nice plots showing relationships, while retaining as much of the original relationships described in the 300d space. There will, of couse, be some information loss, but you could not have visualised the 300d vectors in the first place!

Using the vectors for your words, you can compute things like the similarity (on a scale between 0 and 1) between president and CEO is something like 0.92 - meaning they are almost synonyms!

  • $\begingroup$ Thank you very much for your exhaustive answer n1k31t4 :D $\endgroup$
    – GG24
    Aug 16, 2018 at 14:54
  • $\begingroup$ @GG24 - you're welcome! Feel free to up vote and (if it answered your question) accept the answer! :-) $\endgroup$
    – n1k31t4
    Aug 16, 2018 at 15:03
  • $\begingroup$ It's done :) , And I already vote for the answer yesterday but he told me than I had less than 15 in reputation and that my vote was not public because of that $\endgroup$
    – GG24
    Aug 17, 2018 at 7:35
  • $\begingroup$ @GG24 - interesting, I didn't know that rule! +1 to your question :) $\endgroup$
    – n1k31t4
    Aug 17, 2018 at 8:27

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.