<commit_before>import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;

/**
 * Created by hjort on 12/11/15.
 */
public class Anagram {

    private static String[] commonEnglishWords = getCommonWords();
    private static String[] longAnagrams = getLongAnagrams(100, 10000);
    //TODO Add test case with many random strings, no or few anagrams

    public static void runTests() {

        SortingAnagramMap sortAM = new SortingAnagramMap();
        HashAnagramMap hashAM = new HashAnagramMap();
        PrimesAnagramMap primesAM = new PrimesAnagramMap();

        String[] originalTest = {"niste", "stien", "allfarveien", "konsert", "torsken", "stein"};
        System.out.println(new SortingAnagramMap().createMap(originalTest));
        System.out.println(new HashAnagramMap().createMap(originalTest));
        System.out.println(new PrimesAnagramMap().createMap(originalTest));

        long startCES = System.nanoTime();
        Map<String, List<String>> commonEnglishSort = sortAM.createMap(commonEnglishWords);
        long endCES = System.nanoTime();
        long runtimeCES = endCES - startCES;

        long startLAS = System.nanoTime();
        Map longAnagramsSort = sortAM.createMap(longAnagrams);
        long endLAS = System.nanoTime();
        long runtimeLAS = endLAS - startLAS;

        long startCEH = System.nanoTime();
        Map<Map<Character, Integer>, List<String>> commonEnglishHash = hashAM.createMap(commonEnglishWords);
        long endCEH = System.nanoTime();
        long runtimeCEH = endCEH - startCEH;

        long startLAH = System.nanoTime();
        Map longAnagramsHash = hashAM.createMap(longAnagrams);
        long endLAH = System.nanoTime();
        long runtimeLAH = endLAH - startLAH;

        long startCEP = System.nanoTime();
        Map<BigInteger, List<String>> commonEnglishPrime = primesAM.createMap(commonEnglishWords);
        long endCEP = System.nanoTime();
        long runtimeCEP = endCEP - startCEP;

        long startLAP = System.nanoTime();
        Map longAnagramsPrime = primesAM.createMap(longAnagrams);
        long endLAP = System.nanoTime();
        long runtimeLAP = endLAP - startLAP;


        //Logging
        try {
            PrintWriter writer = new PrintWriter("log.md", "UTF-8");

            writer.println("* <a href='#performance'>Performance</a>");
            writer.println("* <a href='#sortingres'>Sorting results</a>");
            writer.println("* <a href='#hashres'>Hash map results</a>");
            writer.println("* <a href='#primesres'>Prime numbers results</a>");
            writer.println();
            writer.println("<a name='performance'></a>Performance (unit = milliseconds)");
            writer.println("=============================\n");
            writer.println("* Common english, sort: " + (runtimeCES / 1000000));
            writer.println("* Common english, hash: " + (runtimeCEH / 1000000));
            writer.println("* Common english, primes: " + (runtimeCEP / 1000000));
            writer.println("* Long anagrams, sort: " + (runtimeLAS / 1000000));
            writer.println("* Long anagrams, hash: " + (runtimeLAH / 1000000));
            writer.println("* Long anagrams, primes: " + (runtimeLAP / 1000000));

            writer.println();
            writer.println("<a name='sortingres'></a>Result from sorting algorithm");
            writer.println("=============================\n");
            writer.println("| key | values |");
            writer.println("|---|--------|");
            for (Map.Entry<String, List<String>> entry : commonEnglishSort.entrySet()) {
                writer.println("| " + entry.getKey() + " | " + entry.getValue().toString() + " |");
            }
            writer.println();
            writer.println("<a name='hashres'></a>Result from hashmap algorithm");
            writer.println("=============================\n");
            writer.println("| key | values |");
            writer.println("|---|--------|");
            for (Map.Entry<Map<Character, Integer>, List<String>> entry : commonEnglishHash.entrySet()) {
                writer.println("| " + entry.getKey() + " | " + entry.getValue().toString() + " |");
            }
            writer.println();
            writer.println("<a name='primesres'></a>Result from primes algorithm");
            writer.println("=============================\n");
            writer.println("| key | values |");
            writer.println("|---|--------|");
            for (Map.Entry<BigInteger, List<String>> entry : commonEnglishPrime.entrySet()) {
                writer.println("| " + entry.getKey() + " | " + entry.getValue().toString() + " |");
            }
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.print("Testing finished");

    }

    //Methods for generating testing data

    /**
     * Get a whole bunch of ordinary words
     *
     * @return
     */
    private static String[] getCommonWords() {
        List<String> words = new LinkedList<>();
        try {
            Scanner sc = new Scanner(new File("/usr/share/dict/words"));
            while (sc.hasNext()) {
                words.add(sc.next());
            }
            sc = new Scanner(new File("/usr/share/dict/propernames"));
            while (sc.hasNext()) {
                words.add(sc.next());
            }
            sc = new Scanner(new File("/usr/share/dict/connectives"));
            while (sc.hasNext()) {
                words.add(sc.next());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();<commit_msg>Switch to logging with utility<commit_after>import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;

/**
 * Created by hjort on 12/11/15.
 */
public class Anagram {

    private static String[] commonEnglishWords = getCommonWords();
    private static String[] longAnagrams = getLongAnagrams(100, 10000);
    private static PrintWriter writer;
    //TODO Add test case with many random strings, no or few anagrams

    public static void runTests() {

        SortingAnagramMap sortAM = new SortingAnagramMap();
        HashAnagramMap hashAM = new HashAnagramMap();
        PrimesAnagramMap primesAM = new PrimesAnagramMap();

        String[] originalTest = {"niste", "stien", "allfarveien", "konsert", "torsken", "stein"};
        System.out.println(new SortingAnagramMap().createMap(originalTest));
        System.out.println(new HashAnagramMap().createMap(originalTest));
        System.out.println(new PrimesAnagramMap().createMap(originalTest));

        long startCES = System.nanoTime();
        Map<String, List<String>> commonEnglishSort = sortAM.createMap(commonEnglishWords);
        long endCES = System.nanoTime();
        long runtimeCES = endCES - startCES;

        long startLAS = System.nanoTime();
        Map longAnagramsSort = sortAM.createMap(longAnagrams);
        long endLAS = System.nanoTime();
        long runtimeLAS = endLAS - startLAS;

        long startCEH = System.nanoTime();
        Map<Map<Character, Integer>, List<String>> commonEnglishHash = hashAM.createMap(commonEnglishWords);
        long endCEH = System.nanoTime();
        long runtimeCEH = endCEH - startCEH;

        long startLAH = System.nanoTime();
        Map longAnagramsHash = hashAM.createMap(longAnagrams);
        long endLAH = System.nanoTime();
        long runtimeLAH = endLAH - startLAH;

        long startCEP = System.nanoTime();
        Map<BigInteger, List<String>> commonEnglishPrimes = primesAM.createMap(commonEnglishWords);
        long endCEP = System.nanoTime();
        long runtimeCEP = endCEP - startCEP;

        long startLAP = System.nanoTime();
        Map longAnagramsPrime = primesAM.createMap(longAnagrams);
        long endLAP = System.nanoTime();
        long runtimeLAP = endLAP - startLAP;

        Map<String, String> results = new LinkedHashMap<>();
        results.put("Common english, sort", "" + (runtimeCES / 1000000));
        results.put("Common english, hash", "" + (runtimeCEH / 1000000));
        results.put("Common english, primes", "" + (runtimeCEP / 1000000));
        results.put("Long anagrams, sort", "" + (runtimeLAS / 1000000));
        results.put("Long anagrams, hash", "" + (runtimeLAH / 1000000));
        results.put("Long anagrams, primes", "" + (runtimeLAP / 1000000));

        //Logging
        try {

            Logger logger = new Logger("test_results.md");
            String[] tableOfContents = {
                    "* <a href='#performance'>Performance</a>",
                    "* <a href='#sorting'>Sorting results</a>",
                    "* <a href='#hashres'>Hash map results</a>",
                    "* <a href='#primesres'>Prime numbers results</a>"
            };

            logger.writeBulletlist(tableOfContents);

            logger.writeAnchor("performance");
            logger.writeHeader("Performance", 1);

            logger.writeParagraph("These are the running times of the different algorithms, on the different data sets");

            logger.writeTableMap("Algorithm, data set", "Running time (ms)", results);

            logger.writeAnchor("sorting");
            logger.writeHeader("Output from grouping ordinary words, sorting algorithm", 2);
            logger.writeTableMap("Key", "Value", commonEnglishSort);

            logger.writeAnchor("hash");
            logger.writeHeader("Output from grouping ordinary words, hash map algorithm", 2);
            logger.writeTableMap("Key", "Value", commonEnglishHash);

            logger.writeAnchor("sorting");
            logger.writeHeader("Output from grouping ordinary words, primes algorithm", 2);
            logger.writeTableMap("Key", "Value", commonEnglishPrimes);

            logger.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.print("Testing finished");

    }

    //GENERATE TEST DATA

    /**
     * Get a whole bunch of ordinary words
     *
     * @return
     */
    private static String[] getCommonWords() {
        List<String> words = new LinkedList<>();
        try {
            Scanner sc = new Scanner(new File("/usr/share/dict/words"));
            while (sc.hasNext()) {
                words.add(sc.next());
            }
            sc = new Scanner(new File("/usr/share/dict/propernames"));
            while (sc.hasNext()) {
                words.add(sc.next());
            }
            sc = new Scanner(new File("/usr/share/dict/connectives"));
            while (sc.hasNext()) {
                words.add(sc.next());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
{'fraction_non_alphanumeric': 0.14696983588521134, 'fraction_numerical': 0.010727056019070322, 'mean_word_length': 2.450806706738374, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1826141', 'n_tokens_mistral': 3158, 'n_tokens_neox': 2923, 'n_words': 667}