Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (C) 2012 cogroo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.cogroo.tools.checker.rules;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import org.cogroo.analyzer.ComponentFactory;
import org.cogroo.checker.CheckDocument;
import org.cogroo.checker.GrammarChecker;
import org.cogroo.entities.Mistake;
import org.cogroo.entities.impl.MistakeImpl;
import org.cogroo.text.Sentence;
import org.cogroo.tools.checker.rules.applier.RulesProvider;
import org.cogroo.tools.checker.rules.applier.RulesXmlAccess;
import org.cogroo.tools.checker.rules.model.Example;
import org.cogroo.tools.checker.rules.model.Rule;
import org.cogroo.tools.checker.rules.model.Rules;
import org.cogroo.tools.checker.rules.util.RuleUtils;
import org.cogroo.tools.checker.rules.util.RuleUtils.RuleInfo;
/**
* This class grammar checks all examples from the rules file and prints an html report
* showing which rules are working.
*
* @author Marcelo Suzumura
*/
public class CogrooHtml {
/**
* The file in which the report will be written.
*/
private Writer out;
/**
* The rules.
*/
private Rules rules;
/**
* The grammar checker.
*/
private GrammarChecker cogroo;
/**
* Examples that were not matched by any rule at all.
*/
private List no = new ArrayList();
/**
* Examples matched by the correct rule.
*/
private List ok = new ArrayList();
/**
* Examples matched by the correct rule, but matched by other rules too.
*/
private List partial = new ArrayList();
/**
* Examples matched only by other rules.
*/
private List wrong = new ArrayList();
/**
* List of rules that does not have any suggestion.
*/
private List noSuggestions = new ArrayList();
/**
* List of rules that does have bad suggestion.
*/
private List badSuggestion = new ArrayList();
/**
* Maps rules ids and the sentences that caused an exception.
*/
private Map> exceptions = new LinkedHashMap>();
private Map rulesInfo = new HashMap();
private String path;
public CogrooHtml(File f, GrammarChecker cogroo) throws Exception {
path = f.getAbsolutePath();
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(f), StandardCharsets.UTF_8
));
this.cogroo = cogroo;
// TagDictionary td = new TagDictionary(new FSALexicalDictionary(), false,
// new FlorestaTagInterpreter());
//
// converter = new TextEntitiesConverter(td);
this.rules = getRules();
}
private Rules getRules() {
// Create XML rules applier
RulesProvider xmlProvider = new RulesProvider(RulesXmlAccess.getInstance(),
false);
return xmlProvider.getRules();
}
public void evaluate() throws Exception {
this.printHtmlHeader();
int totalRules = 0;
for (Rule rule : this.rules.getRule()) {
// Only active rules will be considered.
if (rule.isActive()) {
// Consolidates rule information.
this.prepareRuleInfo(rule);
totalRules++;
this.printRuleHeader(rule);
// Each position contains the mistakes for each example.
List> sentencesMistakes = new ArrayList>(rule.getExample().size());
List sentences = new ArrayList(rule.getExample().size());
// Checks each incorrect example of the rule.
for (Example example : rule.getExample()) {
// Check sentence for mistakes.
try {
CheckDocument d = new CheckDocument();
d.setText(example.getIncorrect());
this.cogroo.analyze(d);
sentencesMistakes.add(d.getMistakes());
sentences.add(d.getSentences().get(0));
} catch (RuntimeException e) {
e.printStackTrace();
sentencesMistakes.add(null);
sentences.add(null);
this.logException(Long.valueOf(rule.getId()), example.getIncorrect());
}
}
this.evaluateMistakes(sentencesMistakes, sentences, rule);
this.printRuleFooter(rule);
}
this.storeNoSuggestionRule(rule);
}
this.printReport("ok", this.ok, totalRules, "00ff00");
this.printReport("partial", this.partial, totalRules, "ffff00");
this.printReport("no", this.no, totalRules, "ff8000");
this.printReport("wrong", this.wrong, totalRules, "ff0000");
this.printNoSuggestion();
this.printBadSuggestion();
this.printExceptionReport();
this.printHtmlFooter();
this.out.close();
System.out.println("Finished. Report file: " + path);
}
private void prepareRuleInfo(Rule rule) {
Map mapInfo = RuleUtils.getRuleAsString(rule);
StringBuilder sb = new StringBuilder();
sb.append("
");
return this.escapeHtmlChars(sb.toString());
}
public List> getAnalysisAsTable(Sentence sentence) {
if (sentence.getTokens().size() > 0 && sentence.getTokens().get(0).getPOSTag() == null) {
throw new IllegalStateException("The sentence was not analyzed yet.");
}
List> analysis = new ArrayList>(5);
for (int i = 0; i < sentence.getTokens().size(); i++) {
List row = new ArrayList();
row.add(sentence.getTokens().get(i).getSyntacticTag());
row.add(sentence.getTokens().get(i).getChunkTag());
row.add(sentence.getTokens().get(i).getLexeme());
row.add(Arrays.toString(sentence.getTokens().get(i).getLemmas()));
row.add(sentence.getTokens().get(i).getPOSTag() + "=" + sentence.getTokens().get(i).getFeatures());
analysis.add(row);
}
return analysis;
}
private void evaluateMistakes(List> sentencesMistakes, List sentences, Rule rule) throws Exception {
int zeroMatches = 0;
int wrongMatches = 0;
int correctMatches = 0;
// sentencesMistakes.size() equals to the number of examples.
int sentence = 0;
for (List mistakes : sentencesMistakes) {
Example example = rule.getExample().get(sentence);
// Prints the incorrect and correct examples.
this.printExamples(example, sentences.get(sentence));
// Checks for null in case an exception occurred.
if (mistakes != null) {
if (mistakes.isEmpty()) {
zeroMatches++;
} else { // There were mistakes.
for (Mistake mistake : mistakes) {
if (((MistakeImpl) mistake).getRuleIdentifier().equals(appendPrefix(rule.getId()))) {
correctMatches++;
if(isValidSuggestion(example, mistake)) {
this.out.append("
\n");
boolean first = true;
for (Entry> entry : this.exceptions.entrySet()) {
Long id = entry.getKey();
List causes = entry.getValue();
for (String cause : entry.getValue()) {
if (first) {
this.out.append("