// Decompiled by DJ v3.9.9.91 Copyright 2005 Atanas Neshkov Date: 08/06/2011 13:49:18
// Home Page : http://members.fortunecity.com/neshkov/dj.html - Check often for new version!
// Decompiler options: packimports(3)
// Source File Name: FuzzyMembership.java
package fuzzygrammar;
import IEFG.automaticLearner;
import IEFG.grammarBuilder;
import java.io.*;
import java.util.*;
// Referenced classes of package fuzzygrammar:
// FuzzyGrammar, Cost, Rule
public class FuzzyMembership extends automaticLearner
{
public static void main2(String args[])
{
if(args.length == 3 || args.length == 5)
{
if(!args[0].endsWith(".xml"))
{
System.out.println("Note that we need an XML formatted grammar file");
help();
}
FuzzyMembership fm = new FuzzyMembership(args[0]);
String element = args[1];
String test = args[2];
int row = 0;
int column = 0;
if(DEBUG)
System.out.println("Similarity Value is ");
if(args.length == 5)
{
row = Integer.valueOf(args[3]).intValue();
column = Integer.valueOf(args[4]).intValue();
double sim = fm.evaluateMembership(test, element, row, column);
if(DEBUG)
System.out.println(sim);
} else
{
String values[] = {
"1"
};
double sim = fm.evaluateMembership(values, element);
if(DEBUG)
System.out.println((new StringBuilder(">>> sim:")).append(sim).toString());
}
} else
{
help();
}
}
public static double measureParsing(String file, String entry)
{
FuzzyMembership fm = new FuzzyMembership(file);
double score = fm.evaluateMembership(entry.replace(",", "").replace(".", "").replace("?", "").replace("!", "").trim(), "finalgrammar");
return score;
}
public static double measureParsing(String file, String entry, String targetName)
{
FuzzyMembership fm = new FuzzyMembership(file);
double score = fm.evaluateMembership(entry.replace(",", "").replace(".", "").replace("?", "").replace("!", "").trim(), targetName);
return score;
}
public static void main(String args[])
{
String file = args[0];
String entry = args[2];
entry = "Pizzeria Takeaway 123 Upper Queens Road";
FuzzyGrammar fuzzGram = new FuzzyGrammar();
fuzzGram.execFuzzyGram((new StringBuilder(String.valueOf(file.substring(0, file.indexOf("."))))).append(".txt").toString());
FuzzyMembership fm = new FuzzyMembership(file);
double score = fm.evaluateMembership(entry.replace(",", "").replace(".", "").replace("?", "").replace("!", "").trim(), args[1]);
System.out.println((new StringBuilder(">>>")).append(entry).append(":").append(score).toString());
}
public static void execFuzzMem(String test)
{
File f1 = new File(automaticLearner.strToGramMemResult);
f1.delete();
fuzzGram.execFuzzyGram(automaticLearner.gramFileTXT);
FuzzyMembership fm = new FuzzyMembership(automaticLearner.gramFileHeaderXML);
ArrayList finalTables = new ArrayList();
String minElem = "";
String thegrammar = "";
for(int i = 0; i < grammarBuilder.grammarNamesFromFile.size(); i++)
{
int row = 0;
int column = 0;
if(printCost)
System.out.println("Similarity Value is ");
fm.evaluateMembership(test, (String)grammarBuilder.grammarNamesFromFile.get(i));
Cost table[][] = fm.getMinimalCost((ArrayList)costs.get(grammarBuilder.grammarNamesFromFile.get(i)));
finalTables.add(table);
TheFinalTables.add(table);
}
double tot = 0.0D;
double min = 0.0D;
for(int j = 0; j < finalTables.size(); j++)
{
try
{
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(automaticLearner.strToGramMemResult, true), "UTF8"));
out.write((new StringBuilder(String.valueOf((String)grammarBuilder.grammarNamesFromFile.get(j)))).append("\n").toString());
if(printCost)
System.out.println("******************");
if(printCost)
System.out.println((new StringBuilder("j is ")).append(j).toString());
Cost cost[][] = (Cost[][])finalTables.get(j);
for(int a = 0; a < cost.length; a++)
{
for(int b = 0; b < a; b++);
for(int c = a; c < cost.length; c++)
if(cost[a][c] != null)
{
tot = cost[a][c].getInsert() + cost[a][c].getDelete() + cost[a][c].getSubstitute();
if(printCost)
System.out.println((new StringBuilder(String.valueOf(cost[a][c].getInsert()))).append(" ").append(cost[a][c].getDelete()).append(" ").append(cost[a][c].getSubstitute()).append("--").toString());
out.write((new StringBuilder(String.valueOf(cost[a][c].getInsert()))).append(" ").append(cost[a][c].getDelete()).append(" ").append(cost[a][c].getSubstitute()).append("--").toString());
} else
if(printCost)
System.out.print("undefined |");
if(printCost)
System.out.println("");
out.write("\n");
}
out.close();
}
catch(IOException e)
{
e.printStackTrace();
}
if(printCost)
System.out.println("******************");
}
if(printCost)
System.out.println((new StringBuilder("thegrammar is ")).append(thegrammar).toString());
}
public static void printCell(Cost cost)
{
if(DEBUG)
System.out.println((new StringBuilder(String.valueOf(cost.getDelete()))).append(" ").append(cost.getInsert()).append(" ").append(cost.getSubstitute()).toString());
}
public static void help()
{
if(DEBUG)
{
System.out.println("The program requires THREE or FIVE Parameters to run.");
System.out.println("1. Three Parameters");
System.out.println("FuzzyMembership [grammar file (xml format)] [grammar element] [string to be evaluated]");
System.out.println("2. Five Parameters");
System.out.println("FuzzyMembership [grammar file (xml format)] [grammar element] [string to be evaluated] [row] [column]");
System.out.println("In case that you don't have an XML formatted grammar file");
System.out.println("Please run Java FuzzyGrammar [grammar file] (plain text format)");
System.exit(0);
}
}
public FuzzyMembership(String grammarfile)
{
grammar = (new FuzzyGrammar()).readRules(grammarfile);
costs = new HashMap();
preStr = "";
preGram = "";
}
public FuzzyMembership(HashMap externalgrammar)
{
grammar = externalgrammar;
costs = new HashMap();
preStr = "";
preGram = "";
}
public double evaluateMembership(String string, String grammarElemName, int row, int column)
{
String content[] = string.split("\\s");
if(row > content.length - 1 || column > content.length - 1 || row > column)
{
System.err.println("Error: row/column value is wrong!");
return -1D;
}
ArrayList grammarElement = (ArrayList)grammar.get(grammarElemName);
if(grammarElement == null)
{
System.err.println((new StringBuilder("Error: Grammar Element \"")).append(grammarElemName).append("\" doesn't exist. Please re-check your grammar definition file.").toString());
return -1D;
}
if(preStr.equalsIgnoreCase(string) && preGram.equalsIgnoreCase(grammarElemName))
{
if(DEBUG)
System.out.println("We are to reuse the tables.\n");
double provisionalvalue = 1.0D - membership(grammarElemName, row, column);
return provisionalvalue < 0.0D ? 0.0D : provisionalvalue;
} else
{
preStr = string;
preGram = grammarElemName;
costs = new HashMap();
calculateGrammarCost(string, grammarElemName);
double provisionalvalue = 1.0D - membership(grammarElemName, row, column);
return provisionalvalue < 0.0D ? 0.0D : provisionalvalue;
}
}
public double evaluateMembership(String content[], String grammarElemName, int row, int column)
{
if(row > content.length - 1 || column > content.length - 1 || row > column)
{
System.err.println("Error: row/column value is wrong!");
return -1D;
}
ArrayList grammarElement = (ArrayList)grammar.get(grammarElemName);
if(grammarElement == null)
{
System.out.println(grammarElemName);
System.err.println("Error: Invalid grammar element name!");
return -1D;
}
String strValueOfContent = Arrays.deepToString(content);
if(preStr.equalsIgnoreCase(strValueOfContent) && preGram.equalsIgnoreCase(grammarElemName))
{
if(DEBUG)
System.out.println("We are to reuse the tables.\n");
double provisionalvalue = 1.0D - membership(grammarElemName, row, column);
return provisionalvalue < 0.0D ? 0.0D : provisionalvalue;
} else
{
preStr = strValueOfContent;
preGram = grammarElemName;
costs = new HashMap();
calculateGrammarCost(content, grammarElemName);
double provisionalvalue = 1.0D - membership(grammarElemName, row, column);
return provisionalvalue < 0.0D ? 0.0D : provisionalvalue;
}
}
private HashMap evaluateUnitRules(String content[])
{
HashMap costs = new HashMap();
Object keys[] = grammar.keySet().toArray();
for(int i = 0; i < keys.length; i++)
{
ArrayList grammarElement = (ArrayList)grammar.get((String)keys[i]);
if(((String)keys[i]).equalsIgnoreCase("Keywords"))
{
String keywords[] = ((Rule)grammarElement.get(0)).getRuleDetails().split("#DELI;");
for(int j = 0; j < keywords.length; j++)
{
Rule arule = new Rule();
arule.setMaxLength(1);
arule.setMinLength(1);
arule.setRuletype("t");
arule.setContenttype("e");
arule.setRulename(keywords[j]);
arule.setRuleDetails(keywords[j]);
ArrayList rulecost = new ArrayList();
rulecost.add(evaluateRule(content, arule));
costs.put(keywords[j], rulecost);
}
} else
{
Rule arule = (Rule)grammarElement.get(0);
if(arule.getContenttype().equalsIgnoreCase("e") || arule.getContenttype().equalsIgnoreCase("r") || arule.getContenttype().equalsIgnoreCase("f"))
{
ArrayList rulecost = evaluateGrammarElemCost(content, grammarElement);
costs.put((String)keys[i], rulecost);
}
}
}
return costs;
}
private ArrayList evaluateGrammarElemCost(String content[], ArrayList rules)
{
ArrayList grammarElemCost = new ArrayList();
for(int i = 0; i < rules.size(); i++)
grammarElemCost.add(evaluateRule(content, (Rule)rules.get(i)));
return grammarElemCost;
}
private Cost[][] evaluateRule(String values[], Rule arule)
{
Cost cost[][] = new Cost[values.length][values.length];
int dim = values.length;
for(int i = 0; i < dim; i++)
{
double current = isConformingToRule(values[i], arule);
current = current < 0.0D ? 1.0D : current;
Cost curCell = new Cost();
curCell.setDelete(0.0D);
curCell.setInsert(0.0D);
curCell.setSubstitute(current);
cost[i][i] = curCell;
if(i < dim - 1)
{
for(int k = i + 1; k <= dim - 1; k++)
{
double next = isConformingToRule(values[k], arule);
next = next < 0.0D ? 1.0D : next;
if(Math.min(current, next) < 0.0D || Math.min(current, next) > 1.0D)
break;
Cost nextCell = new Cost();
nextCell.setDelete(cost[i][k - 1].getDelete() + 1.0D);
nextCell.setInsert(0.0D);
nextCell.setSubstitute(Math.min(current, next));
cost[i][k] = nextCell;
current = Math.min(current, next);
}
}
}
return cost;
}
public double evaluateMembership(String string, String grammarElemName)
{
ArrayList grammarElement = (ArrayList)grammar.get(grammarElemName);
if(grammarElement == null)
{
System.out.println(grammarElemName);
System.out.println("Error: Invalid grammar element name!");
return -1D;
}
if(preStr.equalsIgnoreCase(string) && preGram.equalsIgnoreCase(grammarElemName))
{
double provisionalvalue = 1.0D - membership(grammarElemName);
return provisionalvalue < 0.0D ? 0.0D : provisionalvalue;
} else
{
preStr = string;
preGram = grammarElemName;
costs = new HashMap();
calculateGrammarCost(string, grammarElemName);
double provisionalvalue = 1.0D - membership(grammarElemName);
return provisionalvalue < 0.0D ? 0.0D : provisionalvalue;
}
}
private void calculateGrammarCost(String string, String grammarElemName)
{
ArrayList grammarElement = (ArrayList)grammar.get(grammarElemName);
if(grammarElement == null)
{
System.out.println(grammarElemName);
System.out.println("Error: Invalid grammar element name!");
return;
}
if(!((Rule)grammarElement.get(0)).getRuletype().equalsIgnoreCase("N"))
{
storeCost(grammarElemName, evaluateGrammarElemCost(string, grammarElement));
} else
{
costs = evaluateUnitRules(string);
for(int i = 0; i < grammarElement.size(); i++)
{
String ruledetails = ((Rule)grammarElement.get(i)).getRuleDetails();
String detailedinfo[] = ruledetails.split("#DELI;");
if(detailedinfo.length == 1)
{
if(FuzzyGrammar.isKeywords(ruledetails, grammar))
{
storeCost(grammarElemName, (ArrayList)costs.get(ruledetails));
} else
{
if(DEBUG)
System.out.println(ruledetails);
if(((Rule)((ArrayList)grammar.get(removeBrackets(ruledetails))).get(0)).getRuletype().equalsIgnoreCase("N"))
{
evaluateComplexRule(grammarElemName, ruledetails);
storeCost(grammarElemName, (ArrayList)costs.get(removeBrackets(ruledetails)));
} else
{
storeCost(grammarElemName, evaluateGrammarElemCost(string, (ArrayList)grammar.get(removeBrackets(ruledetails))));
}
}
} else
{
if(DEBUG)
System.out.println((new StringBuilder("----------")).append(grammarElemName).append(" ").append(i).toString());
evaluateComplexRule(grammarElemName, ruledetails);
}
}
}
}
private void storeCost(String grammarElemName, ArrayList evaluateGrammarElemCost)
{
ArrayList costlist = (ArrayList)costs.get(grammarElemName);
if(costlist == null)
{
costs.put(grammarElemName, evaluateGrammarElemCost);
} else
{
costlist.addAll(evaluateGrammarElemCost);
costs.put(grammarElemName, costlist);
}
}
public double evaluateMembership(String content[], String grammarElemName)
{
ArrayList grammarElement = (ArrayList)grammar.get(grammarElemName);
if(grammarElement == null)
{
if(DEBUG)
System.out.println(grammarElemName);
if(DEBUG)
System.out.println("Error: Invalid grammar element name!");
return -1D;
}
String strValueOfContent = Arrays.deepToString(content);
if(preStr.equalsIgnoreCase(strValueOfContent) && preGram.equalsIgnoreCase(grammarElemName))
{
double provisionalvalue = 1.0D - membership(grammarElemName);
return provisionalvalue < 0.0D ? 0.0D : provisionalvalue;
} else
{
preStr = strValueOfContent;
preGram = grammarElemName;
costs = new HashMap();
calculateGrammarCost(content, grammarElemName);
double provisionalvalue = 1.0D - membership(grammarElemName);
return provisionalvalue < 0.0D ? 0.0D : provisionalvalue;
}
}
private void calculateGrammarCost(String content[], String grammarElemName)
{
ArrayList grammarElement = (ArrayList)grammar.get(grammarElemName);
if(grammarElement == null)
{
System.out.println(grammarElemName);
System.out.println("Error: Invalid grammar element name!");
return;
}
if(!((Rule)grammarElement.get(0)).getRuletype().equalsIgnoreCase("N"))
{
costs.put(grammarElemName, evaluateGrammarElemCost(content, grammarElement));
} else
{
costs = evaluateUnitRules(content);
for(int i = 0; i < grammarElement.size(); i++)
{
String ruledetails = ((Rule)grammarElement.get(i)).getRuleDetails();
String detailedinfo[] = ruledetails.split("#DELI;");
if(detailedinfo.length == 1)
{
if(FuzzyGrammar.isKeywords(ruledetails, grammar))
{
storeCost(grammarElemName, (ArrayList)costs.get(ruledetails));
} else
{
System.out.println(ruledetails);
if(((Rule)((ArrayList)grammar.get(ruledetails)).get(0)).getRuletype().equalsIgnoreCase("N"))
{
calculateGrammarCost(content, ruledetails);
ArrayList costlist = (ArrayList)costs.get(grammarElemName);
if(costlist == null)
{
costs.put(grammarElemName, (ArrayList)costs.get(ruledetails));
} else
{
costlist.addAll((Collection)costs.get(ruledetails));
costs.put(grammarElemName, costlist);
}
} else
{
ArrayList tempcost = evaluateGrammarElemCost(content, (ArrayList)grammar.get(ruledetails));
ArrayList grammarcost = (ArrayList)costs.get(grammarElemName);
if(grammarcost != null)
{
grammarcost.addAll(tempcost);
costs.put(grammarElemName, grammarcost);
} else
{
costs.put(grammarElemName, tempcost);
}
}
}
} else
{
evaluateComplexRule(grammarElemName, ruledetails);
}
}
}
}
private String removeBrackets(String string)
{
String values[] = string.split("#DELI;");
String removed = "";
for(int i = 0; i < values.length; i++)
{
String string1 = values[i];
if(string1.indexOf("[") == 0 && string1.indexOf("]") == string1.length() - 1)
string1 = string1.substring(string1.indexOf("[") + 1, string1.indexOf("]"));
if(removed.length() == 0)
removed = string1;
else
removed = (new StringBuilder(String.valueOf(removed))).append("#DELI;").append(string1).toString();
}
return removed;
}
private boolean isequal(String preAry[], String content[])
{
if(preAry.length == content.length)
{
for(int i = 0; i < preAry.length; i++)
if(!preAry[i].equalsIgnoreCase(content[i]))
return false;
return true;
} else
{
return false;
}
}
private double membership(String grammarElemName, int row, int column)
{
return sumCost(getMinimalCost((ArrayList)costs.get(grammarElemName))[row][column]) / (double)((column - row) + 1);
}
private double membership(String grammarElemName)
{
if(DEBUG)
System.out.println(grammarElemName);
Cost finalcost[][] = getMinimalCost((ArrayList)costs.get(grammarElemName));
int dim = finalcost.length;
if(DEBUG)
{
System.out.println((new StringBuilder("dim is ")).append(dim).toString());
System.out.println("The cost table is shown below: ");
printCost(finalcost);
}
return sumCost(finalcost[0][dim - 1]) / (double)dim;
}
private void evaluateComplexRule(String grammarElemName, String rule)
{
if(DEBUG)
System.out.println(rule);
String uniRuleSet[] = rule.split("#DELI;");
Cost complexRuleCost[][] = (Cost[][])null;
int i = 0;
int globalmin = 0;
boolean keyword_curr = false;
boolean keyword_next = false;
for(; i < uniRuleSet.length - 1; i++)
if(i == 0)
{
keyword_curr = FuzzyGrammar.isKeywords(uniRuleSet[i], grammar);
keyword_next = FuzzyGrammar.isKeywords(uniRuleSet[i + 1], grammar);
int minlength1 = 0;
int minlength2 = 0;
if(DEBUG)
System.out.println((new StringBuilder("uniRuleSet[i]: ")).append(uniRuleSet[i]).append(" uniRuleSet[i+1]: ").append(uniRuleSet[i + 1]).toString());
if(uniRuleSet[i].indexOf("[") == 0 && uniRuleSet[i].indexOf("]") == uniRuleSet[i].length() - 1)
{
minlength1 = 0;
globalmin += 0;
uniRuleSet[i] = uniRuleSet[i].substring(uniRuleSet[i].indexOf("[") + 1, uniRuleSet[i].indexOf("]"));
} else
{
minlength1 = keyword_curr ? 1 : getGrammarElemMinLength(uniRuleSet[i]);
globalmin += minlength1;
}
if(uniRuleSet[i + 1].indexOf("[") == 0 && uniRuleSet[i + 1].indexOf("]") == uniRuleSet[i + 1].length() - 1)
{
minlength2 = 0;
globalmin += 0;
uniRuleSet[i + 1] = uniRuleSet[i + 1].substring(uniRuleSet[i + 1].indexOf("[") + 1, uniRuleSet[i + 1].indexOf("]"));
} else
{
minlength2 = keyword_next ? 1 : getGrammarElemMinLength(uniRuleSet[i + 1]);
globalmin += minlength2;
}
if(DEBUG)
System.out.println((new StringBuilder("uniRuleSet[i]: ")).append(uniRuleSet[i]).toString());
if(!keyword_curr && costs.get(uniRuleSet[i]) == null)
{
ArrayList element = (ArrayList)grammar.get(uniRuleSet[i]);
for(int k = 0; k < element.size(); k++)
evaluateComplexRule(uniRuleSet[i], ((Rule)element.get(k)).getRuleDetails());
}
if(!keyword_next && costs.get(uniRuleSet[i + 1]) == null)
{
ArrayList element = (ArrayList)grammar.get(uniRuleSet[i + 1]);
for(int k = 0; k < element.size(); k++)
evaluateComplexRule(uniRuleSet[i + 1], ((Rule)element.get(k)).getRuleDetails());
}
if(DEBUG)
System.out.println((new StringBuilder("merging ")).append(uniRuleSet[i]).append(" ").append(uniRuleSet[i + 1]).append(" with individual minlength ").append(minlength1).append(" ").append(minlength2).toString());
complexRuleCost = combineCosts((ArrayList)costs.get(uniRuleSet[i]), (ArrayList)costs.get(uniRuleSet[i + 1]), minlength1, minlength2);
printCost(complexRuleCost);
} else
{
keyword_next = FuzzyGrammar.isKeywords(uniRuleSet[i + 1], grammar);
int minlength1 = globalmin;
int minlength2 = 0;
if(uniRuleSet[i + 1].indexOf("[") == 0 && uniRuleSet[i + 1].indexOf("]") == uniRuleSet[i + 1].length() - 1)
{
minlength2 = 0;
globalmin += 0;
uniRuleSet[i + 1] = uniRuleSet[i + 1].substring(uniRuleSet[i + 1].indexOf("[") + 1, uniRuleSet[i + 1].indexOf("]"));
keyword_next = FuzzyGrammar.isKeywords(uniRuleSet[i + 1], grammar);
} else
{
minlength2 = keyword_next ? 1 : getGrammarElemMinLength(uniRuleSet[i + 1]);
globalmin += minlength2;
}
if(!keyword_next && costs.get(uniRuleSet[i + 1]) == null)
{
ArrayList element = (ArrayList)grammar.get(uniRuleSet[i + 1]);
for(int k = 0; k < element.size(); k++)
{
if(DEBUG)
System.out.println((new StringBuilder(String.valueOf(uniRuleSet[i + 1]))).append(" ").append(k).toString());
evaluateComplexRule(uniRuleSet[i + 1], ((Rule)element.get(k)).getRuleDetails());
}
}
if(DEBUG)
{
if(DEBUG)
System.out.println("ComplexRuleCost");
printCost(complexRuleCost);
if(DEBUG)
System.out.println(uniRuleSet[i + 1]);
printCost((Cost[][])((ArrayList)costs.get(uniRuleSet[i + 1])).get(0));
}
if(DEBUG)
System.out.println((new StringBuilder("merging ")).append(uniRuleSet[i + 1]).append(" with individual minlength ").append(minlength1).append(" ").append(minlength2).toString());
complexRuleCost = combineCosts(complexRuleCost, (ArrayList)costs.get(uniRuleSet[i + 1]), globalmin - minlength2, minlength2);
printCost(complexRuleCost);
}
ArrayList grammarcost = (ArrayList)costs.get(grammarElemName);
if(grammarcost != null)
{
if(uniRuleSet.length == 1)
{
if(rule.indexOf("[") == 0 && rule.indexOf("]") == uniRuleSet[i].length() - 1)
{
rule = rule.substring(rule.indexOf("[") + 1, rule.indexOf("]"));
if(DEBUG)
System.out.println((new StringBuilder("------")).append(rule).toString());
ArrayList tempcost = (ArrayList)costs.get(rule);
if(tempcost == null)
{
ArrayList element = (ArrayList)grammar.get(rule);
for(int k = 0; k < element.size(); k++)
{
if(DEBUG)
System.out.println((new StringBuilder(String.valueOf(rule))).append(" ").append(k).toString());
evaluateComplexRule(rule, ((Rule)element.get(k)).getRuleDetails());
}
}
if(DEBUG)
System.out.println((new StringBuilder("******")).append(rule).toString());
tempcost = (ArrayList)costs.get(rule);
for(int icount = 0; icount < tempcost.size(); icount++)
grammarcost.add((Cost[][])tempcost.get(icount));
} else
{
if(DEBUG)
System.out.println(rule);
ArrayList tempcost = (ArrayList)costs.get(rule);
globalmin += getGrammarElemMinLength(rule);
if(tempcost == null)
{
ArrayList element = (ArrayList)grammar.get(rule);
for(int k = 0; k < element.size(); k++)
{
if(DEBUG)
System.out.println((new StringBuilder(String.valueOf(rule))).append(" ").append(k).toString());
evaluateComplexRule(rule, ((Rule)element.get(k)).getRuleDetails());
}
}
tempcost = (ArrayList)costs.get(rule);
for(int icount = 0; icount < tempcost.size(); icount++)
grammarcost.add((Cost[][])tempcost.get(icount));
}
} else
{
grammarcost.add(complexRuleCost);
}
costs.put(grammarElemName, grammarcost);
} else
if(uniRuleSet.length == 1)
{
if(rule.indexOf("[") == 0 && rule.indexOf("]") == uniRuleSet[i].length() - 1)
{
rule = rule.substring(rule.indexOf("[") + 1, rule.indexOf("]"));
if(DEBUG)
System.out.println((new StringBuilder("------")).append(rule).toString());
ArrayList tempcost = (ArrayList)costs.get(rule);
if(tempcost == null)
{
ArrayList element = (ArrayList)grammar.get(rule);
for(int k = 0; k < element.size(); k++)
{
if(DEBUG)
System.out.println((new StringBuilder(String.valueOf(rule))).append(" ").append(k).toString());
evaluateComplexRule(rule, ((Rule)element.get(k)).getRuleDetails());
}
}
if(DEBUG)
System.out.println((new StringBuilder("******")).append(rule).toString());
tempcost = (ArrayList)costs.get(rule);
costs.put(grammarElemName, tempcost);
} else
{
if(DEBUG)
System.out.println(rule);
ArrayList tempcost = (ArrayList)costs.get(rule);
globalmin += getGrammarElemMinLength(rule);
if(tempcost == null)
{
ArrayList element = (ArrayList)grammar.get(rule);
for(int k = 0; k < element.size(); k++)
{
if(DEBUG)
System.out.println((new StringBuilder(String.valueOf(rule))).append(" ").append(k).toString());
evaluateComplexRule(rule, ((Rule)element.get(k)).getRuleDetails());
}
}
tempcost = (ArrayList)costs.get(rule);
costs.put(grammarElemName, tempcost);
}
} else
{
grammarcost = new ArrayList();
grammarcost.add(complexRuleCost);
costs.put(grammarElemName, grammarcost);
}
}
public int getGrammarElemMinLength(String grammarElemName)
{
ArrayList grammarElement = (ArrayList)grammar.get(grammarElemName);
if(grammarElement != null)
{
int minlength = 0x7fffffff;
for(int i = 0; i < grammarElement.size(); i++)
minlength = minlength <= ((Rule)grammarElement.get(i)).getMinLength() ? minlength : ((Rule)grammarElement.get(i)).getMinLength();
return minlength;
} else
{
System.out.println(grammarElemName);
System.out.println("Invalid rule at getGrammarElemMinLength!");
return 0;
}
}
public int getGrammarElemMaxLength(String grammarElemName)
{
ArrayList grammarElement = (ArrayList)grammar.get(grammarElemName);
if(grammarElement != null)
{
int maxlength = -1;
for(int i = 0; i < grammarElement.size(); i++)
maxlength = maxlength >= ((Rule)grammarElement.get(i)).getMaxLength() ? maxlength : ((Rule)grammarElement.get(i)).getMaxLength();
return maxlength;
} else
{
System.out.println("Invalid rule at getGrammarElemMaxLength method!");
System.exit(1);
return 0;
}
}
private Cost[][] getMinimalCost(ArrayList grammarElemCost)
{
Cost pre[][] = (Cost[][])grammarElemCost.get(0);
int dim = pre.length;
for(int i = 1; i < grammarElemCost.size(); i++)
{
Cost curr[][] = (Cost[][])grammarElemCost.get(i);
for(int k = 0; k < dim; k++)
{
for(int j = k; j < dim; j++)
if(sumCost(pre[k][j]) > sumCost(curr[k][j]))
pre[k][j] = curr[k][j];
}
}
return pre;
}
private Cost[][] combineCosts(ArrayList curr, ArrayList next, int minlength1, int minlength2)
{
int dim = ((Cost[][])curr.get(0)).length;
Cost temp[][] = new Cost[dim][dim];
for(int i = 0; i < dim; i++)
{
for(int j = i; j < dim; j++)
temp[i][j] = combineCell(i, j, getMinimalCost(curr), getMinimalCost(next), minlength1, minlength2);
}
return temp;
}
private Cost combineCell(int i, int j, Cost curr[][], Cost next[][], int minlength1, int minlength2)
{
Cost deleteCost = null;
if(curr[i][j] != null)
{
deleteCost = new Cost();
deleteCost.setInsert(curr[i][j].getInsert() + (double)minlength2);
deleteCost.setDelete(curr[i][j].getDelete());
deleteCost.setSubstitute(curr[i][j].getSubstitute());
}
Cost insertCost = null;
if(next[i][j] != null)
{
insertCost = new Cost();
insertCost.setInsert(next[i][j].getInsert() + (double)minlength1);
insertCost.setDelete(next[i][j].getDelete());
insertCost.setSubstitute(next[i][j].getSubstitute());
}
double finalcost = 1.7976931348623157E+308D;
Cost ultimate = null;
if(deleteCost == null && insertCost == null)
{
finalcost = 1.7976931348623157E+308D;
} else
{
if(deleteCost == null && insertCost != null)
{
finalcost = sumCost(insertCost);
ultimate = insertCost;
}
if(insertCost == null && deleteCost != null)
{
finalcost = sumCost(deleteCost);
ultimate = deleteCost;
}
if(insertCost != null && deleteCost != null)
if(sumCost(deleteCost) > sumCost(insertCost))
{
finalcost = sumCost(insertCost);
ultimate = insertCost;
} else
{
finalcost = sumCost(deleteCost);
ultimate = deleteCost;
}
}
for(int k = i; k < j; k++)
if(curr[i][k] != null && next[k + 1][j] != null)
{
Cost tempcost = addCost(curr[i][k], next[k + 1][j]);
if(sumCost(tempcost) < finalcost)
{
finalcost = sumCost(tempcost);
ultimate = tempcost;
}
}
return ultimate;
}
private Cost addCost(Cost cost1, Cost cost2)
{
Cost sum = new Cost();
sum.setInsert(cost1.getInsert() + cost2.getInsert());
sum.setDelete(cost1.getDelete() + cost2.getDelete());
sum.setSubstitute(cost1.getSubstitute() + cost2.getSubstitute());
return sum;
}
private double sumCost(Cost cost)
{
return cost.getDelete() + cost.getInsert() + cost.getSubstitute();
}
private Cost[][] combineCosts(Cost curr[][], ArrayList next, int minlength1, int minlength2)
{
int dim = curr.length;
Cost temp[][] = new Cost[dim][dim];
for(int i = 0; i < temp.length; i++)
{
for(int j = i; j < temp.length; j++)
temp[i][j] = combineCell(i, j, curr, getMinimalCost(next), minlength1, minlength2);
}
return temp;
}
public HashMap evaluateUnitRules(String string)
{
HashMap costs = new HashMap();
Object keys[] = grammar.keySet().toArray();
for(int i = 0; i < keys.length; i++)
{
ArrayList grammarElement = (ArrayList)grammar.get((String)keys[i]);
if(((String)keys[i]).equalsIgnoreCase("Keywords"))
{
String keywords[] = ((Rule)grammarElement.get(0)).getRuleDetails().split("#DELI;");
for(int j = 0; j < keywords.length; j++)
{
Rule arule = new Rule();
arule.setMaxLength(1);
arule.setMinLength(1);
arule.setRuletype("t");
arule.setContenttype("e");
arule.setRulename(keywords[j]);
arule.setRuleDetails(keywords[j]);
ArrayList rulecost = new ArrayList();
rulecost.add(evaluateRule(string, arule));
costs.put(keywords[j], rulecost);
if(DEBUG)
System.out.println(keywords[j]);
}
} else
{
Rule arule = (Rule)grammarElement.get(0);
if(arule.getContenttype().equalsIgnoreCase("e") || arule.getContenttype().equalsIgnoreCase("r") || arule.getContenttype().equalsIgnoreCase("f"))
{
ArrayList rulecost = evaluateGrammarElemCost(string, grammarElement);
costs.put((String)keys[i], rulecost);
if(DEBUG)
System.out.println(keys[i]);
}
}
}
return costs;
}
private ArrayList evaluateGrammarElemCost(String string, ArrayList rules)
{
ArrayList grammarElemCost = new ArrayList();
for(int i = 0; i < rules.size(); i++)
grammarElemCost.add(evaluateRule(string, (Rule)rules.get(i)));
return grammarElemCost;
}
private Cost[][] evaluateRule(String string, Rule arule)
{
String values[] = string.split("\\s");
Cost cost[][] = new Cost[values.length][values.length];
int dim = values.length;
for(int i = 0; i < dim; i++)
{
double current = isConformingToRule(values[i], arule);
current = current < 0.0D ? 1.0D : current;
Cost curCell = new Cost();
curCell.setDelete(0.0D);
curCell.setInsert(0.0D);
curCell.setSubstitute(current);
cost[i][i] = curCell;
if(i < dim - 1)
{
for(int k = i + 1; k <= dim - 1; k++)
{
double next = isConformingToRule(values[k], arule);
next = next < 0.0D ? 1.0D : next;
if(Math.min(current, next) < 0.0D || Math.min(current, next) > 1.0D)
break;
Cost nextCell = new Cost();
nextCell.setDelete(cost[i][k - 1].getDelete() + 1.0D);
nextCell.setInsert(0.0D);
nextCell.setSubstitute(Math.min(current, next));
cost[i][k] = nextCell;
current = Math.min(current, next);
}
}
}
if(DEBUG)
{
System.out.println(arule.getRulename());
printCost(cost);
}
return cost;
}
private double isConformingToRule(String string, Rule arule)
{
if(arule.getRuletype().equalsIgnoreCase("t"))
{
if(arule.getContenttype().equalsIgnoreCase("f"))
{
String fuzzyset[] = arule.getRuleDetails().split("#DELI;");
for(int i = 0; i < fuzzyset.length; i++)
{
System.out.println((new StringBuilder(String.valueOf(fuzzyset[i].split("~")[0]))).append(" ===== ").append(string).toString());
if(fuzzyset[i].split("~")[0].equalsIgnoreCase(string))
return 1.0D - Double.parseDouble(fuzzyset[i].split("~")[1]);
}
}
if(arule.getContenttype().equalsIgnoreCase("e"))
{
String crispset[] = arule.getRuleDetails().split("#DELI;");
for(int i = 0; i < crispset.length; i++)
if(crispset[i].equalsIgnoreCase(string))
return 0.0D;
return 1.0D;
}
if(arule.getContenttype().equalsIgnoreCase("r"))
{
String regs[] = arule.getRuleDetails().split("#INSC;");
for(int i = 0; i < regs.length; i++)
{
if(DEBUG)
System.out.println((new StringBuilder(String.valueOf(string))).append(">>>").append(regs[i]).toString());
if(!string.matches(regs[i]))
return -1D;
}
return 0.0D;
}
}
return -1D;
}
private void printCost(Cost cost[][])
{
for(int i = 0; i < cost.length; i++)
{
for(int k = 0; k < i; k++)
if(printCost)
System.out.print(" \t");
for(int j = i; j < cost.length; j++)
if(cost[i][j] != null)
{
if(printCost)
System.out.print((new StringBuilder(String.valueOf(cost[i][j].getInsert()))).append(" ").append(cost[i][j].getDelete()).append(" ").append(cost[i][j].getSubstitute()).append("|").toString());
} else
if(printCost)
System.out.print("undefined |");
if(printCost)
System.out.println("");
}
}
public Cost findRowMinimumCost(int row, Cost table[][])
{
Cost cost = new Cost();
cost.setDelete(1.7976931348623157E+308D);
cost.setInsert(1.7976931348623157E+308D);
cost.setSubstitute(1.7976931348623157E+308D);
int rownum = table.length;
int columnnum = table[0].length;
if(row < 0 || row > rownum - 1)
return null;
for(int i = row; i < columnnum; i++)
if(sumCost(table[row][i]) < sumCost(cost))
cost = table[row][i];
return cost;
}
public Cost findColumnMiniCost(int column, Cost table[][])
{
Cost cost = new Cost();
cost.setDelete(1.7976931348623157E+308D);
cost.setInsert(1.7976931348623157E+308D);
cost.setSubstitute(1.7976931348623157E+308D);
int rownum = table.length;
int columnnum = table[0].length;
if(column < 0 || column > columnnum - 1)
return null;
for(int i = 0; i <= column; i++)
if(sumCost(table[i][column]) < sumCost(cost))
cost = table[i][column];
return cost;
}
public Cost findMinimumCost(Cost table[][])
{
Cost cost = new Cost();
cost.setDelete(1.7976931348623157E+308D);
cost.setInsert(1.7976931348623157E+308D);
cost.setSubstitute(1.7976931348623157E+308D);
Cost temp = null;
int rownum = table.length;
int columnnum = table[0].length;
for(int i = 0; i < rownum; i++)
if(sumCost(cost) < sumCost(temp = findRowMinimumCost(i, table)))
cost = temp;
return cost;
}
public Cost findSpecificCost(String element, int row, int column)
{
ArrayList temp = (ArrayList)costs.get(element);
if(temp == null)
return null;
Cost table[][] = getMinimalCost(temp);
int rownum = table.length;
int columnnum = table[0].length;
if(row < 0 || column < 0 || row > rownum - 1 || column > columnnum - 1)
return null;
else
return table[row][column];
}
public String[] findGrammarElements()
{
if(grammar != null)
{
String elements[] = new String[grammar.size()];
elements = (String[])grammar.keySet().toArray(elements);
return elements;
} else
{
return null;
}
}
public Cost findRowMinimumCost(String element, int row)
{
ArrayList temp = (ArrayList)costs.get(element);
if(temp == null)
{
return null;
} else
{
Cost table[][] = getMinimalCost(temp);
return findRowMinimumCost(row, table);
}
}
public Cost findColumnMiniCost(String element, int column)
{
ArrayList temp = (ArrayList)costs.get(element);
if(temp == null)
{
return null;
} else
{
Cost table[][] = getMinimalCost(temp);
return findColumnMiniCost(column, table);
}
}
private HashMap grammar;
private static boolean DEBUG = false;
static Writer out;
static boolean printCost = false;
private static HashMap costs;
private String preStr;
private String preGram;
private final String KEYWORDS = "Keywords";
private final String ENUMERATION = "e";
private final String FUZZYRULE = "f";
private static final double MAXCOST = 1.7976931348623157E+308D;
private static final String DELIMITER = "#DELI;";
private static final String INSC = "#INSC;";
static FuzzyGrammar fuzzGram = new FuzzyGrammar();
static automaticLearner al = new automaticLearner();
static grammarBuilder gb = new grammarBuilder();
static ArrayList TheFinalTables = new ArrayList();
}