Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Exception in thread "main" java.lang.NullPointerException

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Exception in thread "main" java.lang.NullPointerException

    Hi, I'm running a program and received this kind of errors. I'm also quite new to Java.
    Would appreciate if anybody could help me. I'm totally lost here. Thanks.

    This my source code:
    // Decompiled by DJ v3.9.9.91 Copyright 2005 Atanas Neshkov  Date: 08/06/2011 11:19:20
    // Home Page : [url]http://members.fortunecity.com/neshkov/dj.html[/url]  - Check often for new version!
    // Decompiler options: packimports(3) 
    // Source File Name:   BuildTheGrammars.java
     
    package TextFragmentLearning;
     
    import IEFG.*;
    import java.io.*;
    import java.util.ArrayList;
     
    public class BuildTheGrammars
    {
     
        public BuildTheGrammars()
        {
        }
     
        public static void main(String args[])
        {
            workingFolder = args[0];
            dataSet = args[1];
            domain = args[1];
            folders = args[1];
            System.out.println((new StringBuilder("\243\243\243\243")).append(args[2]).toString());
            if(args[2].contains(","))
            {
                String terminalCategory[] = args[2].split(",");
                String compoundCategory[] = args[3].split(",");
                for(int a = 0; a < terminalCategory.length; a++)
                {
                    terminalGrammarName.add(terminalCategory[a]);
                    compoundGrammarName.add(compoundCategory[a]);
                    System.out.println((new StringBuilder("%%%%%")).append((String)compoundGrammarName.get(a)).toString());
                }
     
            } else
            {
                terminalGrammarName.add(args[2]);
                compoundGrammarName.add(args[3]);
            }
            importantTermCalled = args[4];
            TextFragmentLearning();
        }
     
        public static void initFilesForTextFragmentLearning(String f)
        {
            automaticLearner.gramFileHeaderTXT = (new StringBuilder(String.valueOf(workingFolder))).append("\\header.txt").toString();
            workingFolder = (new StringBuilder(String.valueOf(workingFolder.substring(0, workingFolder.lastIndexOf("\\"))))).append(dataSet).append("\\").toString();
            gramFileTXT = (new StringBuilder(String.valueOf(workingFolder))).append("basicGrammar.txt").toString();
            dataAndGrammar = (new StringBuilder(String.valueOf(workingFolder))).append("\\addressWithGrammar.txt").toString();
            strToGramMemResult = (new StringBuilder(String.valueOf(workingFolder))).append("\\strToGramMemResult.txt").toString();
            overlapResult = (new StringBuilder(String.valueOf(workingFolder))).append("\\overlapResult.txt").toString();
            gramFileHeaderTXT = (new StringBuilder(String.valueOf(workingFolder))).append("\\header.txt").toString();
            gramFileHeaderXML = (new StringBuilder(String.valueOf(workingFolder))).append("\\header.xml").toString();
            evoLearning.copyHeaderFile(gramFileHeaderTXT);
            automaticLearner.workingFolder = workingFolder;
            automaticLearner.gramFileTXT = gramFileTXT;
            automaticLearner.dataAndGrammar = dataAndGrammar;
            automaticLearner.strToGramMemResult = strToGramMemResult;
            automaticLearner.gramFileHeaderTXT = gramFileHeaderTXT;
            automaticLearner.gramFileHeaderXML = gramFileHeaderXML;
        }
     
        public static void TextFragmentLearning()
        {
            initFilesForTextFragmentLearning(folders);
            listOfDerivedGrammar.clear();
            editedGrammars.clear();
            deriveManyGrammars(folders);
            filterTheGrammars(folders);
            loadEditedGrammars(folders);
            prepareTheFileForDerivedGrammar(folders);
        }
     
        public static void deriveManyGrammars(String folders)
        {
            File f1 = new File(dataAndGrammar);
            f1.delete();
            inputList.clear();
            dataGram.clear();
            datafile = (new StringBuilder(String.valueOf(workingFolder))).append(folders).append(".txt").toString();
            loadEntries(datafile);
            curfolder = folders;
            System.out.println("---------------------------------------------");
            System.out.println((new StringBuilder("datafile:")).append(datafile).toString());
            for(int j = 0; j < inputList.size(); j++)
            {
                parseAble = false;
                String theGrammarForThisData = grammarBuilder.execGrammarBuilder((String)inputList.get(j), charsToRemove, printCommentsInGrammarBuilder);
                if(!listOfDerivedGrammar.contains(theGrammarForThisData))
                {
                    learnedInput.add((String)inputList.get(j));
                    listOfDerivedGrammar.add(theGrammarForThisData);
                }
                System.out.println((new StringBuilder(String.valueOf(j))).append("****************** NEW DATA: ").append((String)inputList.get(j)).append(":").append(theGrammarForThisData).append("**********************").toString());
            }
     
        }
     
        public static void loadEntries(String filename)
        {
            try
            {
                BufferedReader r = new BufferedReader(new FileReader(filename));
                String str = "";
                int counter = 0;
                while((str = r.readLine()) != null) 
                    inputList.add(str.replace("$", "usd").replace(" a ", " ").replace(" to ", " ").replace(" for ", " ").trim());
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
     
        public static void filterTheGrammars(String foldername)
        {
            try
            {
                BufferedWriter w1 = new BufferedWriter(new FileWriter((new StringBuilder(String.valueOf(workingFolder))).append("\\").append(foldername).append("_fragmentsAndGrammars.txt").toString()));
                for(int j = 0; j < listOfDerivedGrammar.size(); j++)
                {
                    String splitDerivedGrammar[] = ((String)listOfDerivedGrammar.get(j)).split("#DELI;");
                    System.out.println((new StringBuilder("thegrammar originally:")).append((String)listOfDerivedGrammar.get(j)).toString());
                    w1.write((new StringBuilder(String.valueOf((String)learnedInput.get(j)))).append("\n").toString());
                    w1.write((new StringBuilder(String.valueOf((String)listOfDerivedGrammar.get(j)))).append("\n").toString());
                    String thegrammar = "";
                    for(int k = 0; k < splitDerivedGrammar.length; k++)
                        if(thegrammar.equals(""))
                            thegrammar = splitDerivedGrammar[k];
                        else
                        if(thegrammar.equals("aw"))
                            thegrammar = splitDerivedGrammar[k];
                        else
                            thegrammar = (new StringBuilder(String.valueOf(thegrammar))).append("#DELI;").append(splitDerivedGrammar[k]).toString();
     
                    for(; thegrammar.endsWith("#DELI;aw"); thegrammar = thegrammar.substring(0, thegrammar.length() - "#DELI;aw".length()));
                    System.out.println((new StringBuilder("thegrammar edited:")).append(thegrammar).toString());
                    boolean containContentCat = false;
                    for(int a = 0; a < compoundGrammarName.size(); a++)
                        if(thegrammar.contains((CharSequence)terminalGrammarName.get(a)) && thegrammar.contains("#DELI;") && thegrammar.contains(importantTermCalled))
                        {
                            if(!editedGrammars.contains(thegrammar))
                            {
                                System.out.println((new StringBuilder(String.valueOf(thegrammar))).append(" edited and saved").toString());
                                w1.write((new StringBuilder("edited & saved:")).append(thegrammar).append("\n").toString());
                                editedGrammars.add(thegrammar);
                            }
                        } else
                        {
                            w1.write((new StringBuilder("not saved-->")).append(thegrammar).append("\n").toString());
                        }
     
                }
     
                w1.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
     
        public static void loadEditedGrammars(String foldername)
        {
            try
            {
                foldername = (new StringBuilder(String.valueOf(workingFolder))).append("\\").append(foldername).toString();
                BufferedReader r = new BufferedReader(new FileReader((new StringBuilder(String.valueOf(foldername))).append("_fragmentsAndGrammars.txt").toString()));
                for(String str = ""; (str = r.readLine()) != null;)
                    if(str.startsWith("edited"))
                    {
                        str = str.substring("edited:".length());
                        if(!editedGrammars.contains(str) && !str.equals(""))
                            editedGrammars.add(str.trim());
                    }
     
                r.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
     
        public static void prepareTheFileForDerivedGrammar(String foldername)
        {
            try
            {
                evoLearning.copyHeaderFile((new StringBuilder(String.valueOf(workingFolder))).append("\\allGrammar.txt").toString());
                BufferedWriter w = new BufferedWriter(new FileWriter((new StringBuilder(String.valueOf(workingFolder))).append("\\allGrammar.txt").toString(), true));
                w.write("\n");
                for(int j = 0; j < editedGrammars.size(); j++)
                {
                    for(int i = 0; i < compoundGrammarName.size(); i++)
                    {
                        System.out.println((new StringBuilder("i:")).append((String)editedGrammars.get(j)).toString());
                        if(((String)editedGrammars.get(j)).contains((CharSequence)terminalGrammarName.get(i)))
                        {
                            System.out.println((new StringBuilder("saved:")).append((String)editedGrammars.get(j)).toString());
                            w.write((new StringBuilder("\nn\n")).append((String)compoundGrammarName.get(i)).append("\nc\n").append((String)editedGrammars.get(j)).append("\n").append("ext_0\n").toString());
                        }
                    }
     
                }
     
                w.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
     
        static grammarBuilder gramBuilder = new grammarBuilder();
        static evoLearning el = new evoLearning();
        static automaticLearner al = new automaticLearner();
        static ArrayList listOfDerivedGrammar = new ArrayList();
        static ArrayList editedGrammars = new ArrayList();
        static ArrayList terminalGrammarName = new ArrayList();
        static ArrayList compoundGrammarName = new ArrayList();
        static String domain = "suppli";
        public static String dataSet = "suppli";
        static String folders = "suppli";
        static String evolveType = "evolveNew";
        public static String workingFolder;
        public static String gramFileTXT = "";
        public static String dataAndGrammar = "";
        public static String strToGramMemResult = "";
        protected static String overlapResult = "";
        public static String gramFileHeaderTXT = "";
        public static String gramFileHeaderXML = "";
        protected static String firstGenFile = "";
        static final String DELIMITER = "#DELI;";
        static String grammarPrefix = "gram";
        static boolean parseAble = false;
        static boolean printComments = false;
        static boolean printCommentsInGrammarBuilder = false;
        static ArrayList dataGram = new ArrayList();
        static ArrayList inputList = new ArrayList();
        static ArrayList learnedInput = new ArrayList();
        static ArrayList MaxGramElemNames = new ArrayList();
        static ArrayList MaxGramElemDefs = new ArrayList();
        static String datafile;
        static String foldername;
        static String curfolder;
        static String evolvedGrammar;
        public static String charsToRemove = ",.?!()";
        static String importantTermCalled = "";
     
    }

    And these are some of errors I received:

    Error 1:

    keywords
    Error: Invalid grammar element name!
    Exception in thread "main" java.lang.NullPointerException
    at fuzzygrammar.FuzzyMembership.getMinimalCost(FuzzyM embership.java:1187
    )
    at fuzzygrammar.FuzzyMembership.execFuzzMem(FuzzyMemb ership.java:198)
    at IEFG.grammarBuilder.execGrammarBuilder(grammarBuil der.java:50)
    at TextFragmentLearning.BuildTheGrammars.deriveManyGr ammars(BuildTheGram
    mars.java:136)
    at TextFragmentLearning.BuildTheGrammars.TextFragment Learning(BuildTheGr
    ammars.java:113)
    at TextFragmentLearning.BuildTheGrammars.main(BuildTh eGrammars.java:76)

    Error 2:

    Caused by: org.xml.sax.SAXParseException; lineNumber: 15; columnNumber: 3; The e
    lement type "SquadGrammar" must be terminated by the matching end-tag "</SquadGr
    ammar>".
    at com.sun.org.apache.xerces.internal.util.ErrorHandl erWrapper.createSAX
    ParseException(ErrorHandlerWrapper.java:198)
    at com.sun.org.apache.xerces.internal.util.ErrorHandl erWrapper.fatalErro
    r(ErrorHandlerWrapper.java:177)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorRe porter.reportError(
    XMLErrorReporter.java:441)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorRe porter.reportError(
    XMLErrorReporter.java:368)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner .reportFatalError(X
    MLScanner.java:1375)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumen tFragmentScannerImp
    l.scanEndElement(XMLDocumentFragmentScannerImpl.ja va:1708)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumen tFragmentScannerImp
    l$FragmentContentDriver.next(XMLDocumentFragmentSc annerImpl.java:2898)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumen tScannerImpl.next(X
    MLDocumentScannerImpl.java:607)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocum entScannerImpl.next
    (XMLNSDocumentScannerImpl.java:116)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumen tFragmentScannerImp
    l.scanDocument(XMLDocumentFragmentScannerImpl.java :488)
    at com.sun.org.apache.xerces.internal.parsers.XML11Co nfiguration.parse(X
    ML11Configuration.java:835)
    at com.sun.org.apache.xerces.internal.parsers.XML11Co nfiguration.parse(X
    ML11Configuration.java:764)
    at com.sun.org.apache.xerces.internal.parsers.XMLPars er.parse(XMLParser.
    java:123)
    at com.sun.org.apache.xerces.internal.parsers.Abstrac tSAXParser.parse(Ab
    stractSAXParser.java:1210)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserI mpl$JAXPSAXParser.p
    arse(SAXParserImpl.java:568)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller. UnmarshallerImpl.un
    marshal0(UnmarshallerImpl.java:202)
    ... 10 more

    I just dont know where do i go from here. Please help... Thank you


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    You haven't posted any of the relevant code or explained what the program does, so all I can say is that:

    Error 1: 'keywords' is not a valid part of the XML grammar it's being used in.

    At line 1187 in file FuzzyMembership.java, an object variable is being used that has not been initialised (i.e. is null). This code is being called from method fuzzygrammar.FuzzyMembership.getMinimalCost().

    Error 2: An XML element called <SquadGrammar> is missing its end-tag </SquadGrammar>.

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Hi dlorde,
    Thank u so much for your response. As I said I'm new with this thing. What the program does is to tag certain words in a corpus with each grammar defined in a header.txt.

    here is the 2 files related to the error:

    fuzzyMembership.java:
    // 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();
     
    }

    grammarBuilder.java:
    // 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();
     
    }

  4. #4
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Quote Originally Posted by dlorde View Post
    Error 2: An XML element called <SquadGrammar> is missing its end-tag </SquadGrammar>.
    How can i add the end-tag? Through which file? I cant see any XML file related to this program. For your info, this was developed by somebody else.

  5. #5
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    You said you were running the program, so presumably there's some data that it uses? It may be that the XML is in a file that doesn't have a .xml extension. I'm not running it, so I really don't know...

    Looking at the code you posted, the error line numbers don't match up with the source code, so the NullPointerException problem is pretty much guesswork. You'll have to fish around in the relevant methods - put breakpoints in likely looking places, or if you don't have a debugger, try inserting print statements to see what is null and what isn't.

Similar Threads

  1. Exception in thread "main" java.lang.NullPointerException
    By isun in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 28th, 2011, 09:22 AM
  2. Exception in thread "main" java.lang.NullPointerException
    By Tsark in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 11th, 2011, 08:39 AM
  3. Exception in thread "main" java.lang.NullPointerException
    By MryJaho in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 4th, 2011, 05:36 PM
  4. Replies: 16
    Last Post: August 27th, 2010, 03:30 PM
  5. Please help! Exception in thread "main" java.lang.NullPointerException
    By Arutha2321 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 18th, 2009, 02:25 AM