如何找到单词之间的“连接”以对句子进行聚类

我需要连接单词4Gand mobile phonesorInternet以便将有关技术的句子聚集在一起。我有以下句子:


4G is the fourth generation of broadband network.

4G is slow. 

4G is defined as the fourth generation of mobile technology

I bought a new mobile phone. 

我需要在同一簇中考虑上述句子。目前还没有,可能是因为它没有找到 4G 和移动之间的关系。我尝试使用firstwordnet.synsets来查找连接4G到互联网或手机的同义词,但不幸的是它没有找到任何连接。将我正在做的句子聚类如下:


rom sklearn.feature_extraction.text import TfidfVectorizer

from sklearn.cluster import KMeans

from sklearn.metrics import adjusted_rand_score

import numpy


texts = ["4G is the fourth generation of broadband network.",

    "4G is slow.",

    "4G is defined as the fourth generation of mobile technology",

    "I bought a new mobile phone."]


# vectoization of the sentences

vectorizer = TfidfVectorizer(stop_words="english")

X = vectorizer.fit_transform(texts)

words = vectorizer.get_feature_names()

print("words", words)



n_clusters=3

number_of_seeds_to_try=10

max_iter = 300

number_of_process=2 # seads are distributed

model = KMeans(n_clusters=n_clusters, max_iter=max_iter, n_init=number_of_seeds_to_try, n_jobs=number_of_process).fit(X)


labels = model.labels_

# indices of preferible words in each cluster

ordered_words = model.cluster_centers_.argsort()[:, ::-1]


print("centers:", model.cluster_centers_)

print("labels", labels)

print("intertia:", model.inertia_)


texts_per_cluster = numpy.zeros(n_clusters)

for i_cluster in range(n_clusters):

    for label in labels:

        if label==i_cluster:

            texts_per_cluster[i_cluster] +=1 


print("Top words per cluster:")

for i_cluster in range(n_clusters):

    print("Cluster:", i_cluster, "texts:", int(texts_per_cluster[i_cluster])),

    for term in ordered_words[i_cluster, :10]:

        print("\t"+words[term])


print("\n")

print("Prediction")


任何对此的帮助将不胜感激。


浮云间
浏览 124回答 1
1回答

喵喵时光机

密集的词嵌入(如来自 word2vec 或类似算法)可能会有所帮助。他们会将单词转换为密集的高维向量,其中具有相似含义/用法的单词彼此接近。甚至:空间中的某些方向通常会与单词之间的关系类型大致相关。因此,在具有足够代表性(大且多样化)的文本上训练的词向量将使向量彼此'4G'接近'mobile',然后如果您的句子表示是从词向量引导的,这可能有助于您的聚类。使用单个词向量对句子建模的一种快速方法是使用所有句子词向量的平均值作为句子向量。这太简单了,无法对多种意义进行建模(尤其是那些来自语法和词序的意义),但通常可以作为一个很好的基线,特别是对于广泛话题的问题。另一种计算“Word Mover's Distance”将句子视为词向量集(不对其进行平均),并且可以进行句子到句子的距离计算,其效果比简单的平均值更好,但计算时间较长会变得非常昂贵句子。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python