From eed17e3b3331b6ba28df094feaaaf6ea25e9180e Mon Sep 17 00:00:00 2001 From: Maxim Date: Sun, 3 Nov 2024 18:29:01 +0200 Subject: [PATCH] Add name capitalization --- src/assets/lists/german_nouns.txt | 2 -- src/assets/lists/{names => names.txt} | 46 +++---------------------- src/autocorrect_service.py | 48 ++++++++++++++++++++++----- src/settings_manager.py | 21 ++++++------ src/ui/settings_window.py | 12 +++---- 5 files changed, 62 insertions(+), 67 deletions(-) rename src/assets/lists/{names => names.txt} (97%) diff --git a/src/assets/lists/german_nouns.txt b/src/assets/lists/german_nouns.txt index 631195e..fdaebc4 100644 --- a/src/assets/lists/german_nouns.txt +++ b/src/assets/lists/german_nouns.txt @@ -600,8 +600,6 @@ Döner Dörfer Dünndarm Dünndärme -E-Mail -E-Mails Echtzeit Echtzeiten Ehe diff --git a/src/assets/lists/names b/src/assets/lists/names.txt similarity index 97% rename from src/assets/lists/names rename to src/assets/lists/names.txt index 78ac413..5b4f52e 100644 --- a/src/assets/lists/names +++ b/src/assets/lists/names.txt @@ -714,7 +714,6 @@ Ensar Enya Enzo Epic Games -Equatorial Guinea Erbil Eren Eric @@ -725,14 +724,12 @@ Eritrea Erkan Ermolaev Erna -Ernst & Young Erwin Esfahan Esila Eslem Esma Esra -Estación Central Esther Estland Estonia @@ -767,7 +764,6 @@ Falklandinseln Falko Farfetch Faroe Islands -Fashion Nova Fatima Fatma Fedor @@ -828,10 +824,7 @@ Französisch-Polynesien Frauke Fred Frederik -Free People Freetown -French Guiana -French Polynesia FreshBooks Freshly Freya @@ -861,8 +854,6 @@ Gavin Gavrilov Gebhardt Geiger -General Dynamics -General Motors Genesis Geneva Genoa @@ -892,7 +883,6 @@ Gleb Gloria GoDaddy Gojek -Goldman Sachs Golovin Google Gordon @@ -903,7 +893,6 @@ Grab Grace Graf Grailed -Grand Rapids Graz Greece Greenland @@ -918,7 +907,6 @@ Grubhub Grönland Guadeloupe Guatemala -Guatemala City Guayaquil Gucci Gudrun @@ -1007,16 +995,11 @@ Hohhot Holger Hollister Holly -Home Depot Honda Honduras Honeywell Hong Kong Hongkong -Hope -Horn -Horst -Hot Topic Houston HP HSBC @@ -1054,7 +1037,6 @@ Ilyin Iman Imani Imke -Imperial Brands Imran Ina Inaya @@ -1093,7 +1075,6 @@ Isabella Isabelle Isaiah Island -Isle of Man Ismail Israel Issa @@ -1179,7 +1160,6 @@ Johanne Johannes Johannesburg John -Johnson & Johnson Joleen Jolie Jolina @@ -1207,7 +1187,6 @@ Joshua Josie Josua José -JPMorgan Chase Juan Judith Jule @@ -1221,7 +1200,6 @@ Juna Jung Junis Juri -Just Eat Justin Justus Jutta @@ -1244,12 +1222,10 @@ Kamerun Kanada Kanchanaburi Kanpur -Kap Verde Karachi Karam Karan Karen -Karibische Niederlande Karim Karin Karl @@ -1329,14 +1305,13 @@ Kolosov Kolumbien Komoren Konami -Kongo, Demokratische Republik -Kongo, Republik Konovalov Konrad Konstantin Korbinian -Korea, North -Korea, South +Korea +North Korea +South Korea Korolev Kosovo Kozlov @@ -1441,7 +1416,6 @@ Letizia Lettland Lev Levi -Levi Strauss & Co. Levi's Levin Levio @@ -1458,8 +1432,6 @@ Liara Lias Libanon Liberia -Libertador General Bernardo O'Higgins Region -Libertador General Bernardo O'Higgins-Region Libya Libyen Lidiya @@ -1492,7 +1464,6 @@ Lisbeth Lisbon Litauen Lithuania -Little Rock Litvinov Liv Liva @@ -1501,7 +1472,6 @@ Liya Liyana Liz Ljubljana -Lockheed Martin Logan Loginov Loki @@ -1516,8 +1486,7 @@ Loretta Lorik Loris Los Angeles -Los Lagos Region -Los Lagos-Region +Los Lagos Lotta Lotte Lou @@ -1565,8 +1534,6 @@ Madlen Madrid Madurai Mael -Magallanes Region -Magallanes-Region Magdalena Magnus Mahdi @@ -1771,7 +1738,6 @@ Mirko Miro Miron Mironov -Mitsubishi Electric Moderna Mohammed Mohr @@ -1840,6 +1806,7 @@ Nazarov Nea Nefes Neiman Marcus +Montana Black Nela Nele Nelia @@ -2103,15 +2070,12 @@ Razin Rebag Rebecca Recife -Red Hat -Red Wing Shoes Reebok Reformation Regina Rejan Renate Renca -Rent the Runway René Reuter Reykjavik diff --git a/src/autocorrect_service.py b/src/autocorrect_service.py index 97cf28c..f4e2fd5 100644 --- a/src/autocorrect_service.py +++ b/src/autocorrect_service.py @@ -211,13 +211,16 @@ def __init__(self, settings_manager): def get_selected_text(self): try: - controller.release(Key.alt) pyperclip.copy('') time.sleep(0.1) + if self.settings.get_setting('auto_select_text'): + with controller.pressed(Key.ctrl.value): + controller.press('a') + controller.release('a') + time.sleep(0.1) with controller.pressed(Key.ctrl.value): controller.press('c') controller.release('c') - controller.press(Key.alt) time.sleep(0.4) selected_text = pyperclip.paste() logging.info(f"Selected text: {selected_text}") @@ -230,8 +233,12 @@ def replace_selected_text(self, new_text): try: pyperclip.copy(new_text) - controller.release(Key.alt) time.sleep(0.1) + if self.settings.get_setting('auto_select_text'): + with controller.pressed(Key.ctrl.value): + controller.press('a') + controller.release('a') + time.sleep(0.1) with controller.pressed(Key.ctrl.value): controller.press('v') controller.release('v') @@ -246,6 +253,10 @@ def handle_rephrase_hotkey(self): if not selected_text: return + if self.settings.get_setting('fix.use_replacements'): + for old, new in self.settings.get_setting('replacements').items(): + selected_text = re.sub(rf'\b{re.escape(old)}\b', new, selected_text) + prompt = self.settings.get_setting('rephrase.prompt') + selected_text response = requests.post( @@ -283,13 +294,18 @@ def fix_text(self): if not selected_text or selected_text.strip() == "": return - for old, new in self.settings.get_setting('replacements').items(): - selected_text = re.sub(rf'\b{re.escape(old)}\b', new, selected_text) + if self.settings.get_setting('fix.use_replacements'): + for old, new in self.settings.get_setting('replacements').items(): + selected_text = re.sub(rf'\b{re.escape(old)}\b', new, selected_text) if self.settings.get_setting('fix.german_noun_capitalization'): words = selected_text.split() selected_text = ' '.join([self.noun_capitalization(w) for w in words]) + if self.settings.get_setting('fix.name_capitalization'): + words = selected_text.split() + selected_text = ' '.join([self.name_capitalization(w) for w in words]) + if self.settings.get_setting('fix.punctuate'): selected_text = self.auto_punctuate(selected_text) @@ -299,7 +315,6 @@ def fix_text(self): self.replace_selected_text(selected_text) def get_custom_prompt(self, selected_text): - controller.release(Key.alt) show_suggestions = selected_text is not None and selected_text.strip() != "" dialog = CustomPromptDialog(last_prompt=self.last_prompt, show_suggestions=show_suggestions) @@ -363,11 +378,13 @@ def switch_phrasings(self): def handle_custom_prompt_hotkey(self): if not self.enabled: return - controller.release(Key.alt) time.sleep(0.1) selected_text = self.get_selected_text() if not selected_text: selected_text = "" + if self.settings.get_setting('fix.use_replacements'): + for old, new in self.settings.get_setting('replacements').items(): + selected_text = re.sub(rf'\b{re.escape(old)}\b', new, selected_text) self.worker.handle_custom_prompt(selected_text) def handle_translation_hotkey(self): @@ -377,6 +394,11 @@ def handle_translation_hotkey(self): selected_text = self.get_selected_text() if not selected_text or selected_text.strip() == "": return + + if self.settings.get_setting('fix.use_replacements'): + for old, new in self.settings.get_setting('replacements').items(): + selected_text = re.sub(rf'\b{re.escape(old)}\b', new, selected_text) + response = requests.post( url="https://openrouter.ai/api/v1/chat/completions", headers={ @@ -406,13 +428,23 @@ def handle_translation_hotkey(self): def noun_capitalization(self, word): return word.capitalize() if self.is_german_noun(word) else word + def name_capitalization(self, word): + return word.capitalize() if self.is_name(word) else word + def auto_punctuate(self, text): if text.strip() and text.strip()[-1] not in '.!?': return text.rstrip() + '.' return text def is_german_noun(self, word): - return False + with open('./assets/lists/german_nouns.txt', 'r', encoding='utf-8') as file: + german_nouns = file.read().splitlines() + return word.lower() in (noun.lower() for noun in german_nouns) + + def is_name(self, word): + with open('./assets/lists/names.txt', 'r', encoding='utf-8') as file: + german_nouns = file.read().splitlines() + return word.lower() in (noun.lower() for noun in german_nouns) def is_proper_name(self, word): # TODO diff --git a/src/settings_manager.py b/src/settings_manager.py index c1421e9..4c12af0 100644 --- a/src/settings_manager.py +++ b/src/settings_manager.py @@ -36,22 +36,22 @@ def reset_settings(self): 'fix.capitalization': True, 'fix.punctuate': False, 'fix.german_noun_capitalization': True, + 'fix.name_capitalization': True, 'fix.use_replacements': True, 'fix.hotkey': 'Ctrl+F8', 'fix.auto_fix_on_send': True, 'rephrase.hotkey': 'Ctrl+F9', 'rephrase.switch_phrasings_hotkey': 'Ctrl+F10', 'rephrase.use_replacements': True, - 'rephrase.prompt': 'You are a helpful writing assistant. Provide 3 different concise rephrasing of ' - 'the given text, separated by | characters. Please provide 3 alternative ' - 'phrasings for the following text (The first one just spelling and punctuation ' - 'fixes no different words, only the next phrases should contain alternative words) ' - '(if the text is longer than 20 words only one alternative phrasing):', + 'rephrase.prompt': 'Provide 3 different concise rephrasing of ' + 'the given text, separated by | characters. ' + 'The first rephrasing should not be rephrased just correct all spelling, capitalization, grammar, and punctuation errors without changing wording.' + '(If the text is longer than 30 words, only fix the text. Also keep the same language in all rephrasings). Here is the text:', 'translate.hotkey': 'Ctrl+F11', 'translate.use_replacements': True, 'translate.alternative_language': 'german', - 'translate.prompt': 'You are a basic translater. Add punctuation and use correct spelling. ' + 'translate.prompt': 'You are a basic translater. (also additionally add punctuation and use correct spelling.) ' 'Translate the following text to English if it is in any other language than ' 'English, else translate it to %alternative_language% and ONLY answer with the ' 'translated message:', @@ -59,7 +59,7 @@ def reset_settings(self): 'custom_prompt.hotkey': 'Ctrl+F12', 'custom_prompt.use_replacements': True, 'custom_prompt.auto_custom_prompt': True, - 'custom_prompt.auto_select_text': True, + 'auto_select_text': False, 'open_router_key': '', 'replacements': { 'i': 'I', @@ -171,11 +171,10 @@ def reset_settings(self): "woulda": "would have", "shoulda": "should have", "d'you": "do you", - "ain't": "is not", + "aint": "ain't", "y'all": "you all", "y'gotta": "you have got to", "tryna": "trying to", - "em": "them", "let's": "let us", "here's": "here is", "that's": "that is", @@ -317,7 +316,9 @@ def reset_settings(self): "yessir": "yes sir", "yes'm": "yes madam", "cause": "because", - "yk": "you know" + "yk": "you know", + "ki": "KI", + "ai": "AI" } } self.save_settings() \ No newline at end of file diff --git a/src/ui/settings_window.py b/src/ui/settings_window.py index ec472ba..ce0892b 100644 --- a/src/ui/settings_window.py +++ b/src/ui/settings_window.py @@ -48,14 +48,14 @@ def setup_ui(self): self.capitalization = QCheckBox("Capitalization") self.punctuate = QCheckBox("Punctuation") self.german_noun_capitalization = QCheckBox("German Noun Capitalization") + self.name_capitalization = QCheckBox("Name Capitalization") self.use_replacements = QCheckBox("Use Replacements") - self.auto_fix_on_send = QCheckBox("Auto Fix on Send") self.hotkey_input = QKeySequenceEdit() hotkey_label = QLabel("Fix Hotkey:") - for widget in [self.capitalization, self.punctuate, self.german_noun_capitalization, - self.use_replacements, self.auto_fix_on_send]: + for widget in [self.capitalization, self.punctuate, self.german_noun_capitalization, self.name_capitalization, + self.use_replacements]: widget.stateChanged.connect(self.mark_modified) layout.addWidget(widget) @@ -70,16 +70,16 @@ def load_settings(self): self.capitalization.setChecked(self.settings.get_setting('fix.capitalization', True)) self.punctuate.setChecked(self.settings.get_setting('fix.punctuate', True)) self.german_noun_capitalization.setChecked(self.settings.get_setting('fix.german_noun_capitalization', True)) + self.name_capitalization.setChecked(self.settings.get_setting('fix.name_capitalization', True)) self.use_replacements.setChecked(self.settings.get_setting('fix.use_replacements', True)) - self.auto_fix_on_send.setChecked(self.settings.get_setting('fix.auto_fix_on_send', True)) self.hotkey_input.setKeySequence(QKeySequence(self.settings.get_setting('fix.hotkey', 'Ctrl+F8'))) def save_settings(self): self.settings.set_setting('fix.capitalization', self.capitalization.isChecked()) self.settings.set_setting('fix.punctuate', self.punctuate.isChecked()) self.settings.set_setting('fix.german_noun_capitalization', self.german_noun_capitalization.isChecked()) + self.settings.set_setting('fix.name_capitalization', self.name_capitalization.isChecked()) self.settings.set_setting('fix.use_replacements', self.use_replacements.isChecked()) - self.settings.set_setting('fix.auto_fix_on_send', self.auto_fix_on_send.isChecked()) self.settings.set_setting('fix.hotkey', self.hotkey_input.keySequence().toString()) self.settings_modified = False self.close() @@ -216,7 +216,7 @@ def setup_ui(self): layout = QVBoxLayout(self) self.use_replacements = QCheckBox("Use Replacements") - self.auto_custom_prompt = QCheckBox("Auto Custom Prompt") + self.auto_custom_prompt = QCheckBox("Auto Prompt When Typing") for widget in [self.use_replacements, self.auto_custom_prompt]: widget.stateChanged.connect(self.mark_modified)