Search code examples
nlpcluster-analysisgensimsimilarity

Text representations : How to differentiate between strings of similar topic but opposite polarities?


I have been doing clustering of a certain corpus, and obtaining results that group sentences together by obtaining their tf-idf, checking similarity weights > a certain threshold value from the gensim model.

tfidf_dic = DocSim.get_tf_idf()
ds = DocSim(model,stopwords=stopwords, tfidf_dict=tfidf_dic)
sim_scores = ds.calculate_similarity(source_doc, target_docs)

The problem is that despite putting high threshold values, sentences of similar topics but opposite polarities get clustered together as such:

cluster results.

Here is an example of the similarity weights obtained between "don't like it" & "i like it"

similarity results. Are there any other methods, libraries or alternative models that can differentiate the polarities effectively by assigning them very low similarities or opposite vectors?

This is so that the outputs "i like it" and "dont like it" are in separate clusters.

PS: Pardon me if there are any conceptual errors as I am rather new to NLP. Thank you in advance!


Solution

  • The problem is in how you represent your documents. Tf-idf is good for representing long documents where keywords play a more important role. Here, it is probably the idf part of tf-idf that disregards the polarity because negative particles like "no" or "not" will appear in most documents and they will always receive a low weight.

    I would recommend trying some neural embeddings that might capture the polarity. If you want to keep using Gensim, you can try doc2vec but you would need quite a lot of training data for that. If you don't have much data to estimate the representation, I would use some pre-trained embeddings.

    Even averaging word embeddings (you can load FastText embeddings in Gensim). Alternatively, if you want a stronger model, you can try BERT or another large pre-trained model from the Transformers package.