package com.johnuckele.puzzle.crossword;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class TXTtoJSON {

	public void convertFile(String inputFileName, String outputFileName) {
		try {
			JSONArray jsonArray = createJSONFromFile(inputFileName);
			saveJSONtoFile(jsonArray, outputFileName);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private JSONArray createJSONFromFile(String inputFileName) throws IOException {

		File file = new File(inputFileName);
		BufferedReader reader = new BufferedReader(new FileReader(file));
		JSONArray jsonArray = new JSONArray();

		try {
			String line;
			while ((line = reader.readLine()) != null) {
				JSONObject word = makeJSONWordObj(line);
				jsonArray.put(word);
			}
			return jsonArray;
		} finally {
			reader.close();
		}
	}

	private void saveJSONtoFile(JSONArray jsonArray, String outputFileName) throws IOException {
		FileWriter file = new FileWriter(outputFileName);
		file.write(jsonArray.toString());
		file.flush();
		file.close();
	}

	private JSONObject makeJSONWordObj(String s) {
		int score = (int) Math.pow(s.length(), 2);

		JSONObject word = new JSONObject();
		try {
			word.put("word", s);
			word.put("score", score);

		} catch (JSONException e) {
			e.printStackTrace();
		}
		return word;
	}

	public static void main(String[] args) {
		System.out.println("Converting file to a json rep of a WordList");
		TXTtoJSON converter = new TXTtoJSON();
		converter.convertFile("src/main/resources/words.txt", "src/main/resources/filler.json");
		System.out.println("Conversion finished");
	}
}

@@ -168,16 +168,185 @@
 ception;
+%0Aimport java.text.Normalizer;%0Aimport java.text.Normalizer.Form;%0Aimport java.util.ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.HashSet;%0Aimport java.util.Set;
 %0A%0Aimport
@@ -893,121 +893,539 @@
 ;%0A%09%09
-JSONArray jsonArray = new JSONArray();%0A%0A%09%09try %7B%0A%09%09%09String line;%0A%09%09%09while ((line = reader.readLine()) != null) %7B%0A%09
+Set%3CString%3E inputWordSet = new HashSet%3CString%3E();%0A%0A%09%09// Load and simplify all words%0A%09%09try %7B%0A%09%09%09String line;%0A%09%09%09while ((line = reader.readLine()) != null) %7B%0A%09%09%09%09line = stripDiacriticsAndNonLetters(line);%0A%09%09%09%09inputWordSet.add(line);%0A%09%09%09%7D%0A%09%09%7D finally %7B%0A%09%09%09reader.close();%0A%09%09%7D%0A%0A%09%09// Generate a sorted list%0A%09%09ArrayList%3CString%3E inputWordList = new ArrayList%3CString%3E(inputWordSet);%0A%09%09Collections.sort(inputWordList);%0A%0A%09%09// Build and return the JSONArray%0A%09%09JSONArray jsonArray = new JSONArray();%0A%09%09for (String inputWord : inputWordList) %7B%0A
 %09%09%09J
@@ -1457,27 +1457,31 @@
 WordObj(
-l
 in
-e
+putWord
 );%0A%09%09%09
-%09
 jsonArra
@@ -1495,20 +1495,18 @@
 ord);%0A%09%09
-%09
 %7D%0A
-%09
 %09%09return
@@ -1522,43 +1522,242 @@
 y;%0A%09
-%09%7D finally %7B%0A%09%09%09reader.close();%0A%09%09%7D
+%7D%0A%0A%09private String stripDiacriticsAndNonLetters(String input) %7B%0A%09%09String decomposedInput = Normalizer.normalize(input, Form.NFD);%0A%09%09String filteredOuput = decomposedInput.toLowerCase().replaceAll(%22%5B%5Ea-z%5D%22, %22%22);%0A%09%09return filteredOuput;
 %0A%09%7D%0A
@@ -1935,16 +1935,17 @@
 oString(
+2
 ));%0A%09%09fi

word simplification