From 3c214ec2a68f51fc88420d76c37774592befc11d Mon Sep 17 00:00:00 2001 From: hankcs Date: Sun, 20 Oct 2019 11:50:51 -0400 Subject: [PATCH] =?UTF-8?q?WordVectorModel=E6=94=AF=E6=8C=81=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89Map=E7=B1=BB=E5=9E=8B=EF=BC=9Ahttps://github.?= =?UTF-8?q?com/hankcs/HanLP/issues/1304?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mining/word2vec/WordVectorModel.java | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/hankcs/hanlp/mining/word2vec/WordVectorModel.java b/src/main/java/com/hankcs/hanlp/mining/word2vec/WordVectorModel.java index 7c1925f5b..e15da6182 100644 --- a/src/main/java/com/hankcs/hanlp/mining/word2vec/WordVectorModel.java +++ b/src/main/java/com/hankcs/hanlp/mining/word2vec/WordVectorModel.java @@ -29,19 +29,30 @@ public class WordVectorModel extends AbstractVectorModel */ public WordVectorModel(String modelFileName) throws IOException { - super(loadVectorMap(modelFileName)); + this(modelFileName, new TreeMap()); } - private static TreeMap loadVectorMap(String modelFileName) throws IOException + /** + * 加载模型 + * + * @param modelFileName 模型路径 + * @param storage 一个空白的Map(HashMap等) + * @throws IOException 加载错误 + */ + public WordVectorModel(String modelFileName, Map storage) throws IOException + { + super(loadVectorMap(modelFileName, storage)); + } + + private static Map loadVectorMap(String modelFileName, Map storage) throws IOException { VectorsReader reader = new VectorsReader(modelFileName); reader.readVectorFile(); - TreeMap map = new TreeMap(); for (int i = 0; i < reader.vocab.length; i++) { - map.put(reader.vocab[i], new Vector(reader.matrix[i])); + storage.put(reader.vocab[i], new Vector(reader.matrix[i])); } - return map; + return storage; } /**