I observed the problem with optional element (min="0") when replacing
elements in suggestions, e.g.

           <pattern>
              <token>за</token>
              <token postag_regexp="yes" postag="adj.*" min="0"/>
              <token
regexp="yes">грибами|ягодами|брусницею|журавлиною|суницею|ожиною|хлібом</token>
           </pattern>
           <message>Якщо йдеться про мету руху, вживаємо прийменник
«по»</message>
           <suggestion>по <match no="2" postag_regexp="yes"
postag="(adj:.:)v_oru(.*)" postag_replace="$1v_naz$2"/> <match no="3"
postag_regexp="yes" postag="(noun:.:)v_oru(.*)"
postag_replace="$1v_naz$2"/></suggestion>

When sentence have optional element all is good:
           <example type="incorrect" correction="по білі гриби">Піти
<marker>за білими грибами</marker>.</example>

but if sentence does not have second element <match no="2"... will
(incorrectly) use first element ("за" in this case). I'd like to be able
to ignore optional element in suggestions if it's missing, such that
next test passes:
           <example type="incorrect" correction="по гриби">Піти
<marker>за грибами</marker>.</example>
 
Attached is a little patch which fixes this problem. I ran some basic
tests and they passed but I must admit there's a lot of code I don't
understand there so I would appreciate a code review.

In general it looks if the position of the optional element is 0 it'll
skip the replacement and also if two adjusent spaces left due to this
skip it'll remove one of them.

Thanks
Andriy

diff --git a/languagetool-core/src/main/java/org/languagetool/rules/patterns/PatternRuleMatcher.java b/languagetool-core/src/main/java/org/languagetool/rules/patterns/PatternRuleMatcher.java
index ad1074e..2a94e34 100644
--- a/languagetool-core/src/main/java/org/languagetool/rules/patterns/PatternRuleMatcher.java
+++ b/languagetool-core/src/main/java/org/languagetool/rules/patterns/PatternRuleMatcher.java
@@ -289,17 +290,25 @@
         if (j + 1 < positions.size()) {
           nextTokenPos = firstMatchTok + repTokenPos + positions.get(j + 1);
         }
+
         //final List<Match> suggestionMatches = rule.getSuggestionMatches();
         if (suggestionMatches != null) {
           if (matchCounter < suggestionMatches.size()) {
             numbersToMatches[j] = matchCounter;
             if (suggestionMatches.get(matchCounter) != null) {
-              final String[] matches = concatMatches(matchCounter, j,
-                  firstMatchTok + repTokenPos, tokenReadings, nextTokenPos, suggestionMatches);
+              final String[] matches = positions.get(j) != 0
+                   ? concatMatches(matchCounter, j,
+                       firstMatchTok + repTokenPos, tokenReadings, nextTokenPos, suggestionMatches)
+                   : new String[] { "" };
               final String leftSide = errorMessage.substring(0, backslashPos);
               final String rightSide = errorMessage.substring(backslashPos + numLen);
               if (matches.length == 1) {
-                errorMessage = leftSide + matches[0] + rightSide;
+                if( matches[0].length() == 0 && leftSide.endsWith(" ") && rightSide.startsWith(" ") ){
+                  errorMessage = leftSide.substring(0, leftSide.length()-1) + rightSide;
+                }
+                else {
+                  errorMessage = leftSide + matches[0] + rightSide;
+                }
               } else {
                 errorMessage = formatMultipleSynthesis(matches, leftSide,
                     rightSide);
------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk
_______________________________________________
Languagetool-devel mailing list
Languagetool-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/languagetool-devel

Reply via email to