|
| 1 | +/** |
| 2 | + * |
| 3 | + */ |
| 4 | +package io.github.mzattera.v4j.applications.chars; |
| 5 | + |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map.Entry; |
| 8 | + |
| 9 | +import io.github.mzattera.v4j.experiment.Experiment; |
| 10 | +import io.github.mzattera.v4j.text.ElementFilter; |
| 11 | +import io.github.mzattera.v4j.text.Text; |
| 12 | +import io.github.mzattera.v4j.text.alphabet.Alphabet; |
| 13 | +import io.github.mzattera.v4j.text.ivtff.IvtffPage; |
| 14 | +import io.github.mzattera.v4j.text.ivtff.IvtffText; |
| 15 | +import io.github.mzattera.v4j.text.ivtff.LineFilter; |
| 16 | +import io.github.mzattera.v4j.text.ivtff.PageFilter; |
| 17 | +import io.github.mzattera.v4j.text.ivtff.PageHeader; |
| 18 | +import io.github.mzattera.v4j.text.ivtff.VoynichFactory; |
| 19 | +import io.github.mzattera.v4j.text.ivtff.VoynichFactory.Transcription; |
| 20 | +import io.github.mzattera.v4j.text.ivtff.VoynichFactory.TranscriptionType; |
| 21 | +import io.github.mzattera.v4j.util.Counter; |
| 22 | + |
| 23 | +/** |
| 24 | + * Statistics about words starting or containing gallows, in different positions |
| 25 | + * of the text. |
| 26 | + * |
| 27 | + * |
| 28 | + * @author Massimiliano "Maxi" Zattera |
| 29 | + * |
| 30 | + */ |
| 31 | +public final class CountGallows { |
| 32 | + |
| 33 | + /** |
| 34 | + * Which transcription to use. |
| 35 | + */ |
| 36 | + public static final Transcription TRANSCRIPTION = Transcription.AUGMENTED; |
| 37 | + |
| 38 | + /** |
| 39 | + * Which transcription type to use. |
| 40 | + */ |
| 41 | + public static final TranscriptionType TRANSCRIPTION_TYPE = TranscriptionType.MAJORITY; |
| 42 | + |
| 43 | + /** |
| 44 | + * Which Alphabet type to use. |
| 45 | + */ |
| 46 | + public static final Alphabet ALPHABET = Alphabet.SLOT; |
| 47 | + |
| 48 | + /** Filter to use on pages before analysis */ |
| 49 | + public static final ElementFilter<IvtffPage> FILTER = null; |
| 50 | + |
| 51 | + private final static char[] GALLOWS = { 't', 'k', 'p', 'f', 'T', 'K', 'P', 'F', 'C', 'S' }; |
| 52 | + |
| 53 | + private CountGallows() { |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @param args |
| 58 | + */ |
| 59 | + public static void main(String[] args) { |
| 60 | + try { |
| 61 | + // Prints configuration parameters |
| 62 | + System.out.println("Transcription : " + TRANSCRIPTION); |
| 63 | + System.out.println("Transcription Type: " + TRANSCRIPTION_TYPE); |
| 64 | + System.out.println("Alphabet : " + ALPHABET); |
| 65 | + System.out.println("Filter : " + (FILTER == null ? "<no-filter>" : FILTER) |
| 66 | + + " running text only (P0 & P1)"); |
| 67 | + System.out.println(); |
| 68 | + |
| 69 | + IvtffText doc = VoynichFactory.getDocument(TRANSCRIPTION, TRANSCRIPTION_TYPE, ALPHABET); |
| 70 | + doc = doc.filterLines(LineFilter.PARAGRAPH_TEXT_FILTER); |
| 71 | + if (FILTER != null) |
| 72 | + doc = doc.filterPages(FILTER); |
| 73 | + |
| 74 | + System.out.println("% of Tokens Containig Gallows"); |
| 75 | + process(doc, false); |
| 76 | + System.out.println("\n\n% of Tokens Starting with Gallows"); |
| 77 | + process(doc, true); |
| 78 | + |
| 79 | + } catch (Exception e) { |
| 80 | + e.printStackTrace(); |
| 81 | + } finally { |
| 82 | + System.out.println("\nCompleted."); |
| 83 | + } |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @param doc |
| 89 | + * @param first If true count tokens starting with gallows, otherwise count |
| 90 | + * tokens containing gallows. |
| 91 | + */ |
| 92 | + private static void process(IvtffText doc, boolean first) { |
| 93 | + // Header |
| 94 | + System.out.print("Locus;Cluster;"); |
| 95 | + for (char c : GALLOWS) |
| 96 | + System.out.print(c + ";"); |
| 97 | + System.out.println(); |
| 98 | + |
| 99 | + for (String cluster : PageHeader.CLUSTERS) { |
| 100 | + IvtffText clusterText = doc.filterPages(new PageFilter.Builder().cluster(cluster).build()); |
| 101 | + |
| 102 | + // Gets words in different positions; |
| 103 | + // TODO NOTICE WE IGNORE UNREADABLE WORDS!!!!! |
| 104 | + |
| 105 | + Text[] split = new Experiment.FirstLineInParagraph(false).splitDocument(clusterText); |
| 106 | + IvtffText firstLines = (IvtffText)split[0]; |
| 107 | + List<Counter<String>> firstLineWords = Experiment.getWordsByPosition(firstLines, true); |
| 108 | + |
| 109 | + // First words of paragraphs |
| 110 | + Counter<String> firstWordsOfPar = firstLineWords.get(0); |
| 111 | + System.out.print("First Word of a Paragraph;" + cluster + ";"); |
| 112 | + analize(firstWordsOfPar, first); |
| 113 | + |
| 114 | + // Other words in first line |
| 115 | + Counter<String> wordsInFirstLine = new Counter<>(); |
| 116 | + for (int i = 1; i < firstLineWords.size(); ++i) { |
| 117 | + wordsInFirstLine.countAll(firstLineWords.get(i)); |
| 118 | + } |
| 119 | + System.out.print("Remaining Words in First Line;" + cluster + ";"); |
| 120 | + analize(wordsInFirstLine, first); |
| 121 | + |
| 122 | + List<Counter<String>> other = Experiment |
| 123 | + .getWordsByPosition((IvtffText)split[1], true); |
| 124 | + |
| 125 | + // First words in remaining lines |
| 126 | + Counter<String> firstWordsInLine = other.get(0); |
| 127 | + System.out.print("First Word of Other Lines;" + cluster + ";"); |
| 128 | + analize(firstWordsInLine, first); |
| 129 | + |
| 130 | + // All other words |
| 131 | + Counter<String> rest = new Counter<>(); |
| 132 | + for (int i = 1; i < other.size(); ++i) { |
| 133 | + rest.countAll(other.get(i)); |
| 134 | + } |
| 135 | + System.out.print("Remaining Words;" + cluster + ";"); |
| 136 | + analize(rest, first); |
| 137 | + } // for each cluster |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Prints relevant % for given word distribution. |
| 142 | + * |
| 143 | + * @param wordsInFirstLine |
| 144 | + * @param first If true count tokens starting with gallows, otherwise |
| 145 | + * count tokens containing gallows. |
| 146 | + */ |
| 147 | + private static void analize(Counter<String> words, boolean first) { |
| 148 | + |
| 149 | + // How many tokens with given gallows |
| 150 | + Counter<Character> tokens = new Counter<>(); |
| 151 | + |
| 152 | + for (Entry<String, Integer> e : words.entrySet()) { |
| 153 | + for (char c : GALLOWS) { |
| 154 | + if ((first && (e.getKey().charAt(0) == c)) || (!first && (e.getKey().indexOf(c) != -1))) { |
| 155 | + tokens.count(c, e.getValue()); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + // Print results |
| 161 | + double t = 0.0; |
| 162 | + for (char c : GALLOWS) { |
| 163 | + double p = (double) tokens.getCount(c) / words.getTotalCounted(); |
| 164 | + if (Double.isNaN(p)) |
| 165 | + p = 0.0; |
| 166 | + t += p; |
| 167 | + System.out.print(p + ";"); |
| 168 | + } |
| 169 | + System.out.println(t); |
| 170 | + } |
| 171 | +} |
0 commit comments