File size: 2,018 Bytes
45ca953 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import fasttext
import jieba
train_data_path = 'sel.txt'
parse_data_path = 'parse.txt'
model_path = 'word_vectors.bin'
def train():
#jieba cut
with open(train_data_path, 'r', encoding='utf-8') as f:
with open(parse_data_path, 'w', encoding='utf-8') as f1:
lines=f.readlines()
for i,line in enumerate(lines):
print(i,len(lines))
words = jieba.cut(line)
f1.write(" ".join(words) + "\n")
model = fasttext.train_unsupervised( parse_data_path, model='skipgram', dim=300, epoch=10, lr=0.1)
model.save_model(model_path)
#train()
model = fasttext.load_model(model_path)
#targets = ['阴茎', '乳房','阴道', '肛门', '屁股','腿','脚','脚趾','手','手指','手臂','头','嘴巴','眼睛','鼻子','耳朵','脸','脖子','胸','腹','腰','背','舌头','口腔','肩膀']
#targets = ['身体','抓住','抚摸','呻吟','抽插','抱','分开','搂住','揉捏','亲吻','躺下','舔']
#targets = ['没入','压','淫叫','插','吐','吞','挠','翘','吸','闻','握','伸入','捧起']
#targets = ['咬','吮','含','舔','抬','跪','拉','推','蹲','爬','站','趴','坐','拔']
#targets = ['爸爸','妈妈','儿子','女儿','老公','老婆','姐姐','妹妹','哥哥','弟弟','阿姨','舅妈','舅舅','叔叔','姑姑','姑父','姨妈','姨父','婶婶','婶父','嫂子','妹夫','姐夫']
#targets = ['阴唇','阴囊','输精管']
targets = ['精液','爱液']
for target in targets:
print(target,end=' ')
for _,i in model.get_nearest_neighbors(target, k=20):
print(i,end=' ')
print()
#print(model.words)
texts_to_cluster = ["飞机", "肉棒", "汽车", "阴茎", "猫", "B", "老婆"]
vectors = [model.get_sentence_vector(text) for text in texts_to_cluster]
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=3)
clusters = kmeans.fit_predict(vectors)
#for i, text in enumerate(texts_to_cluster):
# print(f"文本 '{text}' 属于类别 {clusters[i]}") |