class Wrapper { static def getDictionaryLineData(String line, String delimiter) { String[] parts = line.split(delimiter, 3) String word = parts[0] String transcription = parts.length > 1 && parts[1] =~ "^[a-zA-Z]+\$" ? parts[1] : "" int frequency try { int partsElement = transcription.isEmpty() ? 1 : 2 frequency = (parts.length > partsElement ? parts[partsElement] : "0") as int } catch (Exception ignored) { frequency = -1 } return [word, transcription, frequency] } static def wordToDigitSequence(Locale locale, String word, HashMap sounds, boolean isTranscribed) { def sequence = new StringBuilder() final String normalizedWord = isTranscribed ? word : word.toUpperCase(locale) String currentSound = "" for (int i = 0, end = normalizedWord.length() - 1; i <= end; i++) { char currentChar = normalizedWord.charAt(i) char nextChar = i < end ? normalizedWord.charAt(i + 1) : 0 int nextCharType = Character.getType(nextChar) currentSound += currentChar // charAt(i) returns "ΐ" as three separate characters, but they must be treated as one. if ( locale.getLanguage() == "el" && (nextCharType == Character.NON_SPACING_MARK || nextCharType == Character.ENCLOSING_MARK || nextCharType == Character.COMBINING_SPACING_MARK) ) { continue } if (!isTranscribed || i == end || Character.isUpperCase(nextChar)) { if (!sounds.containsKey(currentSound)) { throw new IllegalArgumentException("Sound or layout entry '${currentSound}' does not belong to the language sound list: ${sounds}.") } else { sequence << sounds.get(currentSound) currentSound = "" } } } if (sequence.isEmpty()) { throw new IllegalArgumentException("The word does not contain any valid sounds.") } return sequence.toString() } static def getLanguageHash(File definitionFile, File dictionaryFile) { def definitionHash = definitionFile != null && definitionFile.exists() ? definitionFile.text.digest("SHA-256") : "" def dictionaryHash = dictionaryFile != null && dictionaryFile.exists() ? dictionaryFile.text.digest("SHA-256") : "" return definitionHash + dictionaryHash } } ext.DictionaryTools = Wrapper