Very experimental changes with clipboard_translator.sh, now requires sqlite3 and perl.

This commit is contained in:
stormdragon2976 2022-12-05 20:07:17 -05:00
parent 0f6138151f
commit 6d853ff27f
2 changed files with 60 additions and 34 deletions

View File

@ -739,8 +739,11 @@ game_launcher() {
if [[ "$game" =~ laser-breakout ]]; then if [[ "$game" =~ laser-breakout ]]; then
pgrep -u "$USER" nvda2speechd &> /dev/null || ${XDG_DATA_HOME:-$HOME/.local/share}/audiogame-manager/nvda2speechd & pgrep -u "$USER" nvda2speechd &> /dev/null || ${XDG_DATA_HOME:-$HOME/.local/share}/audiogame-manager/nvda2speechd &
fi fi
if [[ "$game" =~ ^bokurano-daibouken\||bokurano-daibouken-2\| ]]; then if [[ "$game" =~ ^bokurano-daibouken-2\| ]]; then
"${0%/*}/speech/clipboard_translator.sh" play.exe & "${0%/*}/speech/clipboard_translator.sh" play.exe bokurano-daibouken2 &
fi
if [[ "$game" =~ ^bokurano-daibouken\| ]]; then
"${0%/*}/speech/clipboard_translator.sh" play.exe bokurano-daibouken &
fi fi
if [[ "$game" =~ shadow-line ]]; then if [[ "$game" =~ shadow-line ]]; then
find "${WINEPREFIX}/drive_c/" -type f -name 'nvdaControllerClient.dll' -exec rm -v "{}" \; find "${WINEPREFIX}/drive_c/" -type f -name 'nvdaControllerClient.dll' -exec rm -v "{}" \;

View File

@ -1,54 +1,77 @@
#!/usr/bin/env bash #!/usr/bin/env bash
cleanup() { # Modified from the script at:
sort -uo "$dictionaryFile" "$dictionaryFile" # https://gist.github.com/fdietze/6768a0970d7d732b7fbd7930ccceee2a
}
trap cleanup EXIT set -Eeuo pipefail # https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/#:~:text=set%20%2Du,is%20often%20highly%20desirable%20behavior.
shopt -s expand_aliases
# Clear the clipboard so you don't accidently send personal info to the translator.
echo "" | xclip -d "${DISPLAY:-:0}" -selection clipboard 2> /dev/null
export dictionaryFile="${XDG_CONFIG_HOME:-$HOME/.config}/storm-games/audiogame-manager/translations.txt"
if [[ ! -r "$dictionaryFile" ]]; then
touch "$dictionaryFile"
fi
if [[ $# -ne 1 ]]; then if [[ $# -ne 2 ]]; then
echo "Usage: $0 application name." echo "Usage: $0 \"application name\" \"file name\"."
exit 1
fi fi
# Wait for the application to start # Wait for the application to start
while ! pgrep -u "$USER" ^$1 &> /dev/null ; do while ! pgrep -u "$USER" ^$1 &> /dev/null ; do
sleep 0.05 sleep 0.05
done done
fileName="${2,,}"
fileName="${fileName//[[:space:]]/-}.sqlite"
translationFile="${XDG_CACHE_HOME:-$HOME/.cache}/audiogame-manager/${fileName}"
# Read so long as the application is running # Read so long as the application is running
while pgrep -u "$USER" ^$1 &> /dev/null ; do while pgrep -u "$USER" ^$1 &> /dev/null ; do
newText="" sleep 0.05
translatedText="" text="$(xclip -d "${DISPLAY:-:0}" -selection clipboard -o 2> /dev/null)"
newText="$(xclip -d "${DISPLAY:-:0}" -selection clipboard -o 2> /dev/null)" if [[ "${text}" =~ ^[0-9A-Za-z[:space:][:punct:]]+$ ]]; then
sleep 0.01 spd-say -- "$text"
if [[ "${#newText}" -lt 1 ]]; then
continue
fi
if [[ "${newText}" =~ ^[0-9A-Za-z[:space:][:punct:]]+$ ]]; then
spd-say -- "$newText"
echo "" | xclip -d "${DISPLAY:-:0}" -selection clipboard 2> /dev/null echo "" | xclip -d "${DISPLAY:-:0}" -selection clipboard 2> /dev/null
continue continue
fi fi
export newText if [[ -z "$text" ]]; then
translatedText="$(awk -v originalText="${newText})==:" 'BEGIN { exitCode = 1; FS = "\\)==:\\[" }
$0~originalText { print $2; exitCode = 0; exit}
END { exit exitCode }' "$dictionaryFile")"
if [[ "${#translatedText}" -ge 1 ]]; then
spd-say -- "$translatedText"
echo "" | xclip -d "${DISPLAY:-:0}" -selection clipboard 2> /dev/null
continue continue
fi fi
translatedText="$(trans -no-autocorrect -b -- "$newText")" q() { sqlite3 -column -init "" "$translationFile" "$@" 2> /dev/null ; }
if ! [[ "${newText}" =~ ^[0-9[:punct:]]+$ ]]; then
echo "${newText})==:[${translatedText}" >> "$dictionaryFile"
if [[ ! -s "$translationFile" ]]; then
rm -f "$translationFile"
cat << EOF | sqlite3 -init "" "$translationFile" 2> /dev/null
.bail on
CREATE TABLE translations(
text TEXT NOT NULL,
translation TEXT NOT NULL,
PRIMARY KEY (text)
);
CREATE INDEX translations_text_idx ON translations (text);
EOF
fi fi
spd-say -- "$translatedText"
# https://en.wikipedia.org/wiki/Unicode_equivalence#Combining_and_precomposed_characters
# https://www.effectiveperlprogramming.com/2011/09/normalize-your-perl-source/
alias nfc="perl -MUnicode::Normalize -CS -ne 'print NFC(\$_)'" # composed characters
# Normalize different unicode space characters to the same space
# https://stackoverflow.com/a/43640405
alias normalize_spaces="perl -CSDA -plE 's/[^\\S\\t]/ /g'"
alias normalize_unicode="normalize_spaces | nfc"
textEscaped="$(echo "$text" | sed "s/'/''/g" | normalize_unicode)" # escape single quotes for sqlite
translated="$(q "SELECT translation FROM translations WHERE text = '$textEscaped' LIMIT 1" | sed 's/\s*$//')"
if [[ -z "$translated" ]]; then
translated="$(trans -no-autocorrect -no-warn -brief "$text" | head -1 | sed 's/\s*$//' | normalize_unicode)"
if [[ -n "$translated" ]]; then
translatedEscaped="$(echo "$translated" | sed "s/'/''/g")"
q "INSERT OR IGNORE INTO translations (text, translation) VALUES ('$textEscaped', '$translatedEscaped')"
fi
fi
spd-say -- "$translated"
echo "" | xclip -d "${DISPLAY:-:0}" -selection clipboard 2> /dev/null echo "" | xclip -d "${DISPLAY:-:0}" -selection clipboard 2> /dev/null
done done