Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct JFastText.getLabels() #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/main/java/com/github/jfasttext/JFastText.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public int getNLabels() {
}

public List<String> getLabels() {
return stringVec2Strings(fta.getWords());
return stringVec2Strings(fta.getLabels());
}

public double getLr() {
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/com/github/jfasttext/JFastTextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class JFastTextTest {
Expand Down Expand Up @@ -181,4 +184,21 @@ public void test10ModelFromURL() throws Exception {
JFastText jft = new JFastText(modelUrl);
System.out.printf("\tnumber of words = %d\n", jft.getNWords());
}

@Test
public void test11GetLabels() throws Exception {
String modelFile = "src/test/resources/models/supervised.model.bin";
URL modelUrl = new File(modelFile).toURI().toURL();
assertNotNull(String.format("Failed to locate model '%s'", modelFile), modelUrl);
JFastText jft = new JFastText(modelUrl);

assertEquals(2, jft.getNLabels());
System.out.printf("\tnumber of labels = %d\n", jft.getNLabels());
final String prefix = jft.getLabelPrefix();
assertEquals("__label__", prefix);

final Set<String> labels = jft.getLabels().stream().collect(Collectors.toSet());
assertTrue("Expected label not found:" + prefix + "soccer", labels.contains(prefix + "soccer"));
assertTrue("Expected label not found:" + prefix + "football", labels.contains(prefix + "football"));
}
}