On Apr 5, 2013, at 9:42 PM, Paolo Bianchini wrote:

> Yes it makes sense. I'll post an updated version. 
> 

Here's the last version of the rule.

Let me know if it is ok.

Thanks

Paolo

/* LanguageTool, a natural language style checker 
 * Copyright (C) 2005 Daniel Naber (http://www.danielnaber.de)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */
package org.languagetool.rules;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import org.languagetool.AnalyzedSentence;
import org.languagetool.AnalyzedTokenReadings;
import org.languagetool.tools.StringTools;

/**
 * A rule that matches several punctuation signs such as : ; and % preceded by 
whitespace.
 * 
 * BUG ID 3607406: no space before semicolon
 * 
 * @author Paolo Bianchini
 */

public class WhitespaceBeforePunctuationRule extends Rule {

  public WhitespaceBeforePunctuationRule(final ResourceBundle messages) {
    super(messages);
    super.setCategory(new Category(messages.getString("category_misc")));
    setLocQualityIssueType("typographical");
  }

  @Override
  public final String getId() {
    return "WHITESPACE_PUNCTUATION";
  }

  @Override
  public final String getDescription() {
    return messages.getString("desc_whitespace_before_punctuation");
  }

  @Override
  public final RuleMatch[] match(final AnalyzedSentence text) {
    final List<RuleMatch> ruleMatches = new ArrayList<RuleMatch>();
    final AnalyzedTokenReadings[] tokens = text.getTokens();
    String prevToken = "";
    String prevPrevToken = "";
    boolean prevWhite = false;    
    int prevLen = 0;
    for (int i = 0; i < tokens.length; i++) {
      final String token = tokens[i].getToken();
      final boolean isWhitespace = tokens[i].isWhitespace() || 
StringTools.isNonBreakingWhitespace(token)
              || tokens[i].isFieldCode();
      String msg = null;
      int fixLen = 0;
      String suggestionText = null;
      if (prevWhite) {
        if (token.equals(":")) {
            msg = messages.getString("no_space_before_colon");
            suggestionText = ":";
            fixLen = 1;
            // exception case for figures such as " : 0" 
            if (i + 2 < tokens.length
                && tokens[i + 1].isWhitespace()
                && Character.isDigit(tokens[i + 2].getToken().charAt(0))) {
              msg = null;
            }
        } else if (token.equals(";")) {
            msg = messages.getString("no_space_before_semicolon");
            suggestionText = ";";
            fixLen = 1;
        } else if (i > 1
                        && token.equals("%")
                        && Character.isDigit(tokens[i - 
2].getToken().charAt(0))) {
            msg = messages.getString("no_space_before_percentage");
            suggestionText = "%";
            fixLen = 1;
        }
      }
      if (msg != null) {
        final int fromPos = tokens[i - 1].getStartPos();
        final int toPos = tokens[i - 1].getStartPos() + fixLen + prevLen;
        // TODO: add some good short comment here
        final RuleMatch ruleMatch = new RuleMatch(this, fromPos, toPos, msg);
        ruleMatch.setSuggestedReplacement(suggestionText);
        ruleMatches.add(ruleMatch);
      }
      prevPrevToken = prevToken;
      prevToken = token;
      prevWhite = isWhitespace && !tokens[i].isFieldCode(); //OOo code before 
comma/dot
      prevLen = tokens[i].getToken().length();
    }

    return toRuleMatchArray(ruleMatches);
  }

  @Override
  public void reset() {
    // nothing
  }

}





> Paolo
> 
> On 05/apr/2013, at 19:09, Daniel Naber <[email protected]> wrote:
> 
>> On 05.04.2013, 17:30:59 Paolo Bianchini wrote:
>> 
>>> OK so I have a patch that adds another Java rule and uses it for the
>>> Italian language module.
>> 
>> Thanks! Could you try to avoid the code duplication with 
>> CommaWhitespaceRule? All static methods seem to be duplicated from that 
>> class, so why not call them in CommaWhitespaceRule instead of copying them?
>> 
>> Regards
>> Daniel
>> 
>> -- 
>> http://www.danielnaber.de
>> 
>> 
>> ------------------------------------------------------------------------------
>> Minimize network downtime and maximize team effectiveness.
>> Reduce network management and security costs.Learn how to hire 
>> the most talented Cisco Certified professionals. Visit the 
>> Employer Resources Portal
>> http://www.cisco.com/web/learning/employer_resources/index.html
>> _______________________________________________
>> Languagetool-devel mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/languagetool-devel
> 
> ------------------------------------------------------------------------------
> Minimize network downtime and maximize team effectiveness.
> Reduce network management and security costs.Learn how to hire 
> the most talented Cisco Certified professionals. Visit the 
> Employer Resources Portal
> http://www.cisco.com/web/learning/employer_resources/index.html
> _______________________________________________
> Languagetool-devel mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/languagetool-devel

------------------------------------------------------------------------------
Minimize network downtime and maximize team effectiveness.
Reduce network management and security costs.Learn how to hire 
the most talented Cisco Certified professionals. Visit the 
Employer Resources Portal
http://www.cisco.com/web/learning/employer_resources/index.html
_______________________________________________
Languagetool-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/languagetool-devel

Reply via email to