BowlerKernel
PhoneticDictionary.java
Go to the documentation of this file.
1 package com.neuronrobotics.bowlerstudio.lipsync;
2 
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileReader;
6 import java.util.ArrayList;
7 import java.util.Arrays;
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11 
12 import com.neuronrobotics.bowlerstudio.AudioStatus;
13 
14 public class PhoneticDictionary {
15  private Map<String, ArrayList<String>> dictionary;
16 
17  public PhoneticDictionary(File f) throws Exception {
18  dictionary = new HashMap<>();
19  init(f);
20  }
21 
22  private String normalizePhonemes(String phonemes) {
23  StringBuilder result = new StringBuilder();
24  for (int i = 0; i < phonemes.length(); i++) {
25  char c = phonemes.charAt(i);
26  if (c == ' ' || (c >= 'A' && c <= 'Z')) {
27  result.append(c);
28  }
29  }
30  return result.toString().toLowerCase();
31  }
32 
33  private ArrayList<String> parseEntry(String entry, Map<String, ArrayList<String>> d) {
34  String[] tokens = entry.split(" ");
35  if (tokens.length < 2) {
36  return null;
37  }
38  String word = tokens[0].trim().toLowerCase();
39  if (word.endsWith(")")) {
40  return null;
41  }
42  ArrayList<String> mine = new ArrayList<String>();
43  for(int i=1;i<tokens.length;i++) {
44  String phonemes = normalizePhonemes(tokens[i].trim());
45  mine.add(phonemes);
46  }
47 
48  //println "Adding to dictionary :"+word+" = phoneme "+mine
49  d.put(word, mine);
50  return mine;
51  }
52 
53  private Map<String, ArrayList<String>> parseDictionary(String dictionaryText) {
54  Map<String, ArrayList<String>> dictionary = new HashMap<>();
55  String[] entries = dictionaryText.split("\n");
56  for (String entry : entries) {
57  if (entry.startsWith(";;;")) {
58  continue;
59  }
60  ArrayList<String> result = parseEntry(entry, dictionary);
61  if (result == null) {
62  continue;
63  }
64  }
65  return dictionary;
66  }
67 
68  private String fetchDictionaryText(File dictionaryUrl) throws Exception {
69  BufferedReader reader = new BufferedReader(new FileReader(dictionaryUrl));
70  StringBuilder stringBuilder = new StringBuilder();
71  String line;
72  while ((line = reader.readLine()) != null) {
73  stringBuilder.append(line).append("\n");
74  }
75  reader.close();
76  return stringBuilder.toString();
77  }
78 
79  public void init(File dictionaryUrl) throws Exception {
80  String dictionaryText = fetchDictionaryText(dictionaryUrl);
81  dictionary = parseDictionary(dictionaryText);
82  }
83 
84  public ArrayList<String> find(String w) {
85  List<String> extra=null;
86  if(w.endsWith("n't")) {
87  String newW =w.substring(0,w.length()-3);
88  //println "Contraction reduced "+newW+" from "+w
89  w=newW;
90  extra =Arrays.asList("n", "t");
91  }
92  if(w.endsWith("'ar")) {
93  String newW =w.substring(0,w.length()-3);
94  //println "Contraction reduced "+newW+" from "+w
95  w=newW;
96  extra =Arrays.asList( "r");
97  }
98  if(w.endsWith("'ll")) {
99  String newW =w.substring(0,w.length()-3);
100  //println "Contraction reduced "+newW+" from "+w
101  w=newW;
102  extra =Arrays.asList("uw", "l");
103  }
104  if(w.endsWith("'ve")) {
105  String newW =w.substring(0,w.length()-3);
106  //println "Contraction reduced "+newW+" from "+w
107  w=newW;
108  extra =Arrays.asList("v");
109  }
110  if(w.endsWith("'re")) {
111  String newW =w.substring(0,w.length()-3);
112  //println "Contraction reduced "+newW+" from "+w
113  w=newW;
114  extra =Arrays.asList("r");
115  }
116  if(w.endsWith("'s")) {
117  String newW =w.substring(0,w.length()-2);
118  //println "Contraction reduced "+newW+" from "+w
119  w=newW;
120  extra =Arrays.asList("s");
121  }
122  if(w.endsWith("'d")) {
123  String newW =w.substring(0,w.length()-2);
124  //println "Contraction reduced "+newW+" from "+w
125  w=newW;
126  extra =Arrays.asList("d");
127  }
128  ArrayList<String> phonemes = new ArrayList<String>();
129  ArrayList<String> dictionaryGet = dictionary.get(w);
130  if(dictionaryGet==null) {
131  dictionaryGet = new ArrayList<String>();
132  byte[] bytes = w.getBytes();
133  //println "Sounding out "+w
134  for(int i=0;i<w.length();i++) {
135  String charAt = ((char)bytes[i])+"";
136  //println charAt
137  if(AudioStatus.getFromPhoneme(charAt)!=null) {
138  dictionaryGet.add(charAt);
139  }else {
140  for(String s:AudioStatus.getPhonemes() ) {
141  if(s.contains(charAt)) {
142  dictionaryGet.add(s);
143  break;
144  }
145  }
146  }
147  }
148  //println "New Word: "+w+" "+dictionaryGet
149  dictionary.put(w,dictionaryGet);
150  }
151  phonemes.addAll(dictionaryGet);
152  if(extra!=null) {
153  phonemes.addAll(extra);
154  }
155  return phonemes;
156  }
157 }
ArrayList< String > parseEntry(String entry, Map< String, ArrayList< String >> d)
Map< String, ArrayList< String > > parseDictionary(String dictionaryText)
static AudioStatus getFromPhoneme(String code)