Skip to content

Commit

Permalink
Add name capitalization
Browse files Browse the repository at this point in the history
  • Loading branch information
max1mde committed Nov 3, 2024
1 parent d7fc990 commit eed17e3
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 67 deletions.
2 changes: 0 additions & 2 deletions src/assets/lists/german_nouns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,6 @@ Döner
Dörfer
Dünndarm
Dünndärme
E-Mail
E-Mails
Echtzeit
Echtzeiten
Ehe
Expand Down
46 changes: 5 additions & 41 deletions src/assets/lists/names → src/assets/lists/names.txt
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,6 @@ Ensar
Enya
Enzo
Epic Games
Equatorial Guinea
Erbil
Eren
Eric
Expand All @@ -725,14 +724,12 @@ Eritrea
Erkan
Ermolaev
Erna
Ernst & Young
Erwin
Esfahan
Esila
Eslem
Esma
Esra
Estación Central
Esther
Estland
Estonia
Expand Down Expand Up @@ -767,7 +764,6 @@ Falklandinseln
Falko
Farfetch
Faroe Islands
Fashion Nova
Fatima
Fatma
Fedor
Expand Down Expand Up @@ -828,10 +824,7 @@ Französisch-Polynesien
Frauke
Fred
Frederik
Free People
Freetown
French Guiana
French Polynesia
FreshBooks
Freshly
Freya
Expand Down Expand Up @@ -861,8 +854,6 @@ Gavin
Gavrilov
Gebhardt
Geiger
General Dynamics
General Motors
Genesis
Geneva
Genoa
Expand Down Expand Up @@ -892,7 +883,6 @@ Gleb
Gloria
GoDaddy
Gojek
Goldman Sachs
Golovin
Google
Gordon
Expand All @@ -903,7 +893,6 @@ Grab
Grace
Graf
Grailed
Grand Rapids
Graz
Greece
Greenland
Expand All @@ -918,7 +907,6 @@ Grubhub
Grönland
Guadeloupe
Guatemala
Guatemala City
Guayaquil
Gucci
Gudrun
Expand Down Expand Up @@ -1007,16 +995,11 @@ Hohhot
Holger
Hollister
Holly
Home Depot
Honda
Honduras
Honeywell
Hong Kong
Hongkong
Hope
Horn
Horst
Hot Topic
Houston
HP
HSBC
Expand Down Expand Up @@ -1054,7 +1037,6 @@ Ilyin
Iman
Imani
Imke
Imperial Brands
Imran
Ina
Inaya
Expand Down Expand Up @@ -1093,7 +1075,6 @@ Isabella
Isabelle
Isaiah
Island
Isle of Man
Ismail
Israel
Issa
Expand Down Expand Up @@ -1179,7 +1160,6 @@ Johanne
Johannes
Johannesburg
John
Johnson & Johnson
Joleen
Jolie
Jolina
Expand Down Expand Up @@ -1207,7 +1187,6 @@ Joshua
Josie
Josua
José
JPMorgan Chase
Juan
Judith
Jule
Expand All @@ -1221,7 +1200,6 @@ Juna
Jung
Junis
Juri
Just Eat
Justin
Justus
Jutta
Expand All @@ -1244,12 +1222,10 @@ Kamerun
Kanada
Kanchanaburi
Kanpur
Kap Verde
Karachi
Karam
Karan
Karen
Karibische Niederlande
Karim
Karin
Karl
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1441,7 +1416,6 @@ Letizia
Lettland
Lev
Levi
Levi Strauss & Co.
Levi's
Levin
Levio
Expand All @@ -1458,8 +1432,6 @@ Liara
Lias
Libanon
Liberia
Libertador General Bernardo O'Higgins Region
Libertador General Bernardo O'Higgins-Region
Libya
Libyen
Lidiya
Expand Down Expand Up @@ -1492,7 +1464,6 @@ Lisbeth
Lisbon
Litauen
Lithuania
Little Rock
Litvinov
Liv
Liva
Expand All @@ -1501,7 +1472,6 @@ Liya
Liyana
Liz
Ljubljana
Lockheed Martin
Logan
Loginov
Loki
Expand All @@ -1516,8 +1486,7 @@ Loretta
Lorik
Loris
Los Angeles
Los Lagos Region
Los Lagos-Region
Los Lagos
Lotta
Lotte
Lou
Expand Down Expand Up @@ -1565,8 +1534,6 @@ Madlen
Madrid
Madurai
Mael
Magallanes Region
Magallanes-Region
Magdalena
Magnus
Mahdi
Expand Down Expand Up @@ -1771,7 +1738,6 @@ Mirko
Miro
Miron
Mironov
Mitsubishi Electric
Moderna
Mohammed
Mohr
Expand Down Expand Up @@ -1840,6 +1806,7 @@ Nazarov
Nea
Nefes
Neiman Marcus
Montana Black
Nela
Nele
Nelia
Expand Down Expand Up @@ -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
Expand Down
48 changes: 40 additions & 8 deletions src/autocorrect_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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')
Expand All @@ -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(
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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):
Expand All @@ -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={
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit eed17e3

Please sign in to comment.