diff --git a/build.gradle.kts b/build.gradle.kts index 082014a..b4c66ab 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,11 +1,13 @@ repositories { mavenCentral() + maven("https://jitpack.io") } plugins { `java-library` `maven-publish` } dependencies { + implementation("com.github.demidko:aot-bytecode:2021.10.26") testImplementation("org.junit.jupiter:junit-jupiter:5.8.1") testImplementation("org.hamcrest:hamcrest:2.2") } @@ -17,8 +19,6 @@ tasks.test { publishing { publications { create("aot") { - groupId = "com.github.demidko" - artifactId = "aot" from(components["java"]) } } diff --git a/src/main/java/com/github/demidko/aot/Bytecode.java b/src/main/java/com/github/demidko/aot/Bytecode.java deleted file mode 100644 index e6582d8..0000000 --- a/src/main/java/com/github/demidko/aot/Bytecode.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.github.demidko.aot; - -/** - * Класс хранит константы байткода - */ -final class Bytecode { - - public static final byte endOfCompiledLine = 100; - - public static boolean isEndl(byte b) { - return b == endOfCompiledLine; - } - - public static boolean isContent(byte b) { - return b != endOfCompiledLine; - } -} diff --git a/src/main/java/com/github/demidko/aot/MorphologyTag.java b/src/main/java/com/github/demidko/aot/MorphologyTag.java deleted file mode 100755 index a180bdd..0000000 --- a/src/main/java/com/github/demidko/aot/MorphologyTag.java +++ /dev/null @@ -1,388 +0,0 @@ -package com.github.demidko.aot; - -/** - * Морфологическая характеристика для слова. Например {@link MorphologyTag#FirstPerson} - */ -public enum MorphologyTag { - - /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - * В этом перечислении ни в коем случае нельзя менять (сдвигать) порядок элементов. От этого зависит корректность компиляции и - * декомпиляции. Важно чтобы это перечисление актуализировалось при изменениях формата в - * компиляторе - */ - - /** - * Множественное число - */ - Plural("мн"), - - /** - * Мужской род - */ - Male("мр"), - - /** - * Именительный падеж - */ - Nominative("им"), - - /** - * Местоимение-существительное - */ - Pronoun("МС"), - - /** - * Аббревиатура - */ - Abbreviation("аббр"), - - /** - * Краткое прилагательное - */ - ShortAdjective("КР_ПРИЛ"), - - /** - * Сравнительная степень (для прилагательных) - */ - Comparative("сравн"), - - /** - * Дательный падеж - */ - Dative("дт"), - - /** - * Настоящее время - */ - PresentTense("нст"), - - /** - * Указательное наречие - */ - IndicativeAdverb("указат"), - - /** - * Совершенный вид - */ - PerfectVerb("св"), - - /** - * Отчество (Иванович, Михайлович) - */ - MiddleName("отч"), - - /** - * Глагол в личной форме - */ - Verb("Г"), - - /** - * Прошедшее время - */ - PastTime("прш"), - - /** - * Междометие - */ - Interjection("МЕЖД"), - - /** - * Наречие - */ - Adverb("Н"), - - /** - * Единственное число - */ - Singular("ед"), - - /** - * Сравнительная степень (для прилагательных) - */ - NeuterGender("ср"), - - /** - * Прилагательное - */ - Adjective("П"), - - /** - * Существительное - */ - Noun("С"), - - /** - * Неодушевленное - */ - Inanimate("но"), - - /** - * Организация - */ - Organization("орг"), - - /** - * Вводное слово - */ - Introduction("ВВОДН"), - - /** - * Непереходный глагол - */ - Intransitive("нп"), - - /** - * Несовершенный вид - */ - Imperfect("нс"), - - /** - * Архаизм - */ - Archaism("арх"), - - /** - * Топоним (Москва, Лена, Эверест) - */ - Toponym("лок"), - - /** - * Общий род (сирота, пьяница) - */ - MaleFemale("мр-жр"), - - /** - * Местоимение-предикатив - */ - PronounPredicative("МС-ПРЕДК"), - - /** - * Первое лицо - */ - FirstPerson("1л"), - - /** - * Безличный глагол (2) - */ - ImpersonalVerb2("*"), - - /** - * Страдательный залог - */ - PassiveParticiple("стр"), - - /** - * Творительный падеж - */ - Ablative("тв"), - - /** - * Неизменяемое - */ - Immutable("0"), - - /** - * Второй родительный или второй предложный падежи - */ - SecondGenetive("2"), - - /** - * Разговорный - */ - ColloquialSpeech("разг"), - - /** - * Одушевленное - */ - Animated("од"), - - /** - * Опечатка - */ - Typo("опч"), - - /** - * Местоименное прилагательное - */ - PronounAdjective("МС-П"), - - /** - * Причастие - */ - Participle("ПРИЧАСТИЕ"), - - /** - * Превосходная степень (для прилагательных) - */ - SuperlativeAdjective("прев"), - - /** - * Фамилия (Иванов, Сидоров) - */ - Surname("фам"), - - /** - * Качественное прилагательное - */ - QualitativeAdjective("кач"), - - /** - * Действительный залог - */ - ActiveVoice("дст"), - - /** - * Деепричастие - */ - VerbalParticiple("ДЕЕПРИЧАСТИЕ"), - - /** - * Краткое причастие - */ - BriefCommunion("КР_ПРИЧАСТИЕ"), - - /** - * Прилагательное (?) - */ - AdjectiveUnusedType("дфст"), - - /** - * Второе лицо - */ - SecondPerson("2л"), - - /** - * Женский род - */ - Female("жр"), - - /** - * Винительный падеж - */ - Accusative("вн"), - - /** - * Будущее время - */ - FutureTime("буд"), - - /** - * Вопросительное наречие - */ - InterrogativeAdverb("вопр"), - - /** - * Некая часть речи (?) - */ - POSL_PART_OF_SPEECH("ПОСЛ"), - - /** - * Переходный глагол - */ - Transive("пе"), - - /** - * Жаргонизм - */ - Slang("жарг"), - - /** - * Повелительное наклонение (императив) - */ - Imperative("пвл"), - - /** - * Числительное (количественное) - */ - Numeral("ЧИСЛ"), - - /** - * Предикатив - */ - Predicative("ПРЕДК"), - - /** - * Частица - */ - Particle("ЧАСТ"), - - /** - * Предлог - */ - Pretext("ПРЕДЛ"), - - /** - * Звательный падеж (Отче, Боже) - */ - Vocative("зв"), - - /** - * Имя (Иван, Михаил) - */ - Name("имя"), - - /** - * Предложный падеж - */ - Prepositional("пр"), - - /** - * Третье лицо - */ - ThirdPerson("3л"), - - /** - * Инфинитив - */ - Infinitive("ИНФИНИТИВ"), - - /** - * Безличный глагол - */ - InpersonalVerb("безл"), - - /** - * Притяжательное - */ - Superlative("притяж"), - - /** - * Порядковое числительное - */ - OrdinalNumber("ЧИСЛ-П"), - - /** - * Фразеологизм - */ - Idiom("ФРАЗ"), - - /** - * Родительный падеж - */ - Genitive("рд"), - - /** - * Союз - */ - Union("СОЮЗ"); - - private final String token; - - MorphologyTag(final String token) { - this.token = token; - } - - @Override - public String toString() { - return token; - } - - public static MorphologyTag fromString(final String token) { - for (MorphologyTag info : values()) { - if (info.token.equals(token)) { - return info; - } - } - throw new IllegalArgumentException("Invalid token: " + token); - } -} diff --git a/src/main/java/com/github/demidko/aot/PartOfSpeech.java b/src/main/java/com/github/demidko/aot/PartOfSpeech.java index 8bcc56e..a33be42 100644 --- a/src/main/java/com/github/demidko/aot/PartOfSpeech.java +++ b/src/main/java/com/github/demidko/aot/PartOfSpeech.java @@ -1,5 +1,7 @@ package com.github.demidko.aot; +import com.github.demidko.aot.bytecode.MorphologyTag; + /** * Часть речи, например существительное, глагол и т. д. */ @@ -172,23 +174,6 @@ public static PartOfSpeech partOfSpeech(MorphologyTag tag) { } } - /** - * Метод извлекает часть речи из набора всей морфологической информации - * - * @param tags вся морфологическая информация - * @return извлеченная часть речи или null - */ - @Deprecated - public static PartOfSpeech partOfSpeech(MorphologyTag[] tags) { - for (MorphologyTag tag : tags) { - PartOfSpeech maybe = partOfSpeech(tag); - if (maybe != null) { - return maybe; - } - } - return null; - } - /** * Метод извлекает часть речи из набора всей морфологической информации * diff --git a/src/main/java/com/github/demidko/aot/Reader.java b/src/main/java/com/github/demidko/aot/Reader.java index beb356a..35860aa 100644 --- a/src/main/java/com/github/demidko/aot/Reader.java +++ b/src/main/java/com/github/demidko/aot/Reader.java @@ -1,78 +1,83 @@ package com.github.demidko.aot; +import static com.github.demidko.aot.bytecode.Bytecode.isContent; +import static com.github.demidko.aot.bytecode.MorphologyTag.values; +import static com.github.demidko.aot.bytecode.Utils.safeByteToChar; + +import com.github.demidko.aot.bytecode.MorphologyTag; import java.util.HashMap; import java.util.Map; class Reader { - static MorphologyTag[][] readMorph(ByteBlock block) { - MorphologyTag[][] result = new MorphologyTag[block.getLinesCount()][]; - for (int i = 0, pos = 0; i < block.getLinesCount(); ++i) { - int to = pos; - while (Bytecode.isContent(block.getBytes()[to])) { - ++to; - } - MorphologyTag[] currentTags = result[i] = new MorphologyTag[to - pos]; - for (int j = 0; j < currentTags.length; ++j, ++pos) { - currentTags[j] = MorphologyTag.values()[block.getBytes()[pos]]; - } - // ! - ++pos; - // ! - } - return result; - } + static MorphologyTag[][] readMorph(ByteBlock block) { + MorphologyTag[][] result = new MorphologyTag[block.getLinesCount()][]; + for (int i = 0, pos = 0; i < block.getLinesCount(); ++i) { + int to = pos; + while (isContent(block.getBytes()[to])) { + ++to; + } + MorphologyTag[] currentTags = result[i] = new MorphologyTag[to - pos]; + for (int j = 0; j < currentTags.length; ++j, ++pos) { + currentTags[j] = values()[block.getBytes()[pos]]; + } + // ! + ++pos; + // ! + } + return result; + } - static String[] readStrings(ByteBlock block) { - String[] result = new String[block.getLinesCount()]; - for (int i = 0, pos = 0; i < block.getLinesCount(); ++i) { - int to = pos; - while (Bytecode.isContent(block.getBytes()[to])) { - ++to; - } - char[] buf = new char[to - pos]; - for (int j = 0; j < buf.length; ++j, ++pos) { - buf[j] = Utils.safeByteToChar(block.getBytes()[pos]); - } - ++pos; - result[i] = new String(buf); - } - return result; - } + static String[] readStrings(ByteBlock block) { + String[] result = new String[block.getLinesCount()]; + for (int i = 0, pos = 0; i < block.getLinesCount(); ++i) { + int to = pos; + while (isContent(block.getBytes()[to])) { + ++to; + } + char[] buf = new char[to - pos]; + for (int j = 0; j < buf.length; ++j, ++pos) { + buf[j] = safeByteToChar(block.getBytes()[pos]); + } + ++pos; + result[i] = new String(buf); + } + return result; + } - static int[][] readLemmas(ByteBlock block) { - int[][] result = new int[block.getLinesCount()][]; - for (int i = 0, pos = 0; i < block.getLinesCount(); ++i) { - int lemmaSize = intFromByteArray(block.getBytes(), pos); - pos += 4; - int[] curr = result[i] = new int[lemmaSize * 2]; - for (int j = 0; j < (lemmaSize * 2); j += 2, pos += 8) { - curr[j] = intFromByteArray(block.getBytes(), pos); - curr[j + 1] = intFromByteArray(block.getBytes(), pos + 4); - } - } - return result; - } + static int[][] readLemmas(ByteBlock block) { + int[][] result = new int[block.getLinesCount()][]; + for (int i = 0, pos = 0; i < block.getLinesCount(); ++i) { + int lemmaSize = intFromByteArray(block.getBytes(), pos); + pos += 4; + int[] curr = result[i] = new int[lemmaSize * 2]; + for (int j = 0; j < (lemmaSize * 2); j += 2, pos += 8) { + curr[j] = intFromByteArray(block.getBytes(), pos); + curr[j + 1] = intFromByteArray(block.getBytes(), pos + 4); + } + } + return result; + } - static Map readRefs(ByteBlock block) { - Map result = new HashMap<>(); - for (int i = 0, pos = 0; i < block.getLinesCount(); ++i) { - int wordHash = intFromByteArray(block.getBytes(), pos); - pos += 4; - int[] indexes = new int[intFromByteArray(block.getBytes(), pos)]; - pos += 4; - for (int j = 0; j < indexes.length; ++j, pos += 4) { - indexes[j] = intFromByteArray(block.getBytes(), pos); - } - result.put(wordHash, indexes); - } - return result; - } + static Map readRefs(ByteBlock block) { + Map result = new HashMap<>(); + for (int i = 0, pos = 0; i < block.getLinesCount(); ++i) { + int wordHash = intFromByteArray(block.getBytes(), pos); + pos += 4; + int[] indexes = new int[intFromByteArray(block.getBytes(), pos)]; + pos += 4; + for (int j = 0; j < indexes.length; ++j, pos += 4) { + indexes[j] = intFromByteArray(block.getBytes(), pos); + } + result.put(wordHash, indexes); + } + return result; + } - private static int intFromByteArray(byte[] arr, int from) { - return arr[from + 3] & 0xFF | - (arr[from + 2] & 0xFF) << 8 | - (arr[from + 1] & 0xFF) << 16 | - (arr[from] & 0xFF) << 24; - } + private static int intFromByteArray(byte[] arr, int from) { + return arr[from + 3] & 0xFF | + (arr[from + 2] & 0xFF) << 8 | + (arr[from + 1] & 0xFF) << 16 | + (arr[from] & 0xFF) << 24; + } } diff --git a/src/main/java/com/github/demidko/aot/Utils.java b/src/main/java/com/github/demidko/aot/Utils.java deleted file mode 100644 index 80d85f9..0000000 --- a/src/main/java/com/github/demidko/aot/Utils.java +++ /dev/null @@ -1,350 +0,0 @@ -package com.github.demidko.aot; - -class Utils { - - public static char safeByteToChar(byte b) { - char c = byteToChar(b); - if (c == 0) { - throw new IllegalArgumentException("Invalid byte character: " + b); - } - return c; - } - - public static char byteToChar(byte b) { - switch (b) { - case (byte) 0x2d: - return '-'; - case (byte) 0x96: - return '–'; - case (byte) 0x97: - return '—'; - case (byte) 0x30: - return '0'; - case (byte) 0x31: - return '1'; - case (byte) 0x32: - return '2'; - case (byte) 0x33: - return '3'; - case (byte) 0x34: - return '4'; - case (byte) 0x35: - return '5'; - case (byte) 0x36: - return '6'; - case (byte) 0x37: - return '7'; - case (byte) 0x38: - return '8'; - case (byte) 0x39: - return '9'; - case (byte) 0xa8: - return 'Ё'; - case (byte) 0xb8: - return 'ё'; - case (byte) 0xc0: - return 'А'; - case (byte) 0xc1: - return 'Б'; - case (byte) 0xc2: - return 'В'; - case (byte) 0xc3: - return 'Г'; - case (byte) 0xc4: - return 'Д'; - case (byte) 0xc5: - return 'Е'; - case (byte) 0xc6: - return 'Ж'; - case (byte) 0xc7: - return 'З'; - case (byte) 0xc8: - return 'И'; - case (byte) 0xc9: - return 'Й'; - case (byte) 0xca: - return 'К'; - case (byte) 0xcb: - return 'Л'; - case (byte) 0xcc: - return 'М'; - case (byte) 0xcd: - return 'Н'; - case (byte) 0xce: - return 'О'; - case (byte) 0xcf: - return 'П'; - case (byte) 0xd0: - return 'Р'; - case (byte) 0xd1: - return 'С'; - case (byte) 0xd2: - return 'Т'; - case (byte) 0xd3: - return 'У'; - case (byte) 0xd4: - return 'Ф'; - case (byte) 0xd5: - return 'Х'; - case (byte) 0xd6: - return 'Ц'; - case (byte) 0xd7: - return 'Ч'; - case (byte) 0xd8: - return 'Ш'; - case (byte) 0xd9: - return 'Щ'; - case (byte) 0xda: - return 'Ъ'; - case (byte) 0xdb: - return 'Ы'; - case (byte) 0xdc: - return 'Ь'; - case (byte) 0xdd: - return 'Э'; - case (byte) 0xde: - return 'Ю'; - case (byte) 0xdf: - return 'Я'; - case (byte) 0xe0: - return 'а'; - case (byte) 0xe1: - return 'б'; - case (byte) 0xe2: - return 'в'; - case (byte) 0xe3: - return 'г'; - case (byte) 0xe4: - return 'д'; - case (byte) 0xe5: - return 'е'; - case (byte) 0xe6: - return 'ж'; - case (byte) 0xe7: - return 'з'; - case (byte) 0xe8: - return 'и'; - case (byte) 0xe9: - return 'й'; - case (byte) 0xea: - return 'к'; - case (byte) 0xeb: - return 'л'; - case (byte) 0xec: - return 'м'; - case (byte) 0xed: - return 'н'; - case (byte) 0xee: - return 'о'; - case (byte) 0xef: - return 'п'; - case (byte) 0xf0: - return 'р'; - case (byte) 0xf1: - return 'с'; - case (byte) 0xf2: - return 'т'; - case (byte) 0xf3: - return 'у'; - case (byte) 0xf4: - return 'ф'; - case (byte) 0xf5: - return 'х'; - case (byte) 0xf6: - return 'ц'; - case (byte) 0xf7: - return 'ч'; - case (byte) 0xf8: - return 'ш'; - case (byte) 0xf9: - return 'щ'; - case (byte) 0xfa: - return 'ъ'; - case (byte) 0xfb: - return 'ы'; - case (byte) 0xfc: - return 'ь'; - case (byte) 0xfd: - return 'э'; - case (byte) 0xfe: - return 'ю'; - case (byte) 0xff: - return 'я'; - - default: - return 0; - } - } - - public static byte safeCharToByte(char n) { - byte b = charToByte(n); - if (b == 0) { - throw new IllegalArgumentException("Invalid character: " + n); - } - return b; - } - - public static byte charToByte(char n) { - switch (n) { - case '-': - return (byte) 0x2d; - case '0': - return (byte) 0x30; - case '1': - return (byte) 0x31; - case '2': - return (byte) 0x32; - case '3': - return (byte) 0x33; - case '4': - return (byte) 0x34; - case '5': - return (byte) 0x35; - case '6': - return (byte) 0x36; - case '7': - return (byte) 0x37; - case '8': - return (byte) 0x38; - case '9': - return (byte) 0x39; - case '–': - return (byte) 0x96; - case '—': - return (byte) 0x97; - case 'А': - return (byte) 0xc0; - case 'Б': - return (byte) 0xc1; - case 'В': - return (byte) 0xc2; - case 'Г': - return (byte) 0xc3; - case 'Д': - return (byte) 0xc4; - case 'Е': - case 'Ё': - return (byte) 0xc5; - case 'Ж': - return (byte) 0xc6; - case 'З': - return (byte) 0xc7; - case 'И': - return (byte) 0xc8; - case 'Й': - return (byte) 0xc9; - case 'К': - return (byte) 0xca; - case 'Л': - return (byte) 0xcb; - case 'М': - return (byte) 0xcc; - case 'Н': - return (byte) 0xcd; - case 'О': - return (byte) 0xce; - case 'П': - return (byte) 0xcf; - case 'Р': - return (byte) 0xd0; - case 'С': - return (byte) 0xd1; - case 'Т': - return (byte) 0xd2; - case 'У': - return (byte) 0xd3; - case 'Ф': - return (byte) 0xd4; - case 'Х': - return (byte) 0xd5; - case 'Ц': - return (byte) 0xd6; - case 'Ч': - return (byte) 0xd7; - case 'Ш': - return (byte) 0xd8; - case 'Щ': - return (byte) 0xd9; - case 'Ъ': - return (byte) 0xda; - case 'Ы': - return (byte) 0xdb; - case 'Ь': - return (byte) 0xdc; - case 'Э': - return (byte) 0xdd; - case 'Ю': - return (byte) 0xde; - case 'Я': - return (byte) 0xdf; - case 'а': - return (byte) 0xe0; - case 'б': - return (byte) 0xe1; - case 'в': - return (byte) 0xe2; - case 'г': - return (byte) 0xe3; - case 'д': - return (byte) 0xe4; - case 'е': - case 'ё': - return (byte) 0xe5; - case 'ж': - return (byte) 0xe6; - case 'з': - return (byte) 0xe7; - case 'и': - return (byte) 0xe8; - case 'й': - return (byte) 0xe9; - case 'к': - return (byte) 0xea; - case 'л': - return (byte) 0xeb; - case 'м': - return (byte) 0xec; - case 'н': - return (byte) 0xed; - case 'о': - return (byte) 0xee; - case 'п': - return (byte) 0xef; - case 'р': - return (byte) 0xf0; - case 'с': - return (byte) 0xf1; - case 'т': - return (byte) 0xf2; - case 'у': - return (byte) 0xf3; - case 'ф': - return (byte) 0xf4; - case 'х': - return (byte) 0xf5; - case 'ц': - return (byte) 0xf6; - case 'ч': - return (byte) 0xf7; - case 'ш': - return (byte) 0xf8; - case 'щ': - return (byte) 0xf9; - case 'ъ': - return (byte) 0xfa; - case 'ы': - return (byte) 0xfb; - case 'ь': - return (byte) 0xfc; - case 'э': - return (byte) 0xfd; - case 'ю': - return (byte) 0xfe; - case 'я': - return (byte) 0xff; - - default: - return 0x0; - } - } -} diff --git a/src/main/java/com/github/demidko/aot/WordformMeaning.java b/src/main/java/com/github/demidko/aot/WordformMeaning.java index 1d9c56e..8e1f01c 100644 --- a/src/main/java/com/github/demidko/aot/WordformMeaning.java +++ b/src/main/java/com/github/demidko/aot/WordformMeaning.java @@ -10,6 +10,7 @@ import static java.util.Collections.emptyList; import static java.util.Objects.hash; +import com.github.demidko.aot.bytecode.MorphologyTag; import java.io.DataInputStream; import java.io.IOException; import java.io.UncheckedIOException; @@ -27,7 +28,6 @@ */ public class WordformMeaning { - private static final MorphologyTag[][] allMorphologyTags; private static final String[] allFlexionStrings; private static final int[][] lemmas;