Dictionary
The dictionary program is a Java applet that accepts input of a word. It will then look it up in a collection of words. If the word is found, it will display a message to that effect. If not found, the programme will try to find similar matches by transposition, substitution, insertion or deletion of one single letter.
Java features used are String comparison, opening and reading of text files, array of strings, conversion to lowercase, GUI including JCheckBox, JTextArea, JTextField, JLabel.
// dictionary, P.C. 2004-10-31
// dictionary lookup with alternative spellings
//
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
class test extends JFrame implements ActionListener{
private String D[]; // dictionary (string)
private int nD=0; // size of dictionary
private JLabel lDict,lInput,lOptions;
private JTextField inFile;
private JTextArea outArea;
private JCheckBox cTrans, cSubst, cInsert, cDelete;
private String out="";
public test(){
super("Dictionary Search");
// GUI stuff
lInput=new JLabel("Enter word to be searched");
inFile=new JTextField(" ");
inFile.addActionListener(this);
outArea=new JTextArea(20,35);
outArea.setFont(new Font("Courier",Font.BOLD,11));
JScrollPane scr=new JScrollPane(outArea);
lOptions=new JLabel("Options for alternative spellings");
lOptions.setFont(new Font("Courier",Font.BOLD,11));
cTrans=new JCheckBox("Transposition ");
cTrans.setFont(new Font("Courier",Font.PLAIN,11));
cSubst=new JCheckBox("Substitution ");
cSubst.setFont(new Font("Courier",Font.PLAIN,11));
cInsert=new JCheckBox("Insertion ");
cInsert.setFont(new Font("Courier",Font.PLAIN,11));
cDelete=new JCheckBox("Deletion ");
cDelete.setFont(new Font("Courier",Font.PLAIN,11));
Container c=getContentPane();
c.setLayout(new FlowLayout());
LoadDictionary();
lDict=new JLabel("This dictionary contains "+nD+" entries\n");
lDict.setFont(new Font("Arial",Font.BOLD,9));
c.add(lDict);c.add(lOptions);
c.add(cTrans);c.add(cInsert);c.add(cDelete);c.add(cSubst);
c.add(lInput); c.add(inFile);
c.add(scr);
setSize(300,500);
setVisible(true);
} // end constructor test
private int binarySearch(String w){
// return -1 if unsuccessful, return location if found
int n0=0, n1=0, n2=nD;
do{
n1=(n0+n2)/2;
// outArea.append(D[n1]+"\n");
int diff=w.compareToIgnoreCase(D[n1]);
if(diff==0){ // found string
return n1;
} else if(diff>0){ // greater
if(n0==n1)n0=n2; else n0=n1;
} else { // smaller
if(n2==n1) return -1; // not found
n2=n1;
}
}while(1==1);
}
private boolean PrintWord(String s, boolean First){
int b=binarySearch(s);
if(b>=0){ // found alternative spelling
if(First)outArea.append("possible matches:\n");
outArea.append(s+"\n"); First=false;
}
return First;
}
private void nearSearch(String w){
String wc="\\"+"w"; // wild card char. [a-zA-Z_0-9]
int wlen=w.length();
// Find near matches
boolean First=true; // to print heading
boolean bTrans, bInsert, bSubst, bDelete;
if(cTrans.isSelected())bTrans=true;else bTrans=false;
if(cInsert.isSelected())bInsert=true;else bInsert=false;
if(cSubst.isSelected())bSubst=true;else bSubst=false;
if(cDelete.isSelected())bDelete=true;else bDelete=false;
// transposition and deletion
if(bInsert||bSubst||bTrans||bDelete){
for(int i=0;i<=wlen;i++){
if(bInsert){ // insertion
char wa[]=w.toCharArray();
String s0="",s="",s1="";char wb[]=new char[1];
if(i==0){s0="";s1=w;} else if(i==wlen){s0=w;s1="";}
else{ // intermediate cases
s0=w.substring(0,i);s1=w.substring(i,wlen);
}
for(int j=0;j<26;j++){
wb[0]=(char)('a'+j);
s=new String(wb); s=s0+s+s1;
First=PrintWord(s,First);
}
First=PrintWord(s,First);
}
if(i>wlen-1)continue;
if(bSubst){ // substitution
char wa[]=w.toCharArray();
for(int j=0;j<26;j++){
wa[i]=(char)('a'+j); // substitute by all 26 letters
String s=new String(wa); First=PrintWord(s,First);
}
}
if(i>=wlen-1)continue; // Transposition & Delete apply till wlen()-2
if(bTrans){// transposition
char wa[]=w.toCharArray();
char c=wa[i]; wa[i]=wa[i+1];wa[i+1]=c;
String s=new String(wa); First=PrintWord(s,First);
}
if(bDelete){ // Delete & trim last character
char wa[]=w.toCharArray();
for(int j=i;j=0)outArea.append(word+" found\n");
else {
outArea.append(word+" NOT found\n");
nearSearch(word);
}
}
private void LoadDictionary(){
String dictName="words"; // file name of dictionary
File name=new File(dictName);
if(nD==0){ // Dictionary not yet loaded
if(name.exists() && name.isFile()){
try{ // Load Dictionary
StringBuffer buffer=new StringBuffer();
BufferedReader input=new BufferedReader(new FileReader(name));
String text;
while((text=input.readLine())!=null)buffer.append(text+"\n");
text=buffer.toString().toLowerCase();
StringTokenizer st = new StringTokenizer(text);
int nTokens=st.countTokens();
// outArea.append("Dictionary contains "+nTokens+" entries\n");
// Define dictionary D
D=new String[nTokens];
while (st.hasMoreTokens()) {
D[nD]=st.nextToken(); nD++;
}
// outArea.append(D[0]+" "+D[nD-1]+"\n");
}
catch(IOException ioException){
JOptionPane.showMessageDialog(this,name.toString(),"FILE ERROR",JOptionPane.ERROR_MESSAGE);
}
} else outArea.append("FILE NOT FOUND!\n");
}
}
public void actionPerformed(ActionEvent event){
String word=event.getActionCommand();
word=word.trim(); // trim leading and trailing blanks
word=word.toLowerCase();
dict(word);
}
} // end test
public class Dict {
public static void main(String[] args){
test s=new test();
}
}
updated 2004-10-31