linguistics/java group
mh's rhyming dictionary
overview
- There are three versions of this program
- The program requires the "phondic.english" file.
This is an ascii file of approximately 20,000 English words. Each
word occurs in a separate row. Each row contains the following
information in tab-separated fields: transcription, spelling,
part of speech, frequency.
- This handout gives the html and java code for the 1.1 applet
and the java code for the standalone program. Line numbers have
been added here so we can talk about the code.
HTML page for the 1.1 applet
1 <html>
2 <head>
3 <title>A rhyming dictionary</title>
4 </head>
5 <body>
6 This is a primitive rhyming dictionary written in Java. Enter a
7 word in lowercase letters and press the button.
8 <applet code="rhap2.class" archive="rhap2.jar" height=300 width=400>
9 You don't have a java-enabled browser!
10 </applet>
11 <hr>
12 <author>Mike Hammond</author>
13 </body>
14 </html>
Applet
1 import java.applet.*;
2 import java.awt.*;
3 import java.awt.event.*;
4 import java.io.*;
5 import java.net.*;
6 import java.util.*;
7
8 public class rhap2 extends Applet implements ActionListener {
9 Button search = new Button("Search");
10 Button clear = new Button("Clear");
11 TextField word = new TextField(15);
12 TextArea rhymes = new TextArea(15,20);
13 String aword;
14 StringBuffer theresult = new StringBuffer("");
15
16 public void init() {
17 setBackground(Color.pink);
18 search.setBackground(Color.green);
19 search.addActionListener(this);
20 search.setActionCommand("search");
21 add(search);
22 clear.setBackground(Color.red);
23 clear.addActionListener(this);
24 clear.setActionCommand("clear");
25 add(clear);
26 word.setBackground(Color.yellow);
27 add(word);
28 rhymes.setBackground(Color.yellow);
29 rhymes.setEditable(false);
30 add(rhymes);
31 }
32
33 public void actionPerformed(ActionEvent e) {
34 if (e.getActionCommand() == "search") {
35 if (word.getText().equals(""))
36 rhymes.setText("Enter a word above.");
37 else {
38 rhymes.setText("Searching...");
39 aword = word.getText();
40 rhymes.setText(doit());
41 }
42 }
43 if (e.getActionCommand() == "clear") {
44 rhymes.setText("");
45 word.setText("");
46 }
47 }
48
49 public String doit() {
50 BufferedReader mh;
51 String line;
52 String therhyme = null;
53 theresult = new StringBuffer("the word: " + aword + "\n");
54 URL url;
55 try {
56 url = new URL(getDocumentBase(),"phondic.english");
57 mh = new BufferedReader(new InputStreamReader(url.openStream()));
58 while (((line = mh.readLine()) != null) && (therhyme == null)) {
59 therhyme = this.mhp(line, aword);
60 }
61 }
62 catch(Exception e) {}
63 theresult.append("the rhyme: " + therhyme + "\n");
64 try {
65 url = new URL(getDocumentBase(),"phondic.english");
66 mh = new BufferedReader(new InputStreamReader(url.openStream()));
67 while ((line = mh.readLine()) != null) this.mhp2(line,therhyme);
68 }
69 catch (Exception e)
70 {theresult.append("\nThe word is not\nin the dictionary\n" +
71 "or the dictionary\nis unavailable.");}
72 return theresult.toString();
73 }
74
75 String mhp(String astring, String string2) {
76 StringTokenizer words = new StringTokenizer(astring);
77 String[] word = new String[words.countTokens()];
78 int i = 0;
79 int stress = 0;
80 while (words.hasMoreTokens()) {
81 word[i] = words.nextToken();
82 i++;
83 }
84 if (word[0].equals(string2)) {
85 stress = word[2].indexOf("'");
86 return word[2].substring(stress);
87 }
88 else { return null; }
89 }
90
91 void mhp2(String theline, String arhyme) {
92 StringTokenizer words2 = new StringTokenizer(theline);
93 String[] wordsinline = new String[words2.countTokens()];
94 int i = 0;
95 while (words2.hasMoreTokens()) {
96 wordsinline[i] = words2.nextToken();
97 i++;
98 }
99 if (wordsinline[2].endsWith(arhyme)) {
100 theresult.append(wordsinline[0] + "\n");
101 }
102 }
103 }
Standalone
1 import java.io.*;
2 import java.util.*;
3 import java.net.*;
4 import java.applet.*;
5
6 public class rhyme {
7 String aword;
8 StringBuffer theresult = new StringBuffer("");
9 public static void main(String argv[]) {
10 rhyme mhrhyme = new rhyme(argv[0]);
11 System.out.println(mhrhyme.doit());
12 }
13 rhyme(String inword) {aword = inword;}
14 public String doit() {
15 BufferedReader mh;
16 String line;
17 String therhyme = null;
18 theresult.append("the word: " + aword + "\n");
19 URL url;
20 try {
21 url = new URL(getDocumentBase(),"phondic.english");
22 mh = new BufferedReader(new InputStreamReader(url.openStream()));
23 while (((line = mh.readLine()) != null) && (therhyme == null)) {
24 therhyme = this.mhp(line, aword);
25 }
26 }
27 catch(Exception e)
28 {System.out.println("Usage: java rhyme word");}
29 theresult.append("the rhyme: " + therhyme + "\n");
30 try {
31 url = new URL(getDocumentBase(),"phondic.english");
32 mh = new BufferedReader(new InputStreamReader(url.openStream()));
33 while ((line = mh.readLine()) != null) this.mhp2(line,therhyme);
34 }
35 catch (Exception e)
36 {System.out.println("The word is not in the dictionary" +
37 " or the dictionary is unavailable.");}
38 return theresult.toString();
39 }
40 String mhp(String astring, String string2) {
41 StringTokenizer words = new StringTokenizer(astring);
42 String[] word = new String[words.countTokens()];
43 int i = 0;
44 int stress = 0;
45 while (words.hasMoreTokens()) {
46 word[i] = words.nextToken();
47 i++;
48 }
49 if (word[0].equals(string2)) {
50 stress = word[2].indexOf("'");
51 return word[2].substring(stress);
52 }
53 else { return null; }
54 }
55
56 void mhp2(String theline, String arhyme) {
57 StringTokenizer words2 = new StringTokenizer(theline);
58 String[] wordsinline = new String[words2.countTokens()];
59 int i = 0;
60 while (words2.hasMoreTokens()) {
61 wordsinline[i] = words2.nextToken();
62 i++;
63 }
64 if (wordsinline[2].endsWith(arhyme)) {
65 theresult.append(wordsinline[0] + "\n");
66 }
67 }
68 }