Skip to content

Commit

Permalink
make white space removal optional
Browse files Browse the repository at this point in the history
  • Loading branch information
mathewthe2 committed May 11, 2021
1 parent 079aea2 commit bf417f0
Showing 7 changed files with 44 additions and 10 deletions.
1 change: 1 addition & 0 deletions config.ini
Original file line number Diff line number Diff line change
@@ -47,6 +47,7 @@ gamescriptfile =

[TEXTHOOKERCONFIG]
remove_repeat = true
remove_spaces = true

[WINDOWS_HOTKEYS]
refresh_ocr = <ctrl>+q
10 changes: 7 additions & 3 deletions game2text.py
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
from ocr import detect_and_log
from translate import multi_translate
from hotkeys import hotkey_map
from util import RepeatedTimer, open_folder_by_relative_path, create_directory_if_not_exists, get_default_browser_name, get_PID_list, remove_repeat_phrases
from util import RepeatedTimer, open_folder_by_relative_path, create_directory_if_not_exists, get_default_browser_name, get_PID_list, remove_repeat_phrases, remove_spaces
from textractor import Textractor
from tools import path_to_textractor, open_folder_textractor_path
from audio import get_recommended_device_index, get_audio_objects
@@ -199,10 +199,14 @@ def hook_code(code, pids):
return 'Error: failed to hook code'

def monitor_textractor(output_objects):
if r_config(TEXTHOOKER_CONFIG, 'remove_repeat') == 'true':
is_remove_repeat = r_config(TEXTHOOKER_CONFIG, 'remove_repeat') == 'true'
is_remove_spaces = r_config(TEXTHOOKER_CONFIG, 'remove_spaces') == 'true'

if is_remove_repeat or is_remove_spaces:
for output in output_objects:
output['text'] = output['text'].strip()
output['text'] = remove_repeat_phrases(output['text'])
output['text'] = remove_repeat_phrases(output['text']) if is_remove_repeat else output['text']
output['text'] = remove_spaces(output['text']) if is_remove_spaces else output['text']

eel.textractorPipe(output_objects)

3 changes: 3 additions & 0 deletions util.py
Original file line number Diff line number Diff line change
@@ -106,6 +106,9 @@ def remove_repeat_characters(s):
i = (s+s).find(s, 1, -1)
return s if i == -1 else s[:i]

def remove_spaces(s):
return "".join(s.split())

# TODO: use algorithm in textractor
def remove_repeat_phrases(s):
prefix_array=[]
10 changes: 10 additions & 0 deletions web/index.html
Original file line number Diff line number Diff line change
@@ -621,6 +621,16 @@ <h4 class="mdl-dialog__title">Visual Novel Hooker</h4>
</label>
</span>
</li>
<li class="mdl-list__item">
<span class="mdl-list__item-primary-content">
<span>Remove White Spaces</span>
</span>
<span class="mdl-list__item-secondary-action">
<label style="margin-right: 20px" class="mdl-switch mdl-js-switch mdl-js-ripple-effect" for="removeWhiteSpacesSwitch">
<input type="checkbox" onclick="toggleRemoveWhiteSpacesAndPersist()" id="removeWhiteSpacesSwitch" class="mdl-switch__input" />
</label>
</span>
</li>
</ul>
</div>
</div>
1 change: 1 addition & 0 deletions web/index.js
Original file line number Diff line number Diff line change
@@ -44,6 +44,7 @@ let isResizeAnkiScreenshot = false

// Texthooker
let isRemoveRepeatedSentences = false
let isRemoveWhiteSpaces = false

const videoElement = document.getElementById("video");
// const myImg = document.getElementById("my_img");
15 changes: 15 additions & 0 deletions web/settings.js
Original file line number Diff line number Diff line change
@@ -91,6 +91,7 @@ function initConfig () {
// Texthooker
const texthookerConfig = config[TEXTHOOKER_CONFIG];
initSetRemoveRepeatedSentencesSwitch(texthookerConfig['remove_repeat']);
initSetRemoveWhiteSpacesSwitch(texthookerConfig['remove_spaces']);
initSetTextractorPath();
// Hotkeys
const hotkeyConfig = config[HOTKEY_CONFIG];
@@ -547,6 +548,20 @@ function initSetRemoveRepeatedSentencesSwitch(isRemoveRepeatedSentences) {
document.getElementById("removeRepeatSentencesSwitch").parentElement.MaterialSwitch.on();
}
}
function toggleRemoveWhiteSpaces() {
isRemoveWhiteSpaces = !isRemoveWhiteSpaces;
}
async function toggleRemoveWhiteSpacesAndPersist() {
toggleRemoveWhiteSpaces();
eel.update_config(TEXTHOOKER_CONFIG, {'remove_spaces': isRemoveWhiteSpaces ? 'true' : 'false'})();
}
function initSetRemoveWhiteSpacesSwitch(isRemoveWhiteSpaces) {
if (isRemoveWhiteSpaces === 'true') {
toggleRemoveWhiteSpaces();
document.getElementById("removeWhiteSpacesSwitch").parentElement.MaterialSwitch.on();
}
}

async function initSetTextractorPath() {
const textractorPath = await eel.get_path_to_textractor()();
textractorPathInput.value = textractorPath;
14 changes: 7 additions & 7 deletions web/texthookersettings.js
Original file line number Diff line number Diff line change
@@ -82,7 +82,7 @@ function textractorPipe(textractorOutput) {
}
}
if (currentHook == hook) {
const output = parseText(textractorOutputObject.text)
const output = textractorOutputObject.text;
updateOutput(output)
}
}
@@ -129,9 +129,9 @@ function hasSelectedPID() {
return processPIDs.length > 0;
}

function parseText(text) {
// TODO: add custom regex
text = text.replace(/[\x00-\x20]/g, '')
text = text.replace(/_t.*?\//g, '')
return text
}
// function parseText(text) {
// // TODO: add custom regex
// text = text.replace(/[\x00-\x20]/g, '')
// text = text.replace(/_t.*?\//g, '')
// return text
// }

0 comments on commit bf417f0

Please sign in to comment.