import org.deeplearning4j.models.embeddings.loader.WordVectorSerializer;
import org.deeplearning4j.models.word2vec.Word2Vec;
import org.nd4j.linalg.api.ndarray.INDArray;
/**
*必须的maven,核心组件、nlp模块、platform
*INDArray相当于python的numpy
<dependency>
<groupId>org.deeplearning4j</groupId>
<artifactId>deeplearning4j-core</artifactId>
<version>1.0.0-beta4</version>
</dependency>
<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-native-platform</artifactId>
<version>1.0.0-beta4</version>
</dependency>
<dependency>
<groupId>org.deeplearning4j</groupId>
<artifactId>deeplearning4j-nlp</artifactId>
<version>1.0.0-beta4</version>
</dependency>
*/
public static void main(String[] args) {
String categoryVecPath = "D:\\.TencentEmbeddingBinary.w2v";
String allWordVecPath = "D:\\.TencentEmbeddingBinary.w2v";
Word2Vec vecModel = WordVectorSerializer.readWord2VecModel(allWordVecPath);
INDArray word = vecModel.getWordVectorMatrix("谢安琪");
System.out.println(word);
//两个词向量相加,INDArray才可以进行向量操作
INDArray addedWord = word.add(vecModel.getWordVectorMatrix("催眠"))
System.out.println(addedWord);
//获取某个词的向量
double[] w = categoryModel.getWordVector("催眠"); //oov会返回null
List<String> cate = (List) categoryModel.wordsNearest(word, 10);
System.out.println("ok");
System.out.println(cate.get(0));
}
来源:CSDN
作者:Eadon999
链接:https://blog.csdn.net/baidu_24536755/article/details/103469472