-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ecbc72a
Showing
19 changed files
with
1,150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/.idea | ||
/.mvn | ||
/out | ||
/*.iml | ||
/target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 Aleksey G Kalenchukov | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Transliteration | ||
Транслитерация кириллического письма латинским алфавитом. | ||
|
||
### Поддерживаемые стандарты | ||
* ICAO DOC 9303 | ||
* ГОСТ 7.79-2000 схема A | ||
* ГОСТ 7.79-2000 схема B | ||
* ГОСТ 16876-71 система А | ||
* ГОСТ 16876-71 система B | ||
|
||
### Использование | ||
```java | ||
Transliterating transliteration = new Transliteration(Standard.ICAO_DOC_9303); | ||
transliteration.translate("Эй, жлоб! Где туз? Прячь юных съёмщиц в шкаф."); | ||
// Результат выполнения: | ||
// Ei, zhlob! Gde tuz? Priach iunykh sieemshchits v shkaf. | ||
``` | ||
|
||
### Статические методы | ||
Используя стандарт по умолчанию - ICAO DOC 9303: | ||
```java | ||
Transliterator.translate( | ||
"Шеф взъярён тчк щипцы с эхом гудбай Жюль." | ||
); | ||
// Результат выполнения: | ||
// Shef vzieiaren tchk shchiptsy s ekhom gudbai Zhiul. | ||
``` | ||
|
||
Используя указанный стандарт: | ||
```java | ||
Transliterator.translate( | ||
"Эй, жлоб! Где туз? Прячь юных съёмщиц в шкаф.", | ||
Standard.GOST_7_79_2000A | ||
); | ||
// Результат выполнения: | ||
// Èj, žlob! Gde tuz? Prâčʹ ûnyh sʺёmŝic v škaf. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[+] - нужно сделать | ||
[?] - возможно нужно сделать | ||
[>] - перенос в новую версию | ||
[x] - не делать | ||
|
||
# CORE | ||
* | ||
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ Copyright © 2022 Алексей Каленчуков | ||
~ GitHub: https://github.com/kalenchukov | ||
~ E-mail: mailto:[email protected] | ||
--> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>dev.kalenchukov</groupId> | ||
<artifactId>transliteration</artifactId> | ||
<version>1.0.0</version> | ||
|
||
<name>Transliteration</name> | ||
<description>Транслитерация кириллического письма латинским алфавитом.</description> | ||
<url>https://github.com/kalenchukov/Transliteration</url> | ||
|
||
<licenses> | ||
<license> | ||
<name>MIT License</name> | ||
<url>https://opensource.org/licenses/MIT</url> | ||
</license> | ||
</licenses> | ||
|
||
<developers> | ||
<developer> | ||
<id>kalenchukov</id> | ||
<name>Алексей Каленчуков</name> | ||
<email>[email protected]</email> | ||
<url>https://github.com/kalenchukov</url> | ||
</developer> | ||
</developers> | ||
|
||
<properties> | ||
<maven.compiler.source>16</maven.compiler.source> | ||
<maven.compiler.target>16</maven.compiler.target> | ||
<encoding>UTF-8</encoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.jetbrains</groupId> | ||
<artifactId>annotations</artifactId> | ||
<version>23.0.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.13.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>dev.kalenchukov</groupId> | ||
<artifactId>stringi</artifactId> | ||
<version>1.0.0</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
161 changes: 161 additions & 0 deletions
161
src/main/java/dev/kalenchukov/transliteration/AbstractTransliteration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/* | ||
* Copyright © 2022 Алексей Каленчуков | ||
* GitHub: https://github.com/kalenchukov | ||
* E-mail: mailto:[email protected] | ||
*/ | ||
|
||
package dev.kalenchukov.transliteration; | ||
|
||
import dev.kalenchukov.stringi.Stringi; | ||
import dev.kalenchukov.transliteration.resources.Standard; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Класс абстрактной транслитерации. | ||
*/ | ||
public abstract class AbstractTransliteration implements Transliterating | ||
{ | ||
/** | ||
* Стандарт. | ||
*/ | ||
protected Standard standard; | ||
|
||
/** | ||
* Конструктор для {@code AbstractTransliteration}. | ||
* | ||
* @param standard Стандарт. | ||
*/ | ||
public AbstractTransliteration(@NotNull final Standard standard) | ||
{ | ||
Objects.requireNonNull(standard); | ||
|
||
this.standard = standard; | ||
} | ||
|
||
/** | ||
* @see Transliterating#translate(String) | ||
*/ | ||
@NotNull | ||
public String translate(@NotNull final String text) | ||
{ | ||
Objects.requireNonNull(text); | ||
|
||
StringBuilder transliteration = new StringBuilder(); | ||
|
||
for (int position = 0; position < text.length(); position++) { | ||
transliteration.append(this.replacementSymbol(text, position)); | ||
} | ||
|
||
return transliteration.toString(); | ||
} | ||
|
||
/** | ||
* Заменяет символ по правилу замены. | ||
* | ||
* @param text Текст. | ||
* @param position Позиция символа в тексте. | ||
* @return Символы для замены или текущий символ если замена не требуется. | ||
*/ | ||
@NotNull | ||
private String replacementSymbol(@NotNull final String text, final int position) | ||
{ | ||
Objects.requireNonNull(text); | ||
|
||
String newSymbol = null; | ||
|
||
if (newSymbol == null) { | ||
newSymbol = this.checkRulesPrevious(text, position); | ||
} | ||
|
||
if (newSymbol == null) { | ||
newSymbol = this.checkRulesNext(text, position); | ||
} | ||
|
||
if (newSymbol == null) { | ||
newSymbol = this.checkRules(text, position); | ||
} | ||
|
||
if (newSymbol != null) { | ||
return Stringi.isUpperCase(text.charAt(position)) ? Stringi.firstToUpperCase(newSymbol) : newSymbol; | ||
} | ||
|
||
return String.valueOf(text.charAt(position)); | ||
} | ||
|
||
/** | ||
* Проверка символа на правило замены в зависимости от предыдущих символов. | ||
* | ||
* @param text Текст. | ||
* @param position Позиция символа в тексте. | ||
* @return Значение для замены или {@code null}, если замена символа не требуется. | ||
*/ | ||
@Nullable | ||
private String checkRulesPrevious(@NotNull final String text, final int position) | ||
{ | ||
Objects.requireNonNull(text); | ||
|
||
for (Map.Entry<String, String> entry : standard.getSchema().getRulesPrevious().entrySet()) | ||
{ | ||
int countSymbolsRules = entry.getKey().length(); | ||
|
||
if ((position - (countSymbolsRules - 1)) < 0) { | ||
continue; | ||
} | ||
|
||
String previousSymbols = text.substring((position + 1) - countSymbolsRules, position + 1).toLowerCase(); | ||
|
||
return standard.getSchema().getRulesPrevious().get(previousSymbols); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Проверка символа на соответствие правилу замены. | ||
* | ||
* @param text Текст. | ||
* @param position Позиция символа в тексте. | ||
* @return Значение для замены или {@code null}, если замена символа не требуется. | ||
*/ | ||
@Nullable | ||
private String checkRules(@NotNull final String text, final int position) | ||
{ | ||
Objects.requireNonNull(text); | ||
|
||
String currentSymbol = String.valueOf(text.charAt(position)).toLowerCase(); | ||
|
||
return standard.getSchema().getRules().get(currentSymbol); | ||
} | ||
|
||
/** | ||
* Проверка символа на правило замены в зависимости от следующих символов. | ||
* | ||
* @param text Текст. | ||
* @param position Позиция символа в тексте. | ||
* @return Значение для замены или {@code null}, если замена символа не требуется. | ||
*/ | ||
@Nullable | ||
private String checkRulesNext(@NotNull final String text, final int position) | ||
{ | ||
Objects.requireNonNull(text); | ||
|
||
for (Map.Entry<String, String> entry : standard.getSchema().getRulesNext().entrySet()) | ||
{ | ||
int countSymbolsRules = entry.getKey().length(); | ||
|
||
if ((position + countSymbolsRules) > text.length()) { | ||
continue; | ||
} | ||
|
||
String nextSymbols = text.substring(position, position + countSymbolsRules).toLowerCase(); | ||
|
||
return standard.getSchema().getRulesNext().get(nextSymbols); | ||
} | ||
|
||
return null; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/dev/kalenchukov/transliteration/Transliterating.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright © 2022 Алексей Каленчуков | ||
* GitHub: https://github.com/kalenchukov | ||
* E-mail: mailto:[email protected] | ||
*/ | ||
|
||
package dev.kalenchukov.transliteration; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* Интерфейс для реализации классов транслитерации. | ||
*/ | ||
public interface Transliterating | ||
{ | ||
/** | ||
* Выполняет транслитерацию текста. | ||
* | ||
* @param text Текст. | ||
* @return Транслитерированный текст. | ||
*/ | ||
@NotNull | ||
String translate(@NotNull String text); | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/dev/kalenchukov/transliteration/Transliteration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright © 2022 Алексей Каленчуков | ||
* GitHub: https://github.com/kalenchukov | ||
* E-mail: mailto:[email protected] | ||
*/ | ||
|
||
package dev.kalenchukov.transliteration; | ||
|
||
import dev.kalenchukov.transliteration.resources.Standard; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Класс транслитерации. | ||
*/ | ||
public class Transliteration extends AbstractTransliteration | ||
{ | ||
/** | ||
* @see AbstractTransliteration#AbstractTransliteration(Standard) | ||
*/ | ||
public Transliteration(@NotNull final Standard standard) | ||
{ | ||
super(Objects.requireNonNull(standard)); | ||
} | ||
} |
Oops, something went wrong.