transliterate

Transliteration engine
git clone git://lumidify.org/transliterate.git
Log | Files | Refs | README | LICENSE

commit b87948bc783674f647964db5515026d37c0d182e
parent 95b442d44108af5e6bb79061e5a82805b35792a0
Author: lumidify <nobody@lumidify.org>
Date:   Thu,  9 Apr 2020 15:51:14 +0200

Add "Open in unknown word window" button

Diffstat:
Mtransliterate.pl | 144++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
1 file changed, 106 insertions(+), 38 deletions(-)

diff --git a/transliterate.pl b/transliterate.pl @@ -74,6 +74,9 @@ sub add_to_trie { # Prompt user when no replacement has been found for a word # $word is the word that was not found and $context* the context, +# $word_repl is the replacement word - this is only used when the window is +# called from the word choice window, since the original and +# replacement aren't the same then # with context*_orig being the original, non-transliterated context. # $table_paths is a mapping of table paths (here, only the keys, i.e. # the actual paths, are used) to allow the user to choose a table to @@ -86,7 +89,7 @@ sub add_to_trie { # in the form ["<action name>", <optional args>]. # See `handle_unknown_word_action` for currently accepted values sub prompt_unknown_word { - my ($contextl, $contextl_orig, $word, $contextr, $contextr_orig, $config, $cur_lineno, $config_error) = @_; + my ($contextl, $contextl_orig, $word_repl, $word, $contextr, $contextr_orig, $config, $cur_lineno, $config_error) = @_; my $action; my $stop = 0; @@ -111,7 +114,7 @@ sub prompt_unknown_word { # selected text as the word to be replaced - useful when only part # of the entire word that was not found has to be replaced my $make_context_box = sub { - my ($ctxtl, $ctxtr, $lbl) = @_; + my ($ctxtl, $wrd, $ctxtr, $lbl) = @_; my $hbox = Gtk2::HBox->new(FALSE, 5); my $label = Gtk2::Label->new($lbl); my $text = Gtk2::TextView->new; @@ -120,7 +123,7 @@ sub prompt_unknown_word { $buffer->set_text($ctxtr); my $highlight = $buffer->create_tag("yellow_bg", "background", "yellow"); my $start = $buffer->get_start_iter(); - $buffer->insert_with_tags($start, $word, $highlight); + $buffer->insert_with_tags($start, $wrd, $highlight); $start = $buffer->get_start_iter(); $buffer->insert($start, $ctxtl); my $button = Gtk2::Button->new("Use selection as word"); @@ -145,8 +148,8 @@ sub prompt_unknown_word { $hbox->pack_start($button, FALSE, FALSE, 0); $vbox->pack_start($hbox, FALSE, FALSE, 0); }; - $make_context_box->($contextl, $contextr, "Context: "); - $make_context_box->($contextl_orig, $contextr_orig, "Original: "); + $make_context_box->($contextl, $word_repl, $contextr, "Context: "); + $make_context_box->($contextl_orig, $word, $contextr_orig, "Original: "); my $hbox = Gtk2::HBox->new(FALSE, 5); my $label = Gtk2::Label->new("Ignore: "); @@ -266,8 +269,13 @@ sub prompt_unknown_word { # Prompt the user when a word has multiple replacement options (separated by $config->{choicesep}) # $cur_lineno - display string to show the current line number +# Returns: +# 3, if this window needs to be called again but nothing needs +# to be re-transliterated +# 1, if the line needs to be re-transliterated +# 0, if nothing needs to be done sub prompt_choose_word { - my ($substrings, $cur_lineno, $config) = @_; + my ($substrings, $config, $args, $cur_lineno) = @_; # make a list of all substrings that contain multiple word options my @replacements; @@ -302,6 +310,7 @@ sub prompt_choose_word { return if (!@replacements); my $stop = 0; + my $open_unknown = 0; my $cur_replacement = 0; my $window = Gtk2::Window->new('toplevel'); @@ -339,7 +348,9 @@ sub prompt_choose_word { $hbox->pack_start($text, TRUE, TRUE, 10); $vbox->pack_start($hbox, FALSE, FALSE, 10); - $hbox = Gtk2::HBox->new(FALSE, 0); + $hbox = Gtk2::HBox->new(FALSE, 5); + my $unknown_button = Gtk2::Button->new("Open in unknown word window"); + $hbox->pack_start($unknown_button, FALSE, FALSE, 0); my $stop_button = Gtk2::Button->new("Stop processing"); $hbox->pack_start($stop_button, FALSE, FALSE, 0); $vbox->pack_start($hbox, FALSE, FALSE, 0); @@ -450,6 +461,12 @@ sub prompt_choose_word { $window->destroy; }, $window); + $unknown_button->signal_connect( + clicked => sub { + $open_unknown = 1; + $window->destroy; + }, $window); + $stop_button->signal_connect( clicked => sub { $stop = 1; @@ -464,6 +481,16 @@ sub prompt_choose_word { $accept->hide; Gtk2->main; die "Processing stopped at line $cur_lineno\n" if $stop; + if ($open_unknown) { + my $ret = call_unknown_word_window( + $substrings, $replacements[$cur_replacement]->[0], + $config, $args, $cur_lineno); + # the word choice window still needs to be called again + # when 0 is returned + return 3 if $ret == 0; + return $ret; + } + return 0; } my $ID = 0; @@ -1228,6 +1255,47 @@ sub replace_line { return $substrings; } +# Call the unknown word window with the given substrings and index +# See `get_unknown_words` for explanation of other parameters +# (should be obvious) +# Returns: +# 3, if the rest of the line should be skipped +# 1, if the line needs to be re-transliterated +# 0, if nothing needs to be done +sub call_unknown_word_window { + my ($substrings, $i, $config, $args, $cur_lineno) = @_; + my $word = $substrings->[$i]; + my $contextl = ""; + my $contextl_orig = ""; + foreach my $j (0..$i-1) { + $contextl .= $substrings->[$j]->[1]; + $contextl_orig .= $substrings->[$j]->[2]; + } + my $contextr = ""; + my $contextr_orig = ""; + foreach my $j ($i+1..$#$substrings) { + $contextr .= $substrings->[$j]->[1]; + $contextr_orig .= $substrings->[$j]->[2]; + } + my $action = prompt_unknown_word($contextl, $contextl_orig, + $word->[1], $word->[2], $contextr, $contextr_orig, + $config, "$cur_lineno" + ); + # if $ret == 3, rest of line should be skipped + # if $ret == 2, config could not be loaded + # if $ret == 1, line must be redone with new config + my $ret = handle_unknown_word_action($action, $config, $args); + # keep retrying until the user chooses an action which + # didn't throw an error + while ($ret == 2) { + $action = prompt_unknown_word($contextl, $contextl_orig, + $word->[1], $word->[2], $contextr, $contextr_orig, + $config, "$cur_lineno", 1); + $ret = handle_unknown_word_action($action, $config, $args); + } + return $ret; +} + # NOTE: MUST ALWAYS ADD REPLACEMENT WORDS FIRST! # If an ignore word is added which is attached to a word that should have a replacement # added and just that word is selected to ignore, you never get a chance to add a @@ -1251,36 +1319,10 @@ sub get_unknown_words { foreach my $i (0 .. $#$substrings) { my $word = $substrings->[$i]; if (!$word->[0] && !exists($config->{"ignore_words"}->{$word->[1]})) { - my $contextl = ""; - my $contextl_orig = ""; - foreach my $j (0..$i-1) { - $contextl .= $substrings->[$j]->[1]; - $contextl_orig .= $substrings->[$j]->[2]; - } - my $contextr = ""; - my $contextr_orig = ""; - foreach my $j ($i+1..$#$substrings) { - $contextr .= $substrings->[$j]->[1]; - $contextr_orig .= $substrings->[$j]->[2]; - } - my $action = prompt_unknown_word($contextl, $contextl_orig, - $word->[1], $contextr, $contextr_orig, - $config, "$cur_lineno" - ); - # if $ret == 3, just return here - # if $ret == 2, config could not be loaded - # if $ret == 1, line must be redone with new config - my $ret = handle_unknown_word_action($action, $config, $args); - # keep retrying until the user chooses an action which - # didn't throw an error - while ($ret == 2) { - $action = prompt_unknown_word($contextl, $contextl_orig, - $word->[1], $contextr, $contextr_orig, - $config, "$cur_lineno", 1); - $ret = handle_unknown_word_action($action, $config, $args); - } - last if $ret == 3; - # re-transliterate the line with the new config + my $ret = call_unknown_word_window $substrings, $i, $config, $args, $cur_lineno; + # 3 means we ignore the rest of the line + return 0 if $ret == 3; + # 1 means the line needs to be re-transliterated return 1 if $ret == 1; } $i++; @@ -1317,7 +1359,13 @@ sub replace { } } if (!$args->{"nochoices"}) { - prompt_choose_word($substrings, "$./$total_lines", $config); + # this only loops more than once if the user presses the button + # "Open in unknown word window" + while (my $ret = prompt_choose_word($substrings, $config, $args, "$./$total_lines")) { + if ($ret == 1) { + $substrings = replace_line($config, $nfd_line); + } + } } elsif ($args->{"debug"}) { foreach my $s (@$substrings) { if ($s->[0] && $s->[1] =~ /\Q$config->{choicesep}\E/) { @@ -1548,6 +1596,26 @@ it simply needs to be ignored. I guess this could be useful if B<choicesep> occurs in the text (although that should be avoided) and is ignored with a B<matchignore> statement. It shouldn't be needed in most cases, though. +"Open in unknown word window" will open the +L<unknown word window|/"UNKNOWN WORD WINDOW"> with the current word +selected. This is meant as a helper if you notice that another word choice +needs to be added. + +Warning: This is very inconsistent and buggy! Since the unknown word window +is just opened directly, it isn't modified to make more sense for this +situation. Whenever "Add replacement" is pressed, the whole line is +re-transliterated as usual, but the word choice window is opened again +right afterwards. If you just want to go back to the word choice window, +press the ignore button for "whole line" since that shouldn't break +anything. There are weird inconsistencies, though - for instance, if you +delete all words in the tables, then press "Reload config", the line will +be re-transliterated and none of the words will actually be found, but it +will still go on because control passes back to the word choice window no +matter what. Also, none of the word choices that were already done on this +line are saved since the line is restarted from the beginning. As I said, +it's only there as a helper and is very buggy/inconsistent. Maybe I'll make +everything work better in a future release. + "Stop processing" will exit the program and print the line number that was currently being processed.