package de.visone.nlpext.network;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import de.visone.ext.nlp.TextUtilMap;
import edu.stanford.nlp.ling.HasWord;
import edu.stanford.nlp.process.DocumentPreprocessor;

/**
 * This class does the preprocessing part for {@link TextUtilMap}, which uses it
 * through {@link TextPreprocessorProxy}.
 * 
 * @author endele, revised by nick
 */
public class TextPreprocessor {
	private static final boolean logging = false;
	private int[] sentenceBeginnings;
	private int[][] wordBeginnings;
	private StringBuilder resultText;

	public TextPreprocessor() {
	}

	public void preprocessText(final String text) {
		final List<List<? extends HasWord>> sentences = getAllSentences(text);
		sentenceBeginnings = new int[sentences.size()];
		wordBeginnings = new int[sentences.size()][];
		int sentenceBegin = 1; // TODO revise change: sentenceBegin has formerly
		// been 0...
		int sentenceIndex = 0;
		resultText = new StringBuilder();
		for (final List<? extends HasWord> sentence : sentences) {
			// calculate word beginnings in this sentence
			final int[] wordBegins = new int[sentence.size()];
			int wordBegin = 0;
			int wordIndex = 0;
			for (final HasWord word : sentence) {
				wordBegins[wordIndex] = wordBegin;
				wordBegin += word.toString().length() + 1; // +1 for whitespace
				// between words
				wordIndex++;
			}
			wordBeginnings[sentenceIndex] = wordBegins;

			sentenceBeginnings[sentenceIndex] = sentenceBegin;

			// TODO revise change: String stringSent = sentence.toString() +
			// " ";
			final String stringSent = sentence.toString()
					.replaceAll(", .]", ".]").replaceAll(", ", " ");
			// System.out.println(sentence.toString());
			// System.out.println(stringSent);
			resultText.append(stringSent);
			sentenceBegin += stringSent.length();
			sentenceIndex++;
		}
	}

	/**
	 * get list of all document sentences, i.e. a list of objects of type
	 * Sentence, i.e. a list of lists of HasWords.
	 */
	protected static List<List<? extends HasWord>> getAllSentences(
			final String text) {
		final List<List<? extends HasWord>> allSentences = new ArrayList<List<? extends HasWord>>();
		for (final List<HasWord> sentence : new DocumentPreprocessor(
				new StringReader(text)))
			allSentences.add(sentence);
		return allSentences;
	}
}
