I have the following code:
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.cluster import KMeans
import pandas as pd
sentences = ["I have the ability", "I have the weakness", "I have the capability", "I have the power"]
tfidf = TfidfVectorizer(max_features=300)
tfidf.fit(sentences)
X = tfidf.transform(sentences)
k = 2
model = KMeans(n_clusters=k, random_state=1)
model.fit(X)
print(pd.DataFrame(columns=["sentence"], data=sentences).join(pd.DataFrame(columns=["cluster"], data=model.labels_)))
The output looks like this:
index | sentence | cluster |
---|---|---|
0 | I have the ability | 0 |
1 | I have the weakness | 0 |
2 | I have the capability | 0 |
3 | I have the power | 1 |
As you can see "I have the ability", "I have the weakness", "I have the capability" were grouped in the same cluster (cluster 0) and "I have the power" was grouped into a separate cluster. I think they were grouped randomly and it can't tell which sentences actually mean the same thing. I want a way to be able to group "I have the ability", "I have the capability", and "I have the power" together by specifying that ability, capability and power are synonyms. So basically mapping all words to their synonyms. Is there an existing package for this?