File: SegmenterTests.java

package info (click to toggle)
freetts 1.2.2-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 65,244 kB
  • sloc: java: 21,305; xml: 1,340; sh: 969; lisp: 587; ansic: 241; makefile: 25; awk: 11
file content (225 lines) | stat: -rw-r--r-- 6,353 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
 * Copyright 2001 Sun Microsystems, Inc.
 * 
 * See the file "license.terms" for information on usage and
 * redistribution of this file, and for a DISCLAIMER OF ALL 
 * WARRANTIES.
 */
package tests;
import junit.framework.*;
import java.util.*;
import java.io.*;
import com.sun.speech.freetts.*;

/**
 * Tests for the Utterance class
 * 
 * @version 1.0
 */
public class SegmenterTests extends TestCase {
    Voice voice;
    Utterance utterance;
    UtteranceProcessor wordSylSeg = new Segmenter();

    /**
     * JUnit style tests that test proper feature and relation
     * behavior.
     * 
     * @param  name the name of the test.
     */
    public SegmenterTests(String name) {
	super(name);
    }

    /**
     * given some text, create a word relation
     * and the syllable relations that go with it.
     *
     * @param text the text to process
     *
     * @return the utterance
     */
    public Utterance  getSyllables(String text) {
        VoiceManager voiceManager = VoiceManager.getInstance();
        voice = voiceManager.getVoice("kevin");
	voice.allocate();
	utterance = new Utterance(voice);
	Relation words = utterance.createRelation("Word");
	StringTokenizer tok = new StringTokenizer(text);

       while (tok.hasMoreTokens()) {
	   Item word = words.appendItem();
	   word.getFeatures().setString("name", tok.nextToken().toLowerCase());
       }
        try {
	    wordSylSeg.processUtterance(utterance);
	} catch (ProcessException pe) {
	    System.out.println("Error processing " + text);
	}
	return utterance;
    } 


    /**
     * Tests simple syllable and segment behavior
     */
    public void testHowNowBrownCow() {
	Utterance u = getSyllables("how now brown cowboy");
	Relation segment = u.getRelation("Segment");
	assertTrue("segment", segment != null);
	assertTrue("Syllable", u.getRelation("Syllable") != null);
	assertTrue("SylStructure", u.getRelation("SylStructure") != null);
    }

    /**
     * Tests to see if the segment names are created properly, as well
     * as the syllable structure is created properly.
     */
    public void testJanuary() {
	Utterance u = getSyllables("january first two thousand and one");
	Relation segment = u.getRelation("Segment");
	assertTrue("segment", segment != null);
	assertTrue("Syllable", u.getRelation("Syllable") != null);
	assertTrue("SylStructure", u.getRelation("SylStructure") != null);

	// tests the segment

	//assertTrue("segment size", segment.getItems().size() == 26);

	// spot check some segments
	Item i = segment.getHead();
	assertTrue("seg jh", i.toString().equals("jh"));
	i = i.getNext();
	assertTrue("seg ae", i.toString().equals("ae"));
	i = i.getNext();
	assertTrue("seg n",  i.toString().equals("n"));
	i = i.getNext();
	assertTrue("seg y",  i.toString().equals("y"));
	i = i.getNext();
	assertTrue("seg uw", i.toString().equals("uw"));
	i = i.getNext();
	assertTrue("seg eh", i.toString().equals("eh"));
	i = i.getNext();
	assertTrue("seg r",  i.toString().equals("r"));
	i = i.getNext();
	assertTrue("seg iy", i.toString().equals("iy"));
	i = i.getNext();
	assertTrue("seg f",  i.toString().equals("f"));
	i = i.getNext();
	assertTrue("seg er", i.toString().equals("er"));
	i = i.getNext();
	assertTrue("seg s",  i.toString().equals("s"));
	i = i.getNext();
	assertTrue("seg t",  i.toString().equals("t"));
	i = i.getNext();


	// spot check the SylStructure
	Relation sylStructure = u.getRelation("SylStructure");
	//assertTrue("sylStructure size", sylStructure.getItems().size() == 6);

	Item si = sylStructure.getHead();
	assertTrue("january", si.toString().equals("january"));
	si = si.getNext();
	assertTrue("first", si.toString().equals("first"));
	si = si.getNext();
	assertTrue("two", si.toString().equals("two"));
	si = si.getNext();
	assertTrue("thousand", si.toString().equals("thousand"));
	si = si.getNext();
	assertTrue("and", si.toString().equals("and"));
	si = si.getNext();
	assertTrue("one", si.toString().equals("one"));
	si = si.getNext();

	Item january = sylStructure.getHead();

	assertTrue("findItem", january.findItem("R:Word").
		toString().equals("january"));

	assertTrue("findItem", january.findItem("R:Word.n").
		toString().equals("first"));
	assertTrue("findItem", january.findItem("R:Word.n.n").
		toString().equals("two"));
	assertTrue("findItem", january.findItem("R:Word.n.n.p").
		toString().equals("first"));

	PrintWriter pw = new PrintWriter(System.out);
	january.findItem("daughter.daughter").dump(pw, 4, "dd");

	assertTrue("findItem",
		january.findItem("daughter.daughter").
		toString().equals("jh"));

	assertTrue("findItem",
		january.findItem("daughter.daughter.n").
		toString().equals("ae"));

	assertTrue("findItem",
		january.findItem("daughter.daughter.n.p").
		toString().equals("jh"));
	assertTrue("findItem",
		january.findItem("daughter.daughtern").
		toString().equals("n"));
	assertTrue("findItem",
		january.findItem("daughter.daughtern.parent.parent").
		toString().equals("january"));
	assertTrue("findItem",
		january.findItem(
	"daughter.daughtern.parent.parent.R:Word.R:SylStructure" +
	".daughter.daughter.n").toString().equals("ae"));

	assertTrue("findItem",
		january.findItem("daughter.daughtern").
		toString().equals("n"));

	assertTrue("findFeature", january.findFeature("daughter.stress").
		toString().equals("1"));

	Item firstSyllable =  january.getDaughter();
	/*
	assertTrue("num seg in syl",
		firstSyllable.getDaughters().size() == 3);
	*/
	Item l = firstSyllable.getDaughter();
	assertTrue("syl jh", l.toString().equals("jh"));
	l = l.getNext();
	assertTrue("syl ae", l.toString().equals("ae"));
	l = l.getNext();
	assertTrue("syl n", l.toString().equals("n"));
	l = l.getNext();

    }


    /**
     * Factory method that creates the test suite.
     * 
     * @return the test suite.
     */
    public static Test suite() {
	return new TestSuite(SegmenterTests.class);
    } 



    /**
     * Main entry point for this test suite.
     * 
     * @param  args    the command line arguments.
     */
    public static void main(String[] args) {
	// String inputText = "for score and seven years ago";
	String inputText = "january first two thousand and one";

	if (args.length > 0) {
	    inputText = args[0];
	}

	SegmenterTests wsst = new SegmenterTests("tests");
	Utterance t1 = wsst.getSyllables( inputText);
	t1.dump("t1");
    } 
}