Revision: 7180
          
http://languagetool.svn.sourceforge.net/languagetool/?rev=7180&view=rev
Author:   dnaber
Date:     2012-06-02 18:51:58 +0000 (Sat, 02 Jun 2012)
Log Message:
-----------
make rule creator search the prepared index

Modified Paths:
--------------
    trunk/ltcommunity/grails-app/conf/Config.groovy
    
trunk/ltcommunity/grails-app/controllers/org/languagetool/RuleEditorController.groovy
    trunk/ltcommunity/grails-app/views/ruleEditor/checkRule.gsp
    trunk/ltcommunity/web-app/css/iframe.css

Modified: trunk/ltcommunity/grails-app/conf/Config.groovy
===================================================================
--- trunk/ltcommunity/grails-app/conf/Config.groovy     2012-06-02 18:41:57 UTC 
(rev 7179)
+++ trunk/ltcommunity/grails-app/conf/Config.groovy     2012-06-02 18:51:58 UTC 
(rev 7180)
@@ -47,6 +47,9 @@
 hide.languages = ["cs"]
 maxPatternElements = 5
 
+// Lucene index directories for fast rule matching - "LANG" will be replaced 
with the language code:
+fastSearchIndex = "/home/languagetool/corpus/LANG"
+
 // log4j configuration
 log4j = {
     appenders {

Modified: 
trunk/ltcommunity/grails-app/controllers/org/languagetool/RuleEditorController.groovy
===================================================================
--- 
trunk/ltcommunity/grails-app/controllers/org/languagetool/RuleEditorController.groovy
       2012-06-02 18:41:57 UTC (rev 7179)
+++ 
trunk/ltcommunity/grails-app/controllers/org/languagetool/RuleEditorController.groovy
       2012-06-02 18:51:58 UTC (rev 7180)
@@ -19,6 +19,11 @@
 package org.languagetool
 
 import org.languagetool.rules.patterns.PatternRule
+import org.languagetool.rules.RuleMatch
+import org.languagetool.dev.index.Searcher
+import org.apache.lucene.search.IndexSearcher
+import org.apache.lucene.store.FSDirectory
+import org.languagetool.dev.index.SearcherResult
 
 /**
  * Editor that helps with creating the XML for simple rules.
@@ -42,6 +47,36 @@
         List unexpectedRuleMatches = langTool.check(params.correctExample1)
         List problems = []
         List shortProblems = []
+        checkExampleSentences(expectedRuleMatches, unexpectedRuleMatches, 
problems, shortProblems)
+        if (problems.size() == 0) {
+            SearcherResult searcherResult = 
checkRuleAgainstCorpus(patternRule, language)
+            log.info("Checked rule: valid - LANG: ${language.getShortName()} - 
PATTERN: ${params.pattern} - BAD: ${params.incorrectExample1} - GOOD: 
${params.correctExample1}")
+            [messagePreset: params.messageBackup, namePreset: 
params.nameBackup, searcherResult: searcherResult]
+        } else {
+            log.info("Checked rule: invalid - LANG: ${language.getShortName()} 
- PATTERN: ${params.pattern} - BAD: ${params.incorrectExample1} - GOOD: 
${params.correctExample1} - ${shortProblems}")
+            render(template: 'checkRuleProblem', model: [problems: problems, 
hasRegex: hasRegex(patternRule)])
+        }
+    }
+
+    SearcherResult checkRuleAgainstCorpus(PatternRule patternRule, Language 
language) {
+        Searcher searcher = new Searcher()  // TODO: move to service?
+        searcher.setMaxHits(20)
+        String indexDirTemplate = grailsApplication.config.fastSearchIndex
+        File indexDir = new File(indexDirTemplate.replace("LANG", 
language.getShortName()))
+        if (indexDir.isDirectory()) {
+            IndexSearcher indexSearcher = new 
IndexSearcher(FSDirectory.open(indexDir))
+            SearcherResult searcherResult
+            try {
+              searcherResult = searcher.findRuleMatchesOnIndex(patternRule, 
language, indexSearcher)
+            } finally {
+              indexSearcher.close()
+            }
+            return searcherResult
+        }
+        return null
+    }
+
+    private void checkExampleSentences(List<RuleMatch> expectedRuleMatches, 
List<RuleMatch> unexpectedRuleMatches, List problems, List shortProblems) {
         if (expectedRuleMatches.size() == 0) {
             problems.add("The rule did not find an error in the given example 
sentence with an error")
             shortProblems.add("errorNotFound")
@@ -50,13 +85,6 @@
             problems.add("The rule found an error in the given example 
sentence that is not supposed to contain an error")
             shortProblems.add("unexpectedErrorFound")
         }
-        if (problems.size() == 0) {
-            log.info("Checked rule: valid - LANG: ${language.getShortName()} - 
PATTERN: ${params.pattern} - BAD: ${params.incorrectExample1} - GOOD: 
${params.correctExample1}")
-            [messagePreset: params.messageBackup, namePreset: 
params.nameBackup]
-        } else {
-            log.info("Checked rule: invalid - LANG: ${language.getShortName()} 
- PATTERN: ${params.pattern} - BAD: ${params.incorrectExample1} - GOOD: 
${params.correctExample1} - ${shortProblems}")
-            render(template: 'checkRuleProblem', model: [problems: problems, 
hasRegex: hasRegex(patternRule)])
-        }
     }
 
     private JLanguageTool getLanguageToolWithOneRule(Language lang, 
PatternRule patternRule) {

Modified: trunk/ltcommunity/grails-app/views/ruleEditor/checkRule.gsp
===================================================================
--- trunk/ltcommunity/grails-app/views/ruleEditor/checkRule.gsp 2012-06-02 
18:41:57 UTC (rev 7179)
+++ trunk/ltcommunity/grails-app/views/ruleEditor/checkRule.gsp 2012-06-02 
18:51:58 UTC (rev 7180)
@@ -1,3 +1,41 @@
+
+<g:if test="${searcherResult}">
+    <g:set var="sentencesChecked" 
value="${formatNumber(number:searcherResult.getCheckedSentences(), type: 
'number')}"/>
+
+    <g:if test="${searcherResult.getMatchingSentences().size() == 0}">
+
+        <p style="width:700px;">We've checked your pattern against 
${sentencesChecked} sentences
+        from <a href="http://www.wikipedia.org";>Wikipedia</a> and found no 
matches.</p>
+
+    </g:if>
+    <g:else>
+
+        <p style="width:700px;">We've checked your pattern against 
${sentencesChecked} sentences
+        from <a href="http://www.wikipedia.org";>Wikipedia</a> and found the 
following matches.
+        Please consider modifying your rule if these matches are false alarms.
+        As this page does not support our full rule syntax you might want to 
learn
+        more in <a target="_parent" 
href="http://www.languagetool.org/development/";>our development
+        documentation</a>.</p>
+
+        <ul style="margin-bottom: 30px">
+            <g:each in="${searcherResult.getMatchingSentences()}" 
var="matchingSentence">
+                <g:each in="${matchingSentence.getRuleMatches()}" var="match">
+                    <li>
+                        <span 
class="exampleSentence">${org.languagetool.gui.Tools.getContext(
+                            match.getFromPos(), match.getToPos(),
+                            matchingSentence.getSentence(),
+                            100, "<span class='errorMarker'>", "</span>", 
true)}</span>
+                    </li>
+                </g:each>
+            </g:each>
+        </ul>
+
+    </g:else>
+
+</g:if>
+<g:else>
+</g:else>
+
 <table style="border: 0px">
 <tr>
     <td valign="top" width="150">Error Message</td>

Modified: trunk/ltcommunity/web-app/css/iframe.css
===================================================================
--- trunk/ltcommunity/web-app/css/iframe.css    2012-06-02 18:41:57 UTC (rev 
7179)
+++ trunk/ltcommunity/web-app/css/iframe.css    2012-06-02 18:51:58 UTC (rev 
7180)
@@ -220,3 +220,11 @@
 .preFilledField {
     width: 500px;
 }
+
+.errorMarker {
+       padding-bottom: 2px;
+       background-image: url("../images/squiggle.png");
+       background-repeat: repeat-x;
+       background-position: left bottom;
+       vertical-align: text-top;
+}
\ No newline at end of file

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Languagetool-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/languagetool-cvs

Reply via email to