-
Notifications
You must be signed in to change notification settings - Fork 0
/
Phoneme.java
142 lines (136 loc) · 4.84 KB
/
Phoneme.java
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
import java.io.*;
import java.util.Scanner;
/**
* Class that simulates a phoneme according to the Mexican Spanish phoneme dictionary created at Golem IIMAS,UNAM.
* A string will be considered a phoneme if and only if it is included in the 'phoneme.phone' file
* NOTE: The user can change the current phoneme list just by modifying the previous file.
* @author alorozco53
* @version 09.0.23.13
* @see PhonemeNotFoundException
*/
public class Phoneme {
protected String phoneme; //string representation of the phoneme
protected String phFile; //phoneme dictionary
/**
* Constructor with two parameters
* @param p -- new phoneme
* @param phdict -- new phoneme dictionary
* @throws PhonemeNotFoundException -- if the initialized parameter is not a valid string representation of a phoneme
*/
public Phoneme(String p, String phdict) throws Exception {
try {
FileInputStream fstream = new FileInputStream(phdict.equals("") ? (this.phFile="phonemes.phone") : (this.phFile=phdict.trim()));
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while((strLine = br.readLine()) != null) {
if(strLine.trim().substring(0,strLine.indexOf(' ')).equals(p)) {
this.phoneme = p;
break;
}
}
in.close();
if(this.phoneme == null)
throw new PhonemeNotFoundException("The input string '"+p+"' is not a phoneme, please refer to the file 'phonemes.phone'");
} catch (FileNotFoundException e) {
System.err.println("Error: " + e.getMessage());
System.exit(1);
} catch (SecurityException e) {
System.err.println("Error: " + e.getMessage());
System.exit(1);
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
System.exit(1);
} catch (IndexOutOfBoundsException e) {
System.err.println("Error: " + e.getMessage());
System.exit(1);
}
}
/**
* ToString Method
* @return String -- the string representatation of the phoneme
*/
public String toString() {
return phoneme;
}
/**
* Returns true if the phoneme is a consonant; else, it returns false
* @return boolean -- true if the phoneme is a consonant
*/
public boolean isConsonant() {
try {
FileInputStream fstream = new FileInputStream(phFile);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while((strLine = br.readLine()) != null) {
if(strLine.trim().substring(0,strLine.indexOf(' ')).equals(phoneme))
return strLine.trim().charAt(strLine.length()-1) == 'c';
}
in.close();
} catch (FileNotFoundException e) {
System.err.println("Error: " + e.getMessage());
System.exit(1);
} catch (SecurityException e) {
System.err.println("Error: " + e.getMessage());
System.exit(1);
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
System.exit(1);
} catch (IndexOutOfBoundsException e) {
System.err.println("Error: " + e.getMessage());
System.exit(1);
}
return false;
}
/**
* Returns true if the phoneme is a vowell; else, it returns false
* @return boolean -- true if the phoneme is a vowell
*/
public boolean isVowell() {
try {
FileInputStream fstream = new FileInputStream(phFile);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while((strLine = br.readLine()) != null) {
if(strLine.trim().substring(0,strLine.indexOf(' ')).equals(phoneme))
return strLine.trim().charAt(strLine.length()-1) == 'v';
}
in.close();
} catch (FileNotFoundException e) {
System.err.println("Error: " + e.getMessage());
System.exit(1);
} catch (SecurityException e) {
System.err.println("Error: " + e.getMessage());
System.exit(1);
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
System.exit(1);
} catch (IndexOutOfBoundsException e) {
System.err.println("Error: " + e.getMessage());
System.exit(1);
}
return false;
}
/** This main only tests the current class */
public static void main(String[] args) {
for(int i = 0; i < args.length; i++)
System.out.println("i: "+i+"\t"+args[i]);
Phoneme[] array = new Phoneme[10];
Scanner in = new Scanner(System.in);
for(int i = 0; i < array.length; i++) {
try {
array[i] = new Phoneme(in.nextLine().trim(),"");
System.out.println("i: "+i+"\t"+array[i] + (!array[i].isConsonant() ? "\tvowell" : "\tconsonant"));
} catch(PhonemeNotFoundException e) {
System.out.println("oidjfisdjfisdjfi");
i--;
} catch(Exception e) {
System.err.println("Error: " + e.getMessage());
i--;
}
}
System.exit(0);
}
}