词向量的kmeans词聚类实现

该博客介绍了如何使用Java实现K-means算法进行词向量的聚类。通过加载预训练的词向量模型,对输入的词汇进行处理,初始化聚类中心,迭代更新并找到新的聚类中心,最终实现词汇的自动分类。
摘要由CSDN通过智能技术生成
package com.alg.qid.kmeans;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import com.alg.qid.word2vec.VectorModel;

public class Kmeans {
    
    private int K;
    static int vecsize=300;//向量长度
    
    public static void main(String[] args) throws IOException{
        if(args.length < 1){
            System.err.println("parameter missing");
        }
        Kmeans kmclass = new Kmeans();
        HashSet<String> title2word = new HashSet<String>();
        int k=1000;
        HashMap<String, float[]> title2vec = new HashMap<String, float[]>();
        VectorModel vm = VectorModel.loadFromFile(args[1]);
        System.out.println(vm.getVectorSize());
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream(args[2]), "utf-8"));
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(
                new FileOutputStream(args[2] + ".kmeans"), "utf-8"), true);
        String line = null; 
        int count = 0;
        while ((line = br.readLine()) != null){
            count +=1;
            System.out.println(count);
            String[] words=line.split("\t");
            if (!vm.getWordM
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值