Pages

Saturday, April 3, 2010

my fyp (code)

/*************************************************************************
* Compilation: javac Tokenizer.java In.java
* Execution: java Tokenizer
*
* Takes a string as input and tokenizes it into an array of substrings
* See also java.util.StringTokenizer.
* read and write to the text file
* using a specified delimiter character.
* check the availablelity of the words in database
* check the structure due to the malay language rule
*
*
*************************************************************************/
import java.io.*;
import java.util.StringTokenizer;
import java.util.*;
import java.net.*;
import java.sql.*;


public class Tokenizer {


//database
public static final String DRIVER_NAME ="sun.jdbc.odbc.JdbcOdbcDriver";
public static final String DATABASE_URL = "jdbc:odbc:database";


String str;
int num, n;


public static void main (String[] args) throws ClassNotFoundException, SQLException{
PrintWriter outStream=null;
Class.forName(DRIVER_NAME);
Connection con = null;
boolean check=true;
//Tokenizer q = new Tokenizer();

try{
//database
//kosmo denggi 7 kawasan panas
FileInputStream file = new FileInputStream("C:/fyp/test.txt");//input: text file with delimeter
outStream = new PrintWriter(new FileOutputStream("C:/fyp/edited.txt"));//output: sentence eleminate the delimeter
DataInputStream in = new DataInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(in));


//array
String[] arrayToken;
String[] checktrueNp;
String[] checktrueVp;

//declare stack
Stack currentStack= new Stack();
Stack copyStack = new Stack();
Stack toCompleteStack = new Stack();
Stack toCompleteStackSentence = new Stack();
Stack tempStack =new Stack ();
Stack backUpToCompleteStack = new Stack();
Stack backUpToCompleteStackSentence= new Stack();
Stack currentStackSentence= new Stack();
Stack partOfSpeech= new Stack();
Stack partOfSpeech2= new Stack();
//stack for frasa nama,frasa kerja, frasa adjectif, frasa sendi
Stack Np= new Stack();
Stack Vp= new Stack();
Stack Pp= new Stack();
Stack Ap= new Stack();
Stack Pv= new Stack();
Stack Pv2= new Stack();


//insert element in pharase
//Np
// stack frasa nama= bil/penjbil/gelaran/kata nama/penentu/pent
Np.push("Pent");
Np.push("penentu");
Np.push("Kata nama");
Np.push("Kata nama");
Np.push("gelaran");
Np.push("penjodoh bilangan");
Np.push("bil");
String[] arrayNp=new String[]{"bil","penjodoh bilangan","gelaran","Kata nama","penentu","Pent"};
String[] arrayNpCheck=new String[arrayNp.length];
//Vp
//stack frasa kerja= kata bantu/[katakerja(obj|akomp)]ket
Vp.push("Ket");
Vp.push("Akomp");
Vp.push("kata nama");
Vp.push("Kata kerja");
Vp.push("kata bantu");
String[] arrayVp=new String[]{"kata bantu","Kata kerja","Obj"};
String[] arrayVpCheck=new String[arrayVp.length];
//Pp
// stack frasa sendi=kata bantu +kata sendi+kata nama arah+FN+(akomp|ket)*/
Pp.push("ket");
Pp.push("Akomp");
Pp.push("Fn");
Pp.push("Kata nama arah");
Pp.push("Kata sendi");
Pp.push("kata Bantu");
String[] arrayPp=new String[]{"kata Bantu","Kata sendi","Kata nama arah","Fn","Akomp","ket"};
//Ap
//stack frasa adjectiv= kata bantu(kata penguat)+adj+(ket)+(akomp)
// Ap.push("Akomp");
Ap.push("ket");
Ap.push("adjectif");
Ap.push("kata penguat");
Ap.push("kata bantu");
String[] arrayAp=new String[]{"kata bantu","kata penguat","adjectif","ket","Akomp"};


//pasive
Pv.push("obj");
Pv.push("penyambung");
Pv.push("kata kerja");
String[] arrayPasive=new String[]{"kata kerja","penyambung","kata nama"};
String[] arrayPasivepCheck=new String[arrayPasive.length];

//pasive2
Pv2.push("kata kerja");
Pv2.push("kata nama");

String s = br.readLine();
char delimiter = '.';//delimeter

// calculate number of delimiter characters
int N = 0;
while (s != null){

for (int i = 0; i < s.length(); i++)
if (s.charAt(i) == delimiter) N++;
String[] tokens = new String[N+1];

// parse N+1 tokens and store in an array
int right = 0, left = 0;
for (int i = 0; i < N; i++) {
while(s.charAt(right) != delimiter)
right++;
tokens[i] = s.substring(left, right);
right++;
left = right;
}
tokens[N] = s.substring(right, s.length());
int noToken;
// print results for testing
for (int i = 0; i < tokens.length-1; i++)
{//open for
outStream.println(tokens[i]);// tranform paragraph to sentence
String str;
StringTokenizer st = new StringTokenizer(tokens[i]);
noToken=st.countTokens();
outStream.println("number of token: "+noToken+" in sentense "+ (i+1));
//String[] pos = new String[noToken];
arrayToken=new String[noToken];
// String[] addToken= new String[st.countTokens()];
// int count=0;




while (st.hasMoreTokens())
{//open while
str=st.nextToken();
//insert token in stack
//System.out.print(str + " ");
toCompleteStack.push(str);// transform sentence to word


}//close while
//toCompleteStack.push("stop");

// System.out.print("Retrieved elements from the stack : ");


while (!toCompleteStack.empty()){
//pop the sentence from stack stored temp
String temp= toCompleteStack.pop();
//System.out.println(temp);
tempStack.push(temp);
}

// check in database the availablelity and the part of speeecg (POS)
String word ;
String tokenPos ;
int count=0;
String position;


con = DriverManager.getConnection(DATABASE_URL);
Statement stmt = con.createStatement();
ResultSet rs;
int countArray=0;
while(!tempStack.empty()){
position=tempStack.pop();
rs = stmt.executeQuery ("select * from [Sheet1$]");
while (rs.next())
{
word = rs.getString(1);
tokenPos= rs.getString(2);
//outStream.println(position+" "+position.equalsIgnoreCase(word));
if(position.equalsIgnoreCase(word))
{
outStream.println(position+" Ada "+tokenPos);
//check=true;
partOfSpeech.push(tokenPos);//store POS in Stack
// pos[countArray]=tokenPos;

arrayToken[countArray]=tokenPos;
count++;
break;
}
else
{
//partOfSpeech.push("null");
}

}
rs.close();
//insert none of POS if the word not available in the dBase
if(count==0)
{ String attPos="null";
partOfSpeech.push(attPos);
//pos[countArray]=attPos;
arrayToken[countArray]=attPos;
// arrayToken=new String[noToken];
// arrayToken[]
}
// countToken++;
countArray++;
}
//inthe right order


// pop the PartOf Speech in the word containt in the sentences.

stmt.close();


while(!partOfSpeech.empty())
{String temp=partOfSpeech.pop();
partOfSpeech2.push(temp);}


/* show an array of POS
for(int token=0; token outStream.print("Try "+arrayToken[token]);*/

int countToken=0;
int countTrue=0;
int getCurrentNp=0;
int getCurrentVp=0;
int backNp=0;
int countWhile=0;
int countTrueToken=0;
boolean checkStruc=true;
int indexNp=0;

checktrueNp=new String[noToken];
checktrueVp=new String[noToken];
while(noToken != countToken)

{ String wordInPos=partOfSpeech2.pop();
// outStream.println("Check "+ wordInPos+" all "+noToken+" current "+countToken);
boolean checkNp= false;

outStream.println();
//1:::: NP check
//(NP)+np begin
while(!Np.empty()){
String np=Np.pop();
checkNp=arrayToken[countToken].equalsIgnoreCase(np);
if( arrayToken[countToken].equalsIgnoreCase(np))//&&(countToken {
outStream.println(countToken+": "+arrayToken[countToken]+" "
+arrayToken[countToken].equalsIgnoreCase(np));

String np2=Np.pop();
if (countToken<=(noToken-2)){
if( arrayToken[countToken+1].equalsIgnoreCase(np2))
{
outStream.println((countToken+1)+": "+arrayToken[countToken+1]+" "
+arrayToken[countToken+1].equalsIgnoreCase(np2));
}


else
{


indexNp=countToken+1;
//check VP
String vp1= Vp.pop();
String vp2= Vp.pop();
Vp.push("Kata kerja");
Vp.push("kata bantu");


String pv1= Pv.pop();
String pv2= Pv.pop();
Pv.push("penyambung");
Pv.push("kata kerja");
String pv3= Pv2.pop();
Pv2.push("kata nama");
outStream.println("another structure (VP) "+indexNp);
// outStream.println("String pv3= Pv2.pop();"+pv3);
boolean checkPasivePenyambung=false;



//outStream.println(arrayToken[indexNp].equalsIgnoreCase(vp2)+vp2);
//
//checkPasivePenyambung= arrayToken[indexNp+1].equalsIgnoreCase(pv2);
// outStream.println(checkPasivePenyambung+"tr");
if((arrayToken[indexNp].equalsIgnoreCase("kata bantu"))||(arrayToken[indexNp].equalsIgnoreCase("kata kerja")))//&&checkPasivePenyambung==false)
{
outStream.println("VP true "+arrayToken[indexNp]);
while(noToken != indexNp)
{

while(!Vp.empty())
{
String vp=Vp.pop();
if(arrayToken[indexNp].equalsIgnoreCase(vp)&&(indexNp {
outStream.println(indexNp+": "+arrayToken[indexNp]+" "+arrayToken[indexNp]+" "
+arrayToken[indexNp].equalsIgnoreCase(vp)+" "+vp);

vp2=Vp.pop();

if( arrayToken[indexNp+1].equalsIgnoreCase(vp2))
{
outStream.println((indexNp+1)+": "+arrayToken[indexNp+1]+" "+arrayToken[indexNp+1]+" "
+arrayToken[indexNp+1].equalsIgnoreCase(vp2)+" " +vp2);
}

}


}

indexNp++;

}


}
//vp pasive

else if (arrayToken[indexNp].equalsIgnoreCase(pv1)||arrayToken[indexNp].equalsIgnoreCase(pv3))
{
//pv 1
outStream.println("Pv true "+arrayToken[indexNp+1]);
if (arrayToken[indexNp].equalsIgnoreCase(pv1))
{
outStream.println("Pv1 true");
outStream.println("Pv1 true");
while(noToken != indexNp)
{

while(!Pv.empty())
{
String pv11=Pv.pop();
if(arrayToken[indexNp].equalsIgnoreCase(pv11)&&(indexNp {
outStream.println(indexNp+": "+arrayToken[indexNp]+" "+arrayToken[indexNp]+" "
+arrayToken[indexNp].equalsIgnoreCase(pv11));

String vp12=Pv.pop();

if( arrayToken[indexNp+1].equalsIgnoreCase(vp12))
{
outStream.println((indexNp+1)+": "+arrayToken[indexNp+1]+" "+arrayToken[indexNp+1]+" "
+arrayToken[indexNp+1].equalsIgnoreCase(vp12));
}

}


}

indexNp++;

}

}

else if(arrayToken[indexNp].equalsIgnoreCase(pv3))
{
outStream.println("Pv2 true"+ arrayToken[indexNp]+pv3);
while(noToken != indexNp)
{

while(!Pv2.empty())
{
String pv21=Pv2.pop();
if(arrayToken[indexNp].equalsIgnoreCase(pv21)&&(indexNp {
outStream.println(indexNp+": "+arrayToken[indexNp]+" "+arrayToken[indexNp]+" "
+arrayToken[indexNp].equalsIgnoreCase(pv21)+" "+pv21);

String pv22=Pv2.pop();

if( arrayToken[indexNp+1].equalsIgnoreCase(pv22))
{
outStream.println((indexNp+1)+": "+arrayToken[indexNp+1]+" "+arrayToken[indexNp+1]+" "
+arrayToken[indexNp+1].equalsIgnoreCase(pv22)+" " +pv22);
}

}
else
{outStream.println("False ");}


}

indexNp++;

}
//Pv2.push("kata kerja");
//Pv2.push("kata nama");
}


}
// else
//{outStream.println("Another structure "); }

backNp++;
outStream.println("BAck to Np");



//Vp.push("Ket");
// Vp.push("Akomp");
Vp.push("obj");
Vp.push("Kata kerja");
Vp.push("kata bantu");
/* (arrayToken[indexNp].equalsIgnoreCase(Pp.pop())&&!Pp.empty())
{
outStream.println("stack empty PP:"+!Pp.empty());
}
else if (arrayToken[indexNp].equalsIgnoreCase(Ap.pop())&&!Ap.empty())
{
outStream.println("stack empty AP:"+!Ap.empty());
}
else
{outStream.println("check Np back At :"+ indexNp);}*/

}
}
} //end if

else if (checkNp==false&&(countToken {
//outStream.println(countToken+": "+"false");
}


} //while checkNp
// outStream.println("backNP"+backNp);

countToken++;


Np.push("Pent");
Np.push("penentu");
Np.push("Kata nama");
Np.push("gelaran");
Np.push("penjodoh bilangan");
Np.push("bil");
}//end while NP
outStream.println("BAck to Np"+backNp);
if(backNp==0){outStream.println("Sentence false no NP");}
else if (backNp>1){outStream.println("Sentence false ");}
else {outStream.println("Sentence True");}

//(NP)+np begin
/*
/vp

while(indexNp {
while(!Vp.empty())
{
String vp=Vp.pop();

if (arrayToken[indexNp].equalsIgnoreCase(vp)&& (indexNp {
outStream.println((indexNp)+": "+arrayToken[ indexNp]+" "
+arrayToken[ indexNp].equalsIgnoreCase(vp));


String vp2=Vp.pop();
if( arrayToken[indexNp+1].equalsIgnoreCase(vp2))
{
outStream.println((indexNp+1)+": "+arrayToken[ indexNp+1]+" "
+arrayToken[ indexNp+1].equalsIgnoreCase(vp2));
}

}
else
{
outStream.println(arrayToken[indexNp]+"....."+"index: "+indexNp + "Pop vp"+vp);
}

}
indexNp++;

}










if(arrayToken[countToken+(inNp)].equalsIgnoreCase(arrayNp[checkNp+(inNp)])&&(checkNp<(checkNp+inNp)))
{
outStream.println((countToken+inNp)+": "+arrayToken[countToken +(inNp)]
+" "+arrayToken[countToken+inNp].equalsIgnoreCase(arrayNp[checkNp+inNp])
+" aray Np ke:"+(checkNp+inNp));
outStream.println();

}
// else if (arrayToken[countToken+(inNp)].equalsIgnoreCase(arrayNp[checkNp+(inNp)])&&(checkNp>(checkNp+inNp)))
// {outStream.println("sentence false");}
else
//Np+(Np) last
{ outStream.println("check another struc"+arrayToken[countToken+(inNp)]);


for(int checkNpIn=0;checkNpIn {

if(arrayToken[countToken+inNp].equalsIgnoreCase(arrayNp[checkNpIn]))
{
outStream.println((countToken+inNp) +": "+arrayToken[countToken+(inNp)]
+" "+arrayToken[countToken+(inNp)].equalsIgnoreCase(arrayNp[checkNpIn])
+" "+checkNpIn);
outStream.println("Np is true");


}
//else
//{outStream.println((countToken+inNp)+" Np is false "+checkNpIn);}
//outStream.println(" ");
}


}*/


























/* //check the index for the next check
// int getCurrentVp=0;
int countTrueNp=1;
for(int checkNp=0;checkNp {
if((checktrueNp[checkNp].equalsIgnoreCase("True")&&checktrueNp[checkNp+1].equalsIgnoreCase("False")))
indexNp=checkNp+1;
//else if(checktrueNp[checkNp].equalsIgnoreCase("True")&&checktrueNp[checkNp+1].equalsIgnoreCase("True"))
//{}
else if(checktrueNp[checkNp].equalsIgnoreCase("True"))
countTrueNp++;
}


if(countTrueNp==noToken)
{outStream.println(" Np+Np sentences is true");}

else if(checktrueNp[0].equalsIgnoreCase("False")){outStream.println(" sentence no NP: 'False' ");}

//2:::::Vp pasive sentence
else if(countTrueNp>0 && countTrueNp
outStream.println(" Index NP " +indexNp);
outStream.println("");
while(indexNp {

outStream.println("Check "+ arrayToken[indexNp]+" all "+noToken+" current "+indexNp);

for(int checkVp=0;checkVp {
if( arrayToken[indexNp].equalsIgnoreCase(arrayPasive[checkVp]))
{
outStream.println(arrayToken[indexNp].equalsIgnoreCase(arrayPasive[checkVp]));
countTrueToken++;

checktrueVp[indexNp-1]="True";
arrayVpCheck[checkVp]="True";
outStream.println(indexNp);
getCurrentVp++;
break;

}

else
{ outStream.println("FalseVp");
arrayVpCheck[checkVp]="False";
checktrueVp[indexNp-1]="False";}
}

indexNp++;

}//end while NP

//check the index for the next check

int countTrueVp=2;
for(int checkVp=0;checkVp {
if((checktrueVp[checkVp].equalsIgnoreCase("True")&&checktrueVp[checkVp+1].equalsIgnoreCase("False")))
indexVp=checkVp+1;
else if(checktrueVp[checkVp].equalsIgnoreCase("True"))
countTrueVp++;
}

if(countTrueVp==noToken)
{outStream.println(" Np+Vp |negetive|sentences is true");}


//3: sentences NP + Vp normal
else if(countTrueVp >0 && countTrueVp {outStream.println(" Index VP " +(indexVp+1));}


} */


// for(int checkNp=0;checkNp // outStream.println("Try "+ arrayNpCheck[checkNp]);



// get the index for the next check





outStream.println("___________________________________________________________");
outStream.println("___________________________________________________________");
outStream.println("");

// System.out.println(arrayToken[countToken]);
}//close for




}//close main while


}// close try


catch(FileNotFoundException fnf)
{System.out.println("File Not Found");
System.out.println(fnf.getMessage());}

catch(IOException io)
{ System.out.println(io.getMessage()); }

catch(Exception a)
{ }
outStream.close();

if (con != null)
con.close();

}
}

No comments: