aboutsummaryrefslogtreecommitdiff
path: root/buildSrc/src/main/java/WordListWriter.java
blob: 54286c7826e54dbbb078ac95e2f019c2c4c9e36a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

class WordListWriter implements Closeable {

    private final XmlWriter mXmlWriter;
    private long mWordsWritten;

    WordListWriter(File outputWordsListFile) throws IOException {
        final File parentFile = outputWordsListFile.getParentFile();
        if (!parentFile.exists() && !parentFile.mkdirs()) {
            throw new IllegalArgumentException(
                    "Failed to create output folder " + parentFile.getAbsolutePath());
        }
        OutputStreamWriter outputWriter =
                new OutputStreamWriter(
                        new FileOutputStream(outputWordsListFile), Charset.forName("UTF-8"));
        mXmlWriter = new XmlWriter(outputWriter, true, 0, true);
        mXmlWriter.writeEntity("wordlist");
    }

    public static void writeWordWithRuntimeException(
            WordListWriter writer, String word, int frequency) {
        try {
            writer.addEntry(word, frequency);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void addEntry(String word, int frequency) throws IOException {
        mXmlWriter
                .writeEntity("w")
                .writeAttribute("f", Integer.toString(frequency))
                .writeText(word)
                .endEntity();
        mWordsWritten++;
    }

    @Override
    public void close() throws IOException {
        mXmlWriter.endEntity();
        System.out.println("Wrote " + mWordsWritten + " words.");
        mXmlWriter.close();
    }

    private static int calcActualFreq(double wordIndex, double wordsCount) {
        return Math.min(255, 1 + (int) (255 * (wordsCount - wordIndex) / wordsCount));
    }
}