Show pitch accent in result when available
CHANGELOG.md
| 24 | 24 | * Add a Help screen and two topics: kanji input by component and romaji input. | |
| 25 | 25 | * Add a button for direct help access on radical input. | |
| 26 | 26 | * Application can now download dictionaries from Wadoku. | |
| 27 | + | * Application can now show pitch accent with proper dictionary. | |
| 27 | 28 | ||
| 28 | 29 | ### Translations | |
| 29 | 30 |
app/src/main/java/eu/lepiller/nani/MainActivity.java
| 9 | 9 | import androidx.appcompat.widget.Toolbar; | |
| 10 | 10 | import androidx.preference.PreferenceManager; | |
| 11 | 11 | ||
| 12 | + | import android.util.Log; | |
| 12 | 13 | import android.view.View; | |
| 13 | 14 | import android.view.Menu; | |
| 14 | 15 | import android.view.MenuItem; | |
… | |||
| 37 | 38 | private static ArrayList<Result> savedResults; | |
| 38 | 39 | private RadicalSelectorView radical_selector; | |
| 39 | 40 | ||
| 41 | + | private static final String TAG = "MAIN"; | |
| 42 | + | ||
| 40 | 43 | @Override | |
| 41 | 44 | protected void onCreate(Bundle savedInstanceState) { | |
| 42 | 45 | super.onCreate(savedInstanceState); | |
… | |||
| 239 | 242 | FuriganaTextView kanji_view = child_result.findViewById(R.id.kanji_view); | |
| 240 | 243 | LinearLayout senses_view = child_result.findViewById(R.id.sense_view); | |
| 241 | 244 | TextView additional_info = child_result.findViewById(R.id.additional_info_view); | |
| 245 | + | TextView pitch_view = child_result.findViewById(R.id.pitch_view); | |
| 242 | 246 | ||
| 243 | 247 | // Populate the data into the template view using the data object | |
| 244 | 248 | kanji_view.setFuriganaText(result.getKanjiFurigana()); | |
| 245 | 249 | ||
| 250 | + | // If pitch information is available, make it visible | |
| 251 | + | String pitch = result.getPitch(); | |
| 252 | + | if(pitch != null) { | |
| 253 | + | Log.d(TAG, "pitch: "+pitch); | |
| 254 | + | pitch_view.setVisibility(View.VISIBLE); | |
| 255 | + | pitch_view.setText(pitch); | |
| 256 | + | } | |
| 257 | + | ||
| 246 | 258 | StringBuilder additional = new StringBuilder(); | |
| 247 | 259 | boolean separator = false; | |
| 248 | 260 | for (String s : result.getAlternatives()) { | |
app/src/main/java/eu/lepiller/nani/dictionary/DictionaryFactory.java
| 232 | 232 | private static void augment(Result r) throws IncompatibleFormatException { | |
| 233 | 233 | for(Dictionary d: dictionaries) { | |
| 234 | 234 | if(d instanceof ResultAugmenterDictionary && d.isDownloaded()) { | |
| 235 | + | Log.d(TAG, "Augmenting " + r.getKanji()); | |
| 235 | 236 | ((ResultAugmenterDictionary) d).augment(r); | |
| 236 | 237 | } | |
| 237 | 238 | } |
app/src/main/java/eu/lepiller/nani/dictionary/FileDictionary.java
| 75 | 75 | String expected = readSha256FromFile(sha256); | |
| 76 | 76 | ||
| 77 | 77 | // If the file has the expected hash, we have it | |
| 78 | - | Log.d(TAG, "expected: " + expected + "; actual: " + hash); | |
| 78 | + | Log.v(TAG, "expected: " + expected + "; actual: " + hash); | |
| 79 | 79 | return expected.compareTo(hash) == 0; | |
| 80 | 80 | } catch (FileNotFoundException e) { | |
| 81 | 81 | e.printStackTrace(); | |
… | |||
| 179 | 179 | ||
| 180 | 180 | return new HuffmanTree(left, right); | |
| 181 | 181 | } else if (b == 0) { | |
| 182 | - | Log.d(TAG, "Skipping byte " + file.readByte()); | |
| 182 | + | Log.v(TAG, "Skipping byte " + file.readByte()); | |
| 183 | 183 | return new HuffmanValue(""); | |
| 184 | 184 | } else { | |
| 185 | 185 | ArrayList<Byte> bs = new ArrayList<>(); | |
… | |||
| 203 | 203 | while(c == null || !c.isEmpty()) { | |
| 204 | 204 | if(h instanceof ResultDictionary.HuffmanValue) { | |
| 205 | 205 | c = ((ResultDictionary.HuffmanValue) h).character; | |
| 206 | - | Log.d(TAG, "Huffman read: " + c); | |
| 206 | + | Log.v(TAG, "Huffman read: " + c); | |
| 207 | 207 | b.append(c); | |
| 208 | 208 | h = huffman; | |
| 209 | 209 | } else if(h instanceof ResultDictionary.HuffmanTree) { | |
| 210 | 210 | if(bits.isEmpty()) { | |
| 211 | 211 | byte by = file.readByte(); | |
| 212 | - | Log.d(TAG, "Read byte for huffman: " + by); | |
| 212 | + | Log.v(TAG, "Read byte for huffman: " + by); | |
| 213 | 213 | for(int i = 7; i>-1; i--) { | |
| 214 | 214 | bits.add((by&(1<<i))!=0); | |
| 215 | 215 | } | |
| 216 | - | Log.d(TAG, "Read byte for huffman: " + bits); | |
| 216 | + | Log.v(TAG, "Read byte for huffman: " + bits); | |
| 217 | 217 | } | |
| 218 | 218 | ||
| 219 | 219 | Boolean bo = bits.get(0); | |
… | |||
| 226 | 226 | } | |
| 227 | 227 | ||
| 228 | 228 | static void logHuffman(Huffman h, ArrayList<Boolean> address) { | |
| 229 | - | if (h instanceof JMDict.HuffmanValue) { | |
| 230 | - | Log.d(TAG, "HUFF: " + ((JMDict.HuffmanValue) h).character + " -> " + address.toString()); | |
| 231 | - | } else if(h instanceof JMDict.HuffmanTree) { | |
| 229 | + | if (h instanceof ResultDictionary.HuffmanValue) { | |
| 230 | + | Log.v(TAG, "HUFF: " + ((ResultDictionary.HuffmanValue) h).character + " -> " + address.toString()); | |
| 231 | + | } else if(h instanceof ResultDictionary.HuffmanTree) { | |
| 232 | 232 | ArrayList<Boolean> address_l = new ArrayList<>(address); | |
| 233 | 233 | address_l.add(false); | |
| 234 | 234 | ArrayList<Boolean> address_r = new ArrayList<>(address); | |
… | |||
| 241 | 241 | static ArrayList<String> getHuffmanStringList(RandomAccessFile file, Huffman huffman) throws IOException { | |
| 242 | 242 | ArrayList<String> results = new ArrayList<>(); | |
| 243 | 243 | int number = file.readShort(); | |
| 244 | - | Log.d(TAG, "huffmanStrings: " + number); | |
| 244 | + | Log.v(TAG, "huffmanStrings: " + number); | |
| 245 | 245 | for(int i=0; i<number; i++) { | |
| 246 | 246 | results.add(getHuffmanString(file, huffman)); | |
| 247 | 247 | } | |
… | |||
| 255 | 255 | } | |
| 256 | 256 | ||
| 257 | 257 | int valuesLength = file.readShort(); | |
| 258 | - | Log.d(TAG, "number of values: " + valuesLength); | |
| 258 | + | Log.v(TAG, "number of values: " + valuesLength); | |
| 259 | 259 | file.skipBytes(valuesLength * 4); | |
| 260 | 260 | ||
| 261 | 261 | int transitionLength = file.readByte(); | |
| 262 | - | Log.d(TAG, "number of transitions: " + transitionLength); | |
| 262 | + | Log.v(TAG, "number of transitions: " + transitionLength); | |
| 263 | 263 | ||
| 264 | 264 | for(int i = 0; i < transitionLength; i++) { | |
| 265 | 265 | byte letter = file.readByte(); | |
| 266 | - | Log.d(TAG, "Possible transition " + letter + "; Expected transition: " + txt[0]); | |
| 266 | + | Log.v(TAG, "Possible transition " + letter + "; Expected transition: " + txt[0]); | |
| 267 | 267 | if(letter == txt[0]) { | |
| 268 | - | Log.d(TAG, "Taking transition "+letter); | |
| 268 | + | Log.v(TAG, "Taking transition "+letter); | |
| 269 | 269 | byte[] ntxt = new byte[txt.length-1]; | |
| 270 | 270 | System.arraycopy(txt, 1, ntxt, 0, txt.length-1); | |
| 271 | 271 | return searchTrie(file, file.readInt(), ntxt, decoder); | |
app/src/main/java/eu/lepiller/nani/dictionary/ResultDictionary.java
| 30 | 30 | ||
| 31 | 31 | private Result getValue(RandomAccessFile file, long pos) throws IOException { | |
| 32 | 32 | file.seek(pos); | |
| 33 | - | Log.d(TAG, "Getting value at " + pos); | |
| 33 | + | Log.v(TAG, "Getting value at " + pos); | |
| 34 | 34 | ArrayList<String> kanjis = getHuffmanStringList(file, kanjiHuffman); | |
| 35 | 35 | ||
| 36 | - | Log.d(TAG, "Getting readings"); | |
| 36 | + | Log.v(TAG, "Getting readings"); | |
| 37 | 37 | ArrayList<Result.Reading> readings = new ArrayList<>(); | |
| 38 | 38 | int reading_number = file.readShort(); | |
| 39 | - | Log.d(TAG, reading_number + " readings."); | |
| 39 | + | Log.v(TAG, reading_number + " readings."); | |
| 40 | 40 | for(int i=0; i<reading_number; i++) { | |
| 41 | 41 | ArrayList<String> reading_kanjis = getStringList(file); | |
| 42 | - | Log.d(TAG, "kanjis: " + reading_kanjis); | |
| 42 | + | Log.v(TAG, "kanjis: " + reading_kanjis); | |
| 43 | 43 | ArrayList<String> reading_infos = getStringList(file); | |
| 44 | - | Log.d(TAG, "infos: " + reading_kanjis); | |
| 44 | + | Log.v(TAG, "infos: " + reading_kanjis); | |
| 45 | 45 | ArrayList<String> reading_readings = getHuffmanStringList(file, readingHuffman); | |
| 46 | 46 | Result.Reading r = new Result.Reading(reading_kanjis, reading_infos, reading_readings); | |
| 47 | 47 | readings.add(r); | |
… | |||
| 49 | 49 | ||
| 50 | 50 | ArrayList<Result.Sense> senses = new ArrayList<>(); | |
| 51 | 51 | int meaning_number = file.readShort(); | |
| 52 | - | Log.d(TAG, meaning_number + " meanings."); | |
| 52 | + | Log.v(TAG, meaning_number + " meanings."); | |
| 53 | 53 | for(int i=0; i<meaning_number; i++) { | |
| 54 | 54 | ArrayList<String> sense_references = getStringList(file); | |
| 55 | 55 | ArrayList<String> sense_limits = getStringList(file); | |
… | |||
| 74 | 74 | ||
| 75 | 75 | private ArrayList<Integer> getValues(RandomAccessFile file, long triePos) throws IOException { | |
| 76 | 76 | file.seek(triePos); | |
| 77 | - | Log.d(TAG, "Getting values"); | |
| 77 | + | Log.v(TAG, "Getting values"); | |
| 78 | 78 | int valuesLength = file.readShort(); | |
| 79 | 79 | ArrayList<Integer> results = new ArrayList<>(); | |
| 80 | 80 | ||
| 81 | - | Log.d(TAG, "Number of values: " + valuesLength); | |
| 81 | + | Log.v(TAG, "Number of values: " + valuesLength); | |
| 82 | 82 | for(int i=0; i<valuesLength; i++) { | |
| 83 | 83 | results.add(file.readInt()); | |
| 84 | 84 | } | |
| 85 | 85 | ||
| 86 | 86 | int transitionLength = file.readByte(); | |
| 87 | - | Log.d(TAG, "Number of transitions: " + transitionLength); | |
| 87 | + | Log.v(TAG, "Number of transitions: " + transitionLength); | |
| 88 | 88 | int[] others = new int[transitionLength]; | |
| 89 | 89 | for(int i=0; i<transitionLength; i++) { | |
| 90 | 90 | file.skipBytes(1); | |
… | |||
| 95 | 95 | results.addAll(getValues(file, others[i])); | |
| 96 | 96 | } | |
| 97 | 97 | ||
| 98 | - | Log.d(TAG, "result size: " + results.size()); | |
| 98 | + | Log.v(TAG, "result size: " + results.size()); | |
| 99 | 99 | return results; | |
| 100 | 100 | } | |
| 101 | 101 | ||
… | |||
| 128 | 128 | long meaningTriePos = file.readInt(); | |
| 129 | 129 | ||
| 130 | 130 | Log.d(TAG, "Search in: " + getFile()); | |
| 131 | - | Log.d(TAG, "kanji: " + kanjiTriePos); | |
| 132 | - | Log.d(TAG, "reading: " + readingTriePos); | |
| 133 | - | Log.d(TAG, "meaning: " + meaningTriePos); | |
| 131 | + | Log.v(TAG, "kanji: " + kanjiTriePos); | |
| 132 | + | Log.v(TAG, "reading: " + readingTriePos); | |
| 133 | + | Log.v(TAG, "meaning: " + meaningTriePos); | |
| 134 | 134 | ||
| 135 | 135 | kanjiHuffman = loadHuffman(file); | |
| 136 | 136 | readingHuffman = loadHuffman(file); | |
… | |||
| 166 | 166 | } | |
| 167 | 167 | Arrays.sort(uniqResultsArray); | |
| 168 | 168 | ||
| 169 | - | Log.d(TAG, uniqResults.toString()); | |
| 169 | + | Log.v(TAG, uniqResults.toString()); | |
| 170 | 170 | ||
| 171 | 171 | int num = 0; | |
| 172 | 172 | for(int i: uniqResultsArray) { | |
app/src/main/java/eu/lepiller/nani/dictionary/WadokuPitchDictionary.java
| 15 | 15 | ||
| 16 | 16 | class WadokuPitchDictionary extends ResultAugmenterDictionary { | |
| 17 | 17 | private static final String TAG = "PITCH"; | |
| 18 | - | private Huffman huffman; | |
| 18 | + | private Huffman huffman = null; | |
| 19 | 19 | private long triePos; | |
| 20 | 20 | ||
| 21 | 21 | WadokuPitchDictionary(String name, String description, String fullDescription, File cacheDir, String url, int fileSize, int entries, String hash) { | |
… | |||
| 41 | 41 | void augment(Result r) throws IncompatibleFormatException { | |
| 42 | 42 | try { | |
| 43 | 43 | RandomAccessFile file = new RandomAccessFile(getFile(), "r"); | |
| 44 | - | byte[] header = new byte[13]; | |
| 45 | - | int l = file.read(header); | |
| 46 | - | if (l != header.length) | |
| 47 | - | return; | |
| 48 | 44 | ||
| 49 | - | if(!Arrays.equals(header, "NANI_PITCH001".getBytes())) | |
| 50 | - | throw new IncompatibleFormatException(getName()); | |
| 45 | + | if(huffman == null) { | |
| 46 | + | byte[] header = new byte[13]; | |
| 47 | + | int l = file.read(header); | |
| 48 | + | if (l != header.length) | |
| 49 | + | return; | |
| 51 | 50 | ||
| 52 | - | // number of entries | |
| 53 | - | file.readInt(); | |
| 51 | + | if (!Arrays.equals(header, "NANI_PITCH001".getBytes())) | |
| 52 | + | throw new IncompatibleFormatException(getName()); | |
| 54 | 53 | ||
| 55 | - | huffman = loadHuffman(file); | |
| 56 | - | logHuffman(huffman, new ArrayList<Boolean>()); | |
| 54 | + | // number of entries | |
| 55 | + | file.readInt(); | |
| 57 | 56 | ||
| 58 | - | triePos = file.getFilePointer(); | |
| 57 | + | huffman = loadHuffman(file); | |
| 58 | + | logHuffman(huffman, new ArrayList<Boolean>()); | |
| 59 | + | ||
| 60 | + | triePos = file.getFilePointer(); | |
| 61 | + | } | |
| 59 | 62 | ||
| 60 | 63 | List<String> kanjis = r.getAlternatives(); | |
| 61 | 64 | ||
| 62 | 65 | for(String k: kanjis) { | |
| 66 | + | Log.d(TAG, "Searching pitch for " + k); | |
| 63 | 67 | String pitch = findPitch(k, file); | |
| 64 | 68 | if(pitch != null) { | |
| 69 | + | Log.d(TAG, "Found " + pitch); | |
| 65 | 70 | r.addPitch(k, pitch); | |
| 66 | 71 | } | |
| 67 | 72 | } | |
app/src/main/java/eu/lepiller/nani/result/Result.java
| 2 | 2 | ||
| 3 | 3 | import android.util.Log; | |
| 4 | 4 | ||
| 5 | - | import java.io.IOException; | |
| 6 | - | import java.io.RandomAccessFile; | |
| 7 | 5 | import java.util.ArrayList; | |
| 8 | 6 | import java.util.regex.Matcher; | |
| 9 | 7 | import java.util.regex.Pattern; | |
… | |||
| 101 | 99 | return reading; | |
| 102 | 100 | } | |
| 103 | 101 | ||
| 102 | + | public String getPitch() { | |
| 103 | + | if(readings.size() > 0) { | |
| 104 | + | ArrayList<String> pitches = readings.get(0).pitches; | |
| 105 | + | if(pitches.size() > 0) | |
| 106 | + | return pitches.get(0); | |
| 107 | + | } | |
| 108 | + | return null; | |
| 109 | + | } | |
| 110 | + | ||
| 104 | 111 | public String getKanjiFurigana() { | |
| 105 | 112 | String txt = getKanji(); | |
| 106 | 113 | String reading = getReading(); | |
| 107 | - | Log.d("RESULT", "reading: " + reading); | |
| 114 | + | Log.v("RESULT", "reading: " + reading); | |
| 108 | 115 | ||
| 109 | 116 | // split the text into kanji / not kanji portions | |
| 110 | 117 | ArrayList<String> portions = new ArrayList<>(); | |
… | |||
| 142 | 149 | } | |
| 143 | 150 | current.append("$"); | |
| 144 | 151 | ||
| 145 | - | Log.d("RESULT", "regex: " + current.toString()); | |
| 152 | + | Log.v("RESULT", "regex: " + current.toString()); | |
| 146 | 153 | ||
| 147 | 154 | Pattern p = Pattern.compile(current.toString()); | |
| 148 | 155 | Matcher m = p.matcher(reading); | |
| 149 | 156 | ||
| 150 | 157 | if(!m.matches()) { | |
| 151 | - | Log.d("RESULT", "Finaly: " + txt); | |
| 158 | + | Log.v("RESULT", "Finaly: " + txt); | |
| 152 | 159 | return txt; | |
| 153 | 160 | } | |
| 154 | 161 | ||
| 155 | 162 | // We have a match! | |
| 156 | 163 | ||
| 157 | - | Log.d("RESULT", "matched"); | |
| 164 | + | Log.v("RESULT", "matched"); | |
| 158 | 165 | ||
| 159 | 166 | current = new StringBuilder(); | |
| 160 | 167 | int group = 1; | |
… | |||
| 171 | 178 | current.append(s); | |
| 172 | 179 | } | |
| 173 | 180 | } | |
| 174 | - | Log.d("RESULT", "Finaly: " + current.toString()); | |
| 181 | + | Log.v("RESULT", "Finaly: " + current.toString()); | |
| 175 | 182 | return current.toString(); | |
| 176 | 183 | } | |
| 177 | 184 | ||
app/src/main/res/layout/layout_result.xml
| 18 | 18 | app:contains_ruby_tags="true" | |
| 19 | 19 | android:textSize="@dimen/title_size" /> | |
| 20 | 20 | ||
| 21 | + | <TextView | |
| 22 | + | android:layout_width="wrap_content" | |
| 23 | + | android:layout_height="wrap_content" | |
| 24 | + | android:id="@+id/pitch_view" | |
| 25 | + | android:visibility="gone"/> | |
| 26 | + | ||
| 21 | 27 | <LinearLayout | |
| 22 | 28 | android:layout_width="wrap_content" | |
| 23 | 29 | android:layout_height="wrap_content" |