Get Some Free Products at your Door Step. Just feed ur Address in dis Link...!!

Friday, June 7, 2013

GrammerFinder.java

package advance;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import javax.swing.JOptionPane;
public class GrammerFinder
{
    public static void main(String ar[]) throws ClassNotFoundException, SQLException
    {
        ArrayList al = new ArrayList();

        String s="hello";
        //s.









        //String sent=StringExtractor.stringExtractor("anlyse sentence \"what are you doing \"", "\"", '\"');
        //System.out.println(sent);
    }
    /**
<pre>
Input :
String sentence=>
<b>I am going        to   school</b>
Output:<b>
p,hv,iv|n|adv|adj,prep,pfv|n|adv|adj
</b></pre>
     */
    public static String getPosCombinationSyntax(String sentence) throws ClassNotFoundException, SQLException
    {
        String expression="";
        String cword="";
        int count;
        Object ob[]=StringExtractor.seperater(sentence);

        for(int i=0;i<ob.length;i++)
        {
            count=0;
            cword=((String)ob[i]).toLowerCase();
            //System.out.println(cword);
            if(cword.equals("did")||cword.equals("didn't")||cword.equals("do")||cword.equals("doesn't")||
               cword.equals("could")||cword.equals("couldn't")||cword.equals("can")||cword.equals("can't")||
               cword.equals("shall")||cword.equals("shan't")||cword.equals("will")||cword.equals("won't")||
               cword.equals("has")||cword.equals("hasn't")||cword.equals("have")||cword.equals("had")||
               cword.equals("hadn't")||cword.equals("haven't")||cword.equals("what")||cword.equals("where")||
               cword.equals("why")||cword.equals("who")||cword.equals("how")||cword.equals("when")||cword.equals("should")||
               cword.equals("shouldn't")||cword.equals("does")||cword.equals("i")||cword.equals("not")||cword.equals("been")||
               cword.equals("be")||cword.equals("was")||cword.equals("wasn't")||cword.equals("were")||cword.equals("weren't"))
            {
                expression += cword+",";
                count++;
                //continue;
            }
            if(MeaningFinder.pronounChecker(cword))
            {
                        if(count>0){expression=expression.substring(0, expression.length()-1);expression+="|";}
                        if(MeaningFinder.singularPronounChecker(cword)) expression+="PS,";
                        else expression+="PP,";
                count++;
            }
                       
            if(MeaningFinder.verbChecker(cword))
            {          
                        if(count>0){expression=expression.substring(0, expression.length()-1);expression+="|";}
                if(MeaningFinder.verbFirstFormChecker(cword))
                {
                    if(MeaningFinder.verbSingularChecker(cword)){expression+="VFFS,";}
                    else expression+="VFFP,";
                }
                else if(MeaningFinder.verbSecondFormChecker(cword))
                {
                    if(MeaningFinder.verbSecondFormChecker(cword)&& MeaningFinder.verbThirdFormChecker(cword)) expression+="VSF|VTF,";
                    else expression += "VSF,";
                }
                else if(MeaningFinder.verbThirdFormChecker(cword))expression+="VTF,";
                else if(MeaningFinder.verbIngChecker(cword))expression+="VING,";

                count++;
            }
            if(MeaningFinder.articleChecker(cword))
            {
                        if(count>0){expression=expression.substring(0, expression.length()-1);expression+="|";}
                expression+="ART,";
                count++;
            }
            if(MeaningFinder.prepositionChecker(cword))
            {
                        if(count>0){expression=expression.substring(0, expression.length()-1);expression+="|";}
                expression+="PREP,";
                count++;
            }
            if(MeaningFinder.nounChecker(cword))
            {
                        if(count>0){expression=expression.substring(0, expression.length()-1);expression+="|";}
                expression+="N,";
                count++;
            }
            if(MeaningFinder.adjectiveChecker(cword))
            {
                        if(count>0){expression=expression.substring(0, expression.length()-1);expression+="|";}
                expression+="ADV,";
                count++;
            }
            if(MeaningFinder.adjectiveChecker(cword))
            {
                        if(count>0){expression=expression.substring(0, expression.length()-1);expression+="|";}
                expression+="ADJ,";
                count++;
            }
            if(MeaningFinder.helpingVerbChecker(cword))
            {
                if(count>0){expression=expression.substring(0, expression.length()-1);expression+="|";}
                expression+=cword+",";
                count++;
            }
            if(count==0) expression+="*,";
        }
        expression=expression.substring(0, expression.length()-1);
        if(expression.contains("isn\'t")) expression=expression.replace("isn\'t", "isnt");
        if(expression.contains("amn\'t")) expression=expression.replace("am\'t", "amnt");
        if(expression.contains("hasn\'t")) expression=expression.replace("hasn\'t", "hasnt");
        if(expression.contains("haven\'t")) expression=expression.replace("haven\'t", "havent");
        if(expression.contains("hadn\'t")) expression=expression.replace("hadn\'t", "hadnt");
        if(expression.contains("don\'t")) expression=expression.replace("don\'t", "dont");
        if(expression.contains("doesn't")) expression=expression.replace("doesn't", "doesnt");
        if(expression.contains("didn\'t")) expression=expression.replace("didn\'t", "didnt");
        if(expression.contains("shouldn\'t")) expression=expression.replace("shouldn\'t", "shouldnt");
        if(expression.contains("couldn\'t")) expression=expression.replace("couldn\'t", "couldnt");
        if(expression.contains("can\'t")) expression=expression.replace("can\'t", "cant");
        if(expression.contains("wasn\'t")) expression=expression.replace("wasn\'t", "wasnt");
        if(expression.contains("weren\'t")) expression=expression.replace("weren\'t", "nwerent");
        if(expression.contains("won\'t")) expression=expression.replace("won\'t", "wont");
        if(expression.contains("shan\'t")) expression=expression.replace("shan\'t", "shant");
        return expression;
    }
//--------------------------------grammerCombination---------------------------
    /**
    <pre>
    Note: Needs wordlist database to be active
    INPUT:
    String sentence="<b>What are you doing</b>"
    #Here Grammer for this sentence => {p,hv,p,iv|n}
    OUTPUT:<b>
    p,hv,p,iv
    p,hv,p,n
    </b>#Which is cartesian Product of above grammer
    </pre>
     */
    public static Object[] getPosCombination(String sentence) throws ClassNotFoundException, SQLException
    {
        return StringExtractor.uniqueFilter(StringExtractor.cartesianProduct(StringExtractor.seperaterPattern(GrammerFinder.getPosCombinationSyntax(sentence), ','),'|'));
    }
//-------------------------------------combinationFromCFG---------------------------
/**
<pre>
INPUT:<b>
S  -> B,V|HV,B,not,V
V  -> fsv|psv
B  -> p|n
HV -> do|does
</b>
OUTPUT:<b>
p,fsv
n,fsv
p,psv
n,psv
do,p,not,fsv
does,p,not,fsv
do,n,not,fsv
does,n,not,fsv
do,p,not,psv
does,p,not,psv
do,n,not,psv
does,n,not,psv
</b></pre>
 */

    public static Object[] combinationFromCFG(String source)
    {
        Object production[]=StringExtractor.seperaterPattern(source.replace("->",":"), '\n');
            HashMap repTable=new HashMap();
            Object parts[]=null,elements[]=null;
            String language="";
            for(int i=0;i<production.length;i++)
            {
                parts=StringExtractor.seperaterPattern((String)production[i], ':');
                repTable.put(((String)parts[0]).trim(),((String)parts[1]).trim());
            }
            Object languages[]=StringExtractor.seperaterPattern((String) repTable.get("S"), '|');
            for(int j=0;j<languages.length;j++)
            {
                language=(String)languages[j];
                elements=StringExtractor.seperaterPattern(language,',');
                for(int k=0;k<elements.length;k++)
                {
                    if(repTable.containsKey((String)elements[k]))
                    {
                        elements[k]=(String)repTable.get((String)elements[k]);
                    }
                }
                language="";
                for(int k=0;k<elements.length;k++)
                {
                   language+=(String)elements[k]+",";
                }
                languages[j]=language.substring(0, language.length()-1);
            }
            Object[] ob=null;
            ArrayList al=new ArrayList();
            for(int j=0;j<languages.length;j++)
            {
                ob=StringExtractor.cartesianProduct((StringExtractor.seperaterPattern((String) languages[j], ',')),'|');
                for(int i=0;i<ob.length;i++)
                    al.add((String)ob[i]);
            }
            return al.toArray();
    }
//---------------------------------------------------------------------------------------------------
    /**
     *
     * @param filename
     * @return
     * @throws FileNotFoundException
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static String feedCFGCombination(String filename,String type) throws FileNotFoundException, IOException, ClassNotFoundException
    {
            Object ob[]=GrammerFinder.combinationFromCFG(Creater.ReadFile(filename));
            for(int i=0;i<ob.length;i++)
            {
                try
                {
                String seq=(String)ob[i];
                Connection con=Misc.connect(null);
                Statement st=con.createStatement();
                String sql="insert into grammer values('"+seq+"','"+type+"')";
                st.executeUpdate(sql);              
                }catch(SQLException s){}
            }
            return "done";
    }
//---------------------------------------------------------------------------------------------------------
    /**
     *
     * @param filename
     * @param type
     * @return
     * @throws FileNotFoundException
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static String refreshAndFeedCFGCombination(String filename,String type) throws FileNotFoundException, IOException, ClassNotFoundException, SQLException
    {
            Object ob[]=GrammerFinder.combinationFromCFG(Creater.ReadFile("PresentPerfectContinuous.txt"));
            Connection con=Misc.connect(null);
            Statement st=con.createStatement();
            String sql="delete from grammer where type='"+type+"'";
            st.executeUpdate(type);
            for(int i=0;i<ob.length;i++)
            {
                try
                {
                String seq=(String)ob[i];
                st.executeUpdate(sql);
                sql="insert into grammer value('"+seq+"','present perfect continuous')";
                st.executeUpdate(sql);
                }catch(SQLException s){}
            }
            return "done";
    }
/**
<pre>
Tags Correct Parts of Speech for each word
INPUT: String
<b>what will you do</b>
OUTPUT: Object[]  (array of set of correct parts of speech set possible
<b>what,will,PP,VFFP</b>
</pre>
 */
    public static Object[] getCorrectPos(String sentence) throws ClassNotFoundException, SQLException
    {
            Connection con=Misc.connect(null);
            Statement st2=con.createStatement();
            Statement st=con.createStatement();
            ArrayList seqList=new ArrayList();
            Object ob[]=GrammerFinder.getPosCombination(sentence);
            String sql="";
            for(int i=0;i<ob.length;i++)
            {
                sql="select sequence from grammer where sequence ='"+((String)ob[i])+"'";
                //System.out.println(ob[i]) ;
                ResultSet rs=st.executeQuery(sql);
                String seq="",type="";
                while(rs.next())
                {
                    seq=rs.getString("sequence");
                    //type=rs.getString("type");
                    seqList.add(seq);                  
                }
            }
            return seqList.toArray();
    }
/**
 <pre>
 INPUT: String (Sentence)
 <b> what will you do </b>
 OUTPUT: Object[] (set of tense related to tense)
 <b> future indefinite </b>
 ---It actually gets answer for you if
    matches list of syntax in your database---
 </pre>
 */
    public static Object[] getTense(String sentence) throws ClassNotFoundException, SQLException
    {
            Connection con=Misc.connect(null);  
            Statement st=con.createStatement();
            ArrayList seqList=new ArrayList();
            Object ob[]=GrammerFinder.getPosCombination(sentence);          
            String sql="";
            for(int i=0;i<ob.length;i++)
            {
                sql="select * from grammer where sequence ='"+((String)ob[i])+"'";
                //System.out.println(ob[i]) ;
                ResultSet rs=st.executeQuery(sql);
                String seq="",type="";
                while(rs.next())
                {
                    seq=rs.getString("sequence");
                    type=rs.getString("type");
                    seqList.add(type);
                    //System.out.println(seq+" => "+type);
                }
            }
            return StringExtractor.uniqueFilter(seqList.toArray());
    }
    /**
<pre>
INPUT:String (Sentence)
<b> what will you do </b>
OUTPUT:Object[] (sets of sequential combination from length 2 to total no. of words)
<b>what,will,you,do
will,you,do
what,will,you
you,do
will,you
what,will </b>
</pre>
     */
    public static Object[] getSentenceSubPhrases(String s)
    {
        String st="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        HashMap h=new HashMap();
        Object words[]=StringExtractor.seperater(s);
        for(int i=0;i<words.length;i++)
            h.put(st.charAt(i),(String)words[i]);
        Object ob[]=GrammerFinder.getSymbolSubPhrases(words.length);
        ArrayList phrases=new ArrayList();
        for(int i=0;i<ob.length;i++)
        {
            String symbolPhrase=(String)ob[i];
            String realPhrase="";

            for(int j=0;j<symbolPhrase.length();j++)
            {
                realPhrase+=(String)h.get(symbolPhrase.charAt(j))+",";
            }
            phrases.add(realPhrase.substring(0, realPhrase.length()-1));
        }
        Collections.reverse(phrases);
        return phrases.toArray();
    }
/**
<pre>
INPUT: int l (no. of characters for sequencial combination)
<b>4</b>
OUTPUT: Object[] (sets of sequences of symbol from len 2 to l)
<b>ab
bc
cd
abc
bcd
abcd</b>
</pre>
 */
    public static Object[] getSymbolSubPhrases(int l)
    {
        String s="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String st=s.substring(0, l),temp="";
        int len=st.length();
        ArrayList phrases=new ArrayList();
        for(int k=2;(k<=len)&& (k>=2);k++)
            for(int i=0,j=0;i<=len-k;i++)
            {
                j=i+k;
                temp=st.substring(i, j);
                phrases.add(temp);
            }
        return phrases.toArray();
    }
/**
 <pre>
 INPUT: String (Sentence)
 <b> what will you do with your twisted bone </b>
 OUTPUT: Object[] (set of tense related to sentence)
 <b> future indefinite </b>
 ---This function, we call when we don't get help directly from
 sets of sequences in database. So, We break this problem into sets
 of smaller sets sequential combination and again check the tense and
 return possible tenses.---
 </pre>
*/
    public static Object[] getPossibleTense(String sentence) throws ClassNotFoundException, SQLException
    {
        sentence=sentence.toLowerCase();
        Object subPhrases[]=GrammerFinder.getSentenceSubPhrases(sentence);
        ArrayList tense=new ArrayList();
        String current="";
        for(int i=0;i<subPhrases.length;i++)
        {
            Object tempTense[]=GrammerFinder.getTense((String)subPhrases[i]);
            for(int j=0;j<tempTense.length;j++)
                tense.add((String)tempTense[j]);
            if(tempTense.length>0)break;
        }
        return StringExtractor.uniqueFilter(tense.toArray());
    }
}

No comments:

Post a Comment