From f24225b1804b47b355943b73ee66a290524accfa Mon Sep 17 00:00:00 2001 From: Waleed Mortaja Date: Sat, 26 Mar 2022 23:01:21 +0200 Subject: [PATCH] v1.0 --- .gitignore | 91 ++++++++++++++++---- .idea/.gitignore | 3 + .idea/artifacts/RtlSubtitleCorrector_jar.xml | 8 ++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 ++ README.md | 20 +++++ RtlSubtitleCorrector.iml | 11 +++ src/META-INF/MANIFEST.MF | 3 + src/Main.java | 59 +++++++++++++ src/RtlSubtitleCorrector.java | 26 ++++++ src/Util.java | 12 +++ 12 files changed, 235 insertions(+), 18 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/artifacts/RtlSubtitleCorrector_jar.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 README.md create mode 100644 RtlSubtitleCorrector.iml create mode 100644 src/META-INF/MANIFEST.MF create mode 100644 src/Main.java create mode 100644 src/RtlSubtitleCorrector.java create mode 100644 src/Util.java diff --git a/.gitignore b/.gitignore index a1c2a23..8f4e780 100644 --- a/.gitignore +++ b/.gitignore @@ -1,23 +1,78 @@ -# Compiled class file -*.class +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 -# Log file -*.log +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf -# BlueJ files -*.ctxt +# AWS User-specific +.idea/**/aws.xml -# Mobile Tools for Java (J2ME) -.mtj.tmp/ +# Generated files +.idea/**/contentModel.xml -# Package Files # -*.jar -*.war -*.nar -*.ear -*.zip -*.tar.gz -*.rar +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# SonarLint plugin +.idea/sonarlint/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/artifacts/RtlSubtitleCorrector_jar.xml b/.idea/artifacts/RtlSubtitleCorrector_jar.xml new file mode 100644 index 0000000..e82b17e --- /dev/null +++ b/.idea/artifacts/RtlSubtitleCorrector_jar.xml @@ -0,0 +1,8 @@ + + + $PROJECT_DIR$/out/artifacts/RtlSubtitleCorrector_jar + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..e0844bc --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..d355c2c --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..8f10c4a --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# RtlSubtitleCorrector +##About +When writing Right-to-Left content inside Left-To-Right context, it is usually mis-represented. +This happens specially with subtitles. +This project fixes this issue by inserting a Right-To-Left isolation character `\u2067` before and after every Left-To-Right sentences. + +## Usage +Open cmd or terminal. Run the `jar` file followed by file path: + + RtlSubtitleCorrector.jar mysubtitle.srt +Note: You can enter multiple files at once. + + RtlSubtitleCorrector.jar mysubtitle1.srt mysubtitle2.srt +For linux Systems, You can use it as: + + ./RtlSubtitleCorrector.jar mysubtitle.srt + +# See also +- [Wikipedia Bidirectional_text](https://en.wikipedia.org/wiki/Bidirectional_text) +- [A similar project](https://github.com/Majid110/SubtitleRtlCorrector) \ No newline at end of file diff --git a/RtlSubtitleCorrector.iml b/RtlSubtitleCorrector.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/RtlSubtitleCorrector.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/META-INF/MANIFEST.MF b/src/META-INF/MANIFEST.MF new file mode 100644 index 0000000..37197ef --- /dev/null +++ b/src/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Main + diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..90958ab --- /dev/null +++ b/src/Main.java @@ -0,0 +1,59 @@ +import java.io.IOException; +import java.io.PrintStream; +import java.nio.file.Files; +import java.nio.file.Path; + +public class Main { + public static void main(String[] args) { + if (args.length == 0) { + printHelp(System.out); + System.exit(0); + } + + for (String inputFilePathString : args) { + Path currentWorkingDirectory = Path.of(System.getProperty("user.dir")); + Path inputFilePath = Path.of(inputFilePathString); + inputFilePath = currentWorkingDirectory.resolve(inputFilePath); + + String fileContent = null; + try { + fileContent = Files.readString(inputFilePath); + } catch (IOException e) { + System.out.println("ERROR: Could not read file!"); + System.exit(1); + } + + String[] inputFileNameAndExtension = Util.separateFileNameAndExtension(inputFilePath.getFileName().toString()); + + String fixedFileContent = RtlSubtitleCorrector.correctRtl(fileContent, inputFileNameAndExtension[1]); + + if (inputFileNameAndExtension[1] == null) { + inputFileNameAndExtension[1] = "txt"; + } + + String outputParent = inputFilePath.getParent().toString(); + String outputFileName = inputFileNameAndExtension[0] + ".RTL." + inputFileNameAndExtension[1]; + + try { + Files.writeString(Path.of(outputParent, outputFileName), fixedFileContent); + } catch (IOException e) { + System.out.println("ERROR: Could not write file!"); + System.exit(1); + } + } + + } + + private static void printHelp(PrintStream out) { + out.println("Please specify the input file path\n" + + "Example:\n" + + "RtlSubtitleCorrector.jar mysubtitle.srt\n\n" + + "Note: You can enter multiple files at once\n" + + "Example:\n" + + "RtlSubtitleCorrector.jar mysubtitle1.srt mysubtitle2.srt\n\n" + + "For linux Systems, You can use it as:\n" + + "./RtlSubtitleCorrector.jar mysubtitle.srt"); + } + + +} diff --git a/src/RtlSubtitleCorrector.java b/src/RtlSubtitleCorrector.java new file mode 100644 index 0000000..506d387 --- /dev/null +++ b/src/RtlSubtitleCorrector.java @@ -0,0 +1,26 @@ +public class RtlSubtitleCorrector { + private final static char RTL_CHAR = '\u2067'; + private final static String LTR_REGEX = "[\\p{IsLatin}\\p{P}]+"; + + public static String correctRtl(String text, String fileExtension) { + if (fileExtension == null) { + return defaultRtlCorrection(text); + } + + String fixedText = null; + switch (fileExtension) { + case "ssa": + fixedText = text.replaceAll("(\\\\[nN])", RTL_CHAR + "$1" + RTL_CHAR); + fixedText = fixedText.replaceAll("(Dialogue: (?:[^,]*,){9})(.*)", "$1" + RTL_CHAR + "$2"); + break; + default: + fixedText = defaultRtlCorrection(text); + break; + } + return fixedText; + } + + private static String defaultRtlCorrection(String text) { + return text.replaceAll("(" + LTR_REGEX + ")", RTL_CHAR + "$1" + RTL_CHAR); + } +} diff --git a/src/Util.java b/src/Util.java new file mode 100644 index 0000000..6852134 --- /dev/null +++ b/src/Util.java @@ -0,0 +1,12 @@ +public class Util { + public static String[] separateFileNameAndExtension(String fileName) { + int dotIndex = fileName.lastIndexOf('.'); + if (dotIndex > 0) { + String fileNameWithoutExtension = fileName.substring(0, dotIndex); + String extension = fileName.substring(dotIndex + 1); + return new String[]{fileNameWithoutExtension, extension}; + } + + return new String[]{fileName, null}; + } +}