Skip to content
Closed
Changes from 1 commit
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
21 changes: 11 additions & 10 deletions src/main/java/com/medallia/word2vec/Word2VecModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.primitives.Bytes;
import com.google.common.primitives.Doubles;
import com.medallia.word2vec.thrift.Word2VecModelThrift;
import com.medallia.word2vec.util.Common;
Expand Down Expand Up @@ -137,13 +138,13 @@ public static Word2VecModel fromBinFile(File file, ByteOrder byteOrder, Profilin
// every gigabyte. To calculate offsets correctly, we have to keep track how many gigabytes
// we've already skipped. That's what this is for.

StringBuilder sb = new StringBuilder();
char c = (char) buffer.get();
List<Byte> list = new ArrayList<Byte>();
byte c = buffer.get();
while (c != '\n') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are using a hard coded line separator here. Do you have a specific reason not to use System.lineSeparator() instead?

sb.append(c);
c = (char) buffer.get();
list.add(c);
c = buffer.get();
}
String firstLine = sb.toString();
String firstLine = new String(Bytes.toArray(list));
int index = firstLine.indexOf(' ');
Preconditions.checkState(index != -1,
"Expected a space in the first line of file '%s': '%s'",
Expand All @@ -163,17 +164,17 @@ public static Word2VecModel fromBinFile(File file, ByteOrder byteOrder, Profilin
final float[] floats = new float[layerSize];
for (int lineno = 0; lineno < vocabSize; lineno++) {
// read vocab
sb.setLength(0);
c = (char) buffer.get();
list.clear();
c = buffer.get();
while (c != ' ') {
// ignore newlines in front of words (some binary files have newline,
// some don't)
if (c != '\n') {
sb.append(c);
list.add(c);
}
c = (char) buffer.get();
c = buffer.get();
}
vocabs.add(sb.toString());
vocabs.add(new String(Bytes.toArray(list)));

// read vector
final FloatBuffer floatBuffer = buffer.asFloatBuffer();
Expand Down