MarkovChainLanguageModel { public void train(Locale language, String example, String encoding) throws UnsupportedEncodingException { if (!chains.containsKey(language)) { chains.put(language, new Matrix(256 + 1, 256 + 1)); chainCounts.put(language, new long[256 + 1]); } final Matrix chain = chains.get(language); final long[] chainCount = chainCounts.get(language); final byte[] data = example.getBytes(encoding); int currentIndex = 0; final double[][] chainData = chain.getArray(); for (final byte b : data) { final int newIndex = (b & 0xff) + 1; chainData[currentIndex][newIndex] = chainData[currentIndex][newIndex] + 1; chainCount[currentIndex] += 1; currentIndex = newIndex; } } MarkovChainLanguageModel(); void train(Locale language, String example, String encoding); void train(Locale language, InputStream stream); String generate(Locale language, int length, String encoding);  }
@Test public void testGenerateWarAndPeace() throws IOException { final MarkovChainLanguageModel model = new MarkovChainLanguageModel(); model.train(Locale.ENGLISH, MarkovChainLanguageModel.class.getResourceAsStream("/org/openimaj/text/nlp/warandpeace.txt")); model.train(Locale.CHINESE, MarkovChainLanguageModel.class.getResourceAsStream("/org/openimaj/text/nlp/TouPengHsienHua.txt")); }