Search in sources :

Example 1 with LabelAwareListSentenceIterator

use of org.deeplearning4j.text.sentenceiterator.labelaware.LabelAwareListSentenceIterator in project deeplearning4j by deeplearning4j.

the class SentenceIteratorTest method testLabelAware.

@Test
public void testLabelAware() throws Exception {
    String s = "1; hello";
    ByteArrayInputStream bis = new ByteArrayInputStream(s.getBytes());
    LabelAwareSentenceIterator labelAwareSentenceIterator = new LabelAwareListSentenceIterator(bis, ";", 0, 1);
    assertTrue(labelAwareSentenceIterator.hasNext());
    labelAwareSentenceIterator.nextSentence();
    assertEquals("1", labelAwareSentenceIterator.currentLabel());
    InputStream is2 = new ClassPathResource("labelawaresentenceiterator.txt").getInputStream();
    LabelAwareSentenceIterator labelAwareSentenceIterator2 = new LabelAwareListSentenceIterator(is2, ";", 0, 1);
    int count = 0;
    Map<Integer, String> labels = new HashMap<>();
    while (labelAwareSentenceIterator2.hasNext()) {
        String sentence = labelAwareSentenceIterator2.nextSentence();
        labels.put(count, labelAwareSentenceIterator2.currentLabel());
        count++;
    }
    assertEquals("SENT37", labels.get(0));
    assertEquals("SENT38", labels.get(1));
    assertEquals("SENT39", labels.get(2));
    assertEquals("SENT42", labels.get(3));
    assertEquals(4, count);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) LabelAwareSentenceIterator(org.deeplearning4j.text.sentenceiterator.labelaware.LabelAwareSentenceIterator) LabelAwareListSentenceIterator(org.deeplearning4j.text.sentenceiterator.labelaware.LabelAwareListSentenceIterator) ClassPathResource(org.datavec.api.util.ClassPathResource) Test(org.junit.Test)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 ClassPathResource (org.datavec.api.util.ClassPathResource)1 LabelAwareListSentenceIterator (org.deeplearning4j.text.sentenceiterator.labelaware.LabelAwareListSentenceIterator)1 LabelAwareSentenceIterator (org.deeplearning4j.text.sentenceiterator.labelaware.LabelAwareSentenceIterator)1 Test (org.junit.Test)1