@Test public void testSerialize() throws Exception { InputStream stream = getClass().getResourceAsStream("/opennlp/tools/ngram/ngram-model.xml"); NGramModel ngramModel1 = new NGramModel(stream); stream.close(); Dictionary dictionary = ngramModel1.toDictionary(true); Assert.assertNotNull(dictionary); Assert.assertEquals(14, dictionary.size()); Assert.assertEquals(3, dictionary.getMaxTokenCount()); Assert.assertEquals(1, dictionary.getMinTokenCount()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ngramModel1.serialize(baos); final String serialized = new String(baos.toByteArray(), Charset.defaultCharset()); InputStream inputStream = new ByteArrayInputStream(serialized.getBytes(StandardCharsets.UTF_8)); NGramModel ngramModel2 = new NGramModel(inputStream); stream.close(); Assert.assertEquals(ngramModel2.numberOfGrams(), ngramModel2.numberOfGrams()); Assert.assertEquals(ngramModel2.size(), ngramModel2.size()); dictionary = ngramModel2.toDictionary(true); Assert.assertNotNull(dictionary); Assert.assertEquals(14, dictionary.size()); Assert.assertEquals(3, dictionary.getMaxTokenCount()); Assert.assertEquals(1, dictionary.getMinTokenCount()); }
public void serialize(OutputStream out) throws IOException { Iterator<Entry> entryIterator = new Iterator<Entry>() { private Iterator<StringList> mDictionaryIterator = NGramModel.this.iterator(); @Override public boolean hasNext() { return mDictionaryIterator.hasNext(); } @Override public Entry next() { StringList tokens = mDictionaryIterator.next(); Attributes attributes = new Attributes(); attributes.setValue(COUNT, Integer.toString(getCount(tokens))); return new Entry(tokens, attributes); } @Override public void remove() { throw new UnsupportedOperationException(); } }; DictionaryEntryPersistor.serialize(out, entryIterator, false); }
NGramModel implements Iterable<StringList> { public void serialize(OutputStream out) throws IOException { Iterator<Entry> entryIterator = new Iterator<Entry>() { private Iterator<StringList> mDictionaryIterator = NGramModel.this.iterator(); @Override public boolean hasNext() { return mDictionaryIterator.hasNext(); } @Override public Entry next() { StringList tokens = mDictionaryIterator.next(); Attributes attributes = new Attributes(); attributes.setValue(COUNT, Integer.toString(getCount(tokens))); return new Entry(tokens, attributes); } @Override public void remove() { throw new UnsupportedOperationException(); } }; DictionaryEntryPersistor.serialize(out, entryIterator, false); } }
NGramModel implements Iterable<StringList> { public void serialize(OutputStream out) throws IOException { Iterator<Entry> entryIterator = new Iterator<Entry>() { private Iterator<StringList> mDictionaryIterator = NGramModel.this.iterator(); @Override public boolean hasNext() { return mDictionaryIterator.hasNext(); } @Override public Entry next() { StringList tokens = mDictionaryIterator.next(); Attributes attributes = new Attributes(); attributes.setValue(COUNT, Integer.toString(getCount(tokens))); return new Entry(tokens, attributes); } @Override public void remove() { throw new UnsupportedOperationException(); } }; DictionaryEntryPersistor.serialize(out, entryIterator, false); } NGramModel();  NGramModel(InputStream in); }
NGramModel implements Iterable<StringList> { public void serialize(OutputStream out) throws IOException { Iterator<Entry> entryIterator = new Iterator<Entry>() { private Iterator<StringList> mDictionaryIterator = NGramModel.this.iterator(); @Override public boolean hasNext() { return mDictionaryIterator.hasNext(); } @Override public Entry next() { StringList tokens = mDictionaryIterator.next(); Attributes attributes = new Attributes(); attributes.setValue(COUNT, Integer.toString(getCount(tokens))); return new Entry(tokens, attributes); } @Override public void remove() { throw new UnsupportedOperationException(); } }; DictionaryEntryPersistor.serialize(out, entryIterator, false); } NGramModel();  NGramModel(InputStream in); int getCount(StringList ngram); void setCount(StringList ngram, int count); void add(StringList ngram); void add(StringList ngram, int minLength, int maxLength); void add(CharSequence chars, int minLength, int maxLength); void remove(StringList tokens); boolean contains(StringList tokens); int size(); @Override Iterator<StringList> iterator(); int numberOfGrams(); void cutoff(int cutoffUnder, int cutoffOver); Dictionary toDictionary(); Dictionary toDictionary(boolean caseSensitive); void serialize(OutputStream out); @Override boolean equals(Object obj); @Override String toString(); @Override int hashCode(); }
NGramModel implements Iterable<StringList> { public void serialize(OutputStream out) throws IOException { Iterator<Entry> entryIterator = new Iterator<Entry>() { private Iterator<StringList> mDictionaryIterator = NGramModel.this.iterator(); @Override public boolean hasNext() { return mDictionaryIterator.hasNext(); } @Override public Entry next() { StringList tokens = mDictionaryIterator.next(); Attributes attributes = new Attributes(); attributes.setValue(COUNT, Integer.toString(getCount(tokens))); return new Entry(tokens, attributes); } @Override public void remove() { throw new UnsupportedOperationException(); } }; DictionaryEntryPersistor.serialize(out, entryIterator, false); } NGramModel();  NGramModel(InputStream in); int getCount(StringList ngram); void setCount(StringList ngram, int count); void add(StringList ngram); void add(StringList ngram, int minLength, int maxLength); void add(CharSequence chars, int minLength, int maxLength); void remove(StringList tokens); boolean contains(StringList tokens); int size(); @Override Iterator<StringList> iterator(); int numberOfGrams(); void cutoff(int cutoffUnder, int cutoffOver); Dictionary toDictionary(); Dictionary toDictionary(boolean caseSensitive); void serialize(OutputStream out); @Override boolean equals(Object obj); @Override String toString(); @Override int hashCode();  }