Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/org/nlp/vec/VectorModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,63 @@ public static VectorModel loadFromFile(String path){

}

/**
* Load file from classpath
* @param path
* @return
*/
public static VectorModel loadFromClasspath(String path) {

if (path == null || path.isEmpty()){
throw new IllegalArgumentException("模型路径可以为null或空。");
}

DataInputStream dis = null;
int wordCount, layerSizeLoaded = 0;
Map<String, float[]> wordMapLoaded = new HashMap<String, float[]>();
InputStream is = VectorModel.class.getResourceAsStream(path);
try {
dis = new DataInputStream(is);
wordCount = dis.readInt();
layerSizeLoaded = dis.readInt();
float vector;

String key;
float[] value;
for (int i = 0; i < wordCount; i++) {
key = dis.readUTF();
value = new float[layerSizeLoaded];
double len = 0;
for (int j = 0; j < layerSizeLoaded; j++) {
vector = dis.readFloat();
len += vector * vector;
value[j] = vector;
}

len = Math.sqrt(len);

for (int j = 0; j < layerSizeLoaded; j++) {
value[j] /= len;
}
wordMapLoaded.put(key, value);
}

} catch (IOException ioe){
ioe.printStackTrace();
}finally {
try {
if (dis != null){
dis.close();
}
} catch (IOException ioe){
ioe.printStackTrace();
}
}

return new VectorModel(wordMapLoaded, layerSizeLoaded);

}

/**
* 保存词向量模型
* @param file 模型存放路径
Expand Down