/*************************************************************************
* 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.*;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.awt.Container;
import java.awt.FileDialog;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//import java.awt.event.ActionEvent
//import java.io.File;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class guiTokenizer extends javax.swing.JFrame {
Connection connection = null;
String str;
int num, n;
public guiTokenizer()
{ initComponents();
}
public void cmd(){
// jTextField1.setText("Edited file. xls");
PrintWriter outStream=null;
boolean check=true;
//Tokenizer q = new Tokenizer();
String fileName=jTextField1.getText();
try{
//database
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection( "jdbc:odbc:database" );
//file
FileInputStream file = new FileInputStream(fileName);//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 texta=jTextArea1.getText();
//System.out.println(texta);
/* String cmd=null;
cmd(cmd);
System.out.println(cmd);
if(cmd=="Yes"){
System.out.println(cmd);}
else
{ System.out.println("...........");}*/
String s = br.readLine();
char delimiter = '.';//delimeter
// calculate number of delimiter characters
int N = 0;
while (s != null){
for (int i = 0; i < tokens =" new" right =" 0," left =" 0;" i =" 0;" left =" right;" i =" 0;" st =" new" notoken="st.countTokens();" pos =" new" arraytoken="new" addtoken=" new" count="0;" str="st.nextToken();" temp=" toCompleteStack.pop();" count="0;" stmt =" con.createStatement();" countarray="0;" position="tempStack.pop();" rs =" stmt.executeQuery" word =" rs.getString(1);" tokenpos=" rs.getString(2);" check="true;" count="="0)" attpos="null" arraytoken="new" temp="partOfSpeech.pop();" token="0;" try="" counttrue="0;" getcurrentnp="0;" getcurrentvp="0;" backnp="0;" countwhile="0;" counttruetoken="0;" checkstruc="true;" int="" indexnp="0;" checktruenp="new" checktruevp="new" notoken="" wordinpos="partOfSpeech2.pop();" all="" current="" boolean="" check="" begin="" string="" np="" checknp="false;" counttoken="0;"><=(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(); String pv3= Pv2.pop(); //outStream.println("another structure (VP) "+indexNp); Pv.push("penyambung"); Pv.push("kata kerja"); 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); // //if (indexNp<(noToken-2)){ //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<=(noToken-2)){ 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<=(noToken-2)){ 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<=(noToken-2)){ 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&&(countToken1){outStream.println("Sentence false ");}
else {outStream.println("Sentence True");}
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();
}
//Open File Analysis
protected void openOutput()
{
String directoryFileOutDetails="C:/fyp/";
String fileNameFileOutDetails="edited.txt";
setFile(directoryFileOutDetails,fileNameFileOutDetails);
}
//browse
protected void onBrowseNative() {
FileDialog fileDialog = new FileDialog(this, "Open/Save");
fileDialog.show();
if (fileDialog.getFile() != null) {
String directory = fileDialog.getDirectory();
if (!directory.endsWith(File.separator)) {
directory += File.separator;
}
String file = fileDialog.getFile();
jTextField1.setText(directory + file);
setFile(directory,file);
//engine(directory,file);
}
}
public void setFile(String directory, String filename) {
if ((filename == null) || (filename.length() == 0)) return;
File f;
FileReader in = null;
// Read and display the file contents. Since we're reading text, we
// use a FileReader instead of a FileInputStream.
try {
f = new File(directory, filename); // Create a file object
in = new FileReader(f); // And a char stream to read it
char[] buffer = new char[4096]; // Read 4K characters at a time
int len; // How many chars read each time
jTextArea1.setText(""); // Clear the text area
while((len = in.read(buffer)) != -1) { // Read a batch of chars
String s = new String(buffer, 0, len); // Convert to a string
jTextArea1.append(s); // And display them
}
this.setTitle("FileViewer: " + filename); // Set the window title
jTextArea1.setCaretPosition(0); // Go to start of file
}
// Display messages if something goes wrong
catch (IOException e) {
jTextArea1.setText(e.getClass().getName() + ": " + e.getMessage());
this.setTitle("FileViewer: " + filename + ": I/O Exception");
}
// Always be sure to close the input stream!
finally { try { if (in!=null) in.close(); } catch (IOException e) {} }
}
// jbuuton analyze
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//
public void initComponents() {
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Malay Grammar Checker Application");
jTextField1.setText("browse_file");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
// file = jTextField1.getText();
}
});
jButton1.setText("Browse");
jButton2.setText("Analyze");
jButton3.setText("Open File Analysis");
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
onBrowseNative();
}
});
//button analyze
jButton2 .addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent arg0){
cmd();
}
});
jButton3 .addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent arg0){
openOutput();
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(402, Short.MAX_VALUE)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3)
.addGap(28, 28, 28))
.addGroup(layout.createSequentialGroup()
.addGap(60, 60, 60)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 494, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(15, 15, 15)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 338, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)
.addComponent(jButton3))
.addGap(19, 19, 19))
);
pack();
}//
/**
* @param args the command line arguments
*/
public static void main(String args[])throws ClassNotFoundException{
guiTokenizer a=new guiTokenizer();
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Connection con = DriverManager.getConnection( "jdbc:odbc:database" );
Frame f = new FileViewer((args.length == 1)?args[0]:null);
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new guiTokenizer().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
String file;
}