Skip to content

Commit 71917de

Browse files
committed
regex fixes
1 parent e01564f commit 71917de

File tree

1 file changed

+61
-29
lines changed

1 file changed

+61
-29
lines changed

OU Dictionary Editor/OpenUtau_Dictionary_Editor.pyw

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,8 @@ class Dictionary(TkinterDnD.Tk):
16861686
added_symbols = set() # Track added symbols to avoid duplicates
16871687

16881688
for symbol, values in self.symbols.items():
1689+
if ',' in symbol or ' ' in symbol:
1690+
continue
16891691
escaped_symbol = symbol
16901692
type_list = values[0] if isinstance(values[0], list) else [values[0]]
16911693
type_str = ', '.join(f"{t}" for t in type_list)
@@ -1698,36 +1700,50 @@ class Dictionary(TkinterDnD.Tk):
16981700
rename_entries = CommentedSeq()
16991701
added_replacements = set() # Track added replacements to avoid duplicates
17001702
for symbol, data in self.symbols.items():
1701-
if not isinstance(symbol, str):
1702-
continue
17031703
if len(data) > 1:
1704-
from_data = symbol if isinstance(symbol, str) else list(symbol)
1705-
to_entries = data[1:] # Could be one or more replacements
1704+
from_symbol = symbol if not isinstance(symbol, list) else list(symbol)
1705+
to_data_entries = data[1:]
17061706

1707-
for to_data in to_entries:
1708-
if not to_data or (isinstance(to_data, str) and not to_data.strip()):
1709-
continue # Skip empty
1707+
for to_entry in to_data_entries:
1708+
if not to_entry or (isinstance(to_entry, str) and to_entry.strip() == ''):
1709+
continue # Skip empty replacements
17101710

1711-
# Handle comma-separated strings like "O, u"
1712-
if isinstance(to_data, str) and ',' in to_data:
1713-
to_list = [item.strip() for item in to_data.split(',') if item.strip()]
1711+
# Handle comma-separated strings as lists
1712+
if isinstance(to_entry, str) and ',' in to_entry:
1713+
to_list = [item.strip() for item in to_entry.split(',') if item.strip()]
1714+
if not to_list:
1715+
continue
17141716
to_key = tuple(to_list)
17151717
to_yaml_value = to_list
1716-
elif isinstance(to_data, list):
1717-
to_key = tuple(to_data)
1718-
to_yaml_value = to_data
1718+
elif isinstance(to_entry, list):
1719+
if not to_entry:
1720+
continue
1721+
to_key = tuple(to_entry)
1722+
to_yaml_value = to_entry
17191723
else:
1720-
to_key = to_data
1721-
to_yaml_value = to_data
1724+
to_key = to_entry
1725+
to_yaml_value = to_entry
17221726

1723-
# Make from_symbol compatible with list froms later if needed
1724-
from_key = tuple(from_data) if isinstance(from_data, list) else from_data
1725-
replacement_key = (from_key, to_key)
1727+
if isinstance(from_symbol, str) and ',' in from_symbol:
1728+
from_list = [item.strip() for item in from_symbol.split(',') if item.strip()]
1729+
if not from_list:
1730+
continue
1731+
from_key = tuple(from_list)
1732+
from_yaml_value = from_list
1733+
elif isinstance(from_symbol, list):
1734+
if not from_symbol:
1735+
continue
1736+
from_key = tuple(from_symbol)
1737+
from_yaml_value = from_symbol
1738+
else:
1739+
from_key = from_symbol
1740+
from_yaml_value = from_symbol
17261741

1742+
replacement_key = (from_key, to_key)
17271743
if replacement_key not in added_replacements:
17281744
added_replacements.add(replacement_key)
17291745
entry = CommentedMap([
1730-
('from', from_data),
1746+
('from', from_yaml_value),
17311747
('to', to_yaml_value)
17321748
])
17331749
rename_entries.append(entry)
@@ -1840,7 +1856,7 @@ class Dictionary(TkinterDnD.Tk):
18401856
# Normalize rename input
18411857
if isinstance(rename, str):
18421858
# Remove commas, split by spaces, and strip extra spaces
1843-
rename = [item.strip() for item in rename.replace(',', '').split() if item.strip()]
1859+
rename = [item.strip() for item in rename.replace(',,', ',').split() if item.strip()]
18441860
elif not isinstance(rename, list):
18451861
rename = []
18461862

@@ -1896,8 +1912,7 @@ class Dictionary(TkinterDnD.Tk):
18961912
initial_grapheme = item_values[0]
18971913
initial_phoneme = item_values[1]
18981914
initial_rename = item_values[2] if len(item_values) > 2 else ''
1899-
#initial_phoneme = self.viewer_tree.item(selected_item, "values")[2].replace(",", "").replace("'", "")
1900-
1915+
19011916
# Destroy currently open widgets if they exist
19021917
if self.current_entry_widgets:
19031918
for widget in self.current_entry_widgets.values():
@@ -1928,7 +1943,8 @@ class Dictionary(TkinterDnD.Tk):
19281943
# Get the edited values from entry widgets
19291944
new_grapheme = self.entry_popup_sym.get()
19301945
new_phoneme = self.entry_popup_val.get()
1931-
new_rename = self.entry_popup_rn.get().replace(",", "").replace("'", "")
1946+
new_rename = self.entry_popup_rn.get()
1947+
rename_list = [rename.replace(" ", ",") if " " in rename else rename for rename in new_rename.split()]
19321948

19331949
# Update Treeview with edited values
19341950
self.symbol_treeview.set(selected_item, grapheme_column, new_grapheme)
@@ -1948,14 +1964,14 @@ class Dictionary(TkinterDnD.Tk):
19481964

19491965
# Check if a rename value exists, and update accordingly
19501966
if new_rename:
1951-
self.symbols[new_grapheme].append(new_rename) # Add rename if it exists
1967+
self.symbols[new_grapheme].append(rename_list) # Add rename if it exists
19521968

19531969
# Update symbols_list
19541970
for entry in self.symbols_list:
19551971
if entry['symbol'] == initial_grapheme:
19561972
entry['symbol'] = new_grapheme
19571973
entry['type'] = phoneme_list
1958-
entry['rename'] = new_rename.split() # Always update the rename field
1974+
entry['rename'] = rename_list # Always update the rename field
19591975
break
19601976
self.refresh_treeview_symbols()
19611977

@@ -2710,7 +2726,8 @@ class Dictionary(TkinterDnD.Tk):
27102726
updated_entries[grapheme] = (new_grapheme, ', '.join(phonemes)) # Update Treeview
27112727
items_modified += 1
27122728
elif target == "Phonemes":
2713-
phonemes_string = ', '.join(phonemes)
2729+
cleaned_phonemes = [p.replace("'", "") for p in phonemes]
2730+
phonemes_string = ', '.join(cleaned_phonemes)
27142731
modified_phoneme_string = compiled_pattern.sub(replacement, phonemes_string)
27152732
if modified_phoneme_string != phonemes_string:
27162733
new_phoneme_list = [phoneme.strip() for phoneme in modified_phoneme_string.split(',')]
@@ -3411,7 +3428,7 @@ class Dictionary(TkinterDnD.Tk):
34113428
added_symbols = set() # Track added symbols to avoid duplicates
34123429

34133430
for symbol, values in self.symbols.items():
3414-
if isinstance(values[0], list) and ',' and ' ' in symbol:
3431+
if ',' in symbol or ' ' in symbol:
34153432
continue
34163433
escaped_symbol = symbol
34173434
type_list = values[0] if isinstance(values[0], list) else [values[0]]
@@ -3448,11 +3465,26 @@ class Dictionary(TkinterDnD.Tk):
34483465
to_key = to_entry
34493466
to_yaml_value = to_entry
34503467

3451-
replacement_key = (tuple(from_symbol) if isinstance(from_symbol, list) else from_symbol, to_key)
3468+
if isinstance(from_symbol, str) and ',' in from_symbol:
3469+
from_list = [item.strip() for item in from_symbol.split(',') if item.strip()]
3470+
if not from_list:
3471+
continue
3472+
from_key = tuple(from_list)
3473+
from_yaml_value = from_list
3474+
elif isinstance(from_symbol, list):
3475+
if not from_symbol:
3476+
continue
3477+
from_key = tuple(from_symbol)
3478+
from_yaml_value = from_symbol
3479+
else:
3480+
from_key = from_symbol
3481+
from_yaml_value = from_symbol
3482+
3483+
replacement_key = (from_key, to_key)
34523484
if replacement_key not in added_replacements:
34533485
added_replacements.add(replacement_key)
34543486
entry = CommentedMap([
3455-
('from', from_symbol if not isinstance(from_symbol, list) else list(from_symbol)),
3487+
('from', from_yaml_value),
34563488
('to', to_yaml_value)
34573489
])
34583490
rename_entries.append(entry)

0 commit comments

Comments
 (0)