package de.danielnaber.languagetool.dev.index;

import org.apache.lucene.search.Query;

import de.danielnaber.languagetool.rules.patterns.PatternRule;

public class PatternRuleQueryBuilder {

  public static Query bulidQuery(PatternRule rule) {
    // TODO Tao: build query from a rule
    return null;
  }

}

@@ -54,41 +54,383 @@
 ort 
-org.apache.lucene.search.Query;%0D%0A
+java.util.ArrayList;%0D%0A%0D%0Aimport org.apache.lucene.index.Term;%0D%0Aimport org.apache.lucene.search.Query;%0D%0Aimport org.apache.lucene.search.regex.SpanRegexQuery;%0D%0Aimport org.apache.lucene.search.spans.SpanNearQuery;%0D%0Aimport org.apache.lucene.search.spans.SpanQuery;%0D%0Aimport org.apache.lucene.search.spans.SpanTermQuery;%0D%0A%0D%0Aimport de.danielnaber.languagetool.rules.patterns.Element;
 %0D%0Aim
@@ -535,16 +535,62 @@
 er %7B%0D%0A%0D%0A
+  public static final String FN = %22field%22;%0D%0A%0D%0A
   public
@@ -643,61 +643,1527 @@
     
-// TODO Tao: build query from a rule%0D%0A    return null
+ArrayList%3CSpanQuery%3E list = new ArrayList%3CSpanQuery%3E();%0D%0A    int i = 0;%0D%0A    for (Element patternElement : rule.getElements()) %7B%0D%0A      SpanQuery termQuery = createSpanQuery(patternElement.getString(), %22%22,%0D%0A          patternElement.getNegation(), patternElement.isRegularExpression());%0D%0A      SpanQuery posQuery = createSpanQuery(patternElement.getPOStag(),%0D%0A          LanguageToolFilter.POS_PREFIX, patternElement.getPOSNegation(),%0D%0A          patternElement.isPOStagRegularExpression());%0D%0A%0D%0A      if (termQuery != null && posQuery != null) %7B%0D%0A        SpanNearQuery q = new SpanNearQuery(new SpanQuery%5B%5D %7B termQuery, posQuery %7D, 0, false);%0D%0A        list.add(q);%0D%0A      %7D else if (termQuery != null) %7B%0D%0A        list.add(termQuery);%0D%0A      %7D else if (posQuery != null) %7B%0D%0A        list.add(posQuery);%0D%0A      %7D else %7B%0D%0A        i++;%0D%0A      %7D%0D%0A      i += patternElement.getSkipNext();%0D%0A%0D%0A    %7D%0D%0A%0D%0A    SpanNearQuery snq = new SpanNearQuery(list.toArray(new SpanQuery%5Blist.size()%5D), i, true);%0D%0A    return snq;%0D%0A  %7D%0D%0A%0D%0A  private static SpanQuery createSpanQuery(String token, String prefix, boolean isNegation,%0D%0A      boolean isRegularExpression) %7B%0D%0A    SpanQuery q = null;%0D%0A    if (token != null && !token.equals(%22%22)) %7B%0D%0A      Term term = new Term(FN, prefix + token);%0D%0A      if (isNegation) %7B%0D%0A        q = new SpanRegexNotQuery(term);%0D%0A      %7D else %7B%0D%0A        if (isRegularExpression) %7B%0D%0A          q = new SpanRegexQuery(term);%0D%0A        %7D else %7B%0D%0A          q = new SpanTermQuery(term);%0D%0A        %7D%0D%0A      %7D%0D%0A    %7D%0D%0A    return q
 ;%0D%0A 

implement query builder