class MarkovChainLanguageModel {

	public String generate(Locale language, int length, String encoding) throws UnsupportedEncodingException {

		final Matrix chain = this.chains.get(language);
		if (chain == null)
			return null;
		final double[][] chainData = chain.getArray();
		final long[] chainCount = this.chainCounts.get(language);

		int currentIndex = 0;
		final byte[] newString = new byte[length];
		final Random r = new Random();
		for (int i = 0; i < length; i++) {
			final double prob = r.nextDouble();
			final double[] currentLine = chainData[currentIndex];
			double probSum = 0.0;
			int newIndex = 0;
			// System.out.println("CURRENT STATE:" + (char)(currentIndex-1));
			while (probSum + (currentLine[newIndex] / chainCount[currentIndex]) < prob) {
				final double probForIndex = (currentLine[newIndex++] / chainCount[currentIndex]);
				// System.out.println(probForIndex);
				// if(probForIndex > 0){
				// System.out.println("Prob to go to:" + (char)(newIndex-2) +
				// " = " + probForIndex);
				// }
				probSum += probForIndex;
			}
			// System.out.println("NEW STATE:" + (char)(newIndex-1));
			newString[i] = (byte) (newIndex - 1);
			currentIndex = newIndex;
		}

		return new String(newString, encoding);
	}

	public  MarkovChainLanguageModel();

}