Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class PronouncingDictionary
- {
- private Map<String, List<String>> wordList = new HashMap();
- private Map<String, Boolean> isVowel = new HashMap();
- private Thesaurus th;
- PronouncingDictionary(String wordlistFile, String phonemeFile)
- {
- try{
- BufferedReader word;
- BufferedReader phoneme;
- word = new BufferedReader(new FileReader(wordlistFile));
- phoneme = new BufferedReader(new FileReader(phonemeFile));
- while(!word.ready()){}
- while(!phoneme.ready()){}
- String[] wordArr;
- String wordLine;
- String phLine;
- while((phLine = phoneme.readLine()) != null){
- String[] phArr = phLine.split(" ");
- String lword = phArr[0];
- if(phArr[1].equals("Vowel")){
- isVowel.put(lword,true);
- }
- else{
- isVowel.put(lword, false);
- }
- }
- while((wordLine = word.readLine()) != null){
- if(wordLine.contains(";;;"))
- continue;
- List<String> phonemeList = new ArrayList<>();
- wordArr = wordLine.split(" ");
- wordArr[1] = wordArr[1].replace(" ","");
- for (int i =1;i < wordArr.length;i++) {
- String w = wordArr[i];
- if(w.length() > 2)
- {
- w = w.substring(0,2);
- //System.out.println(w);
- }
- phonemeList.add(w);
- }
- wordList.put(wordArr[0].toLowerCase(),phonemeList);
- }
- phoneme.close();
- }catch(IOException e){
- System.out.println("Error occured while reading / accessing files");
- }
- }
- public void setTh(Thesaurus th)
- {
- this.th = th;
- }
- int rhymePhoneCount(String word1, String word2) throws ThesaurusException
- {
- String[] words = word1.split(" ");
- int matchingV = 0;
- // System.out.println(words.length);
- for (int z =words.length-1; z >= 0 ;z-- ) {
- word1 = words[z].toLowerCase();
- if(this.wordList.get(word2) == null){ //machiavellic is not in the list, should we just continue
- throw new ThesaurusException(word2+" does not exist in the list");
- }
- if(wordList.get(word1) == null)
- continue;
- List<String> ph1 = wordList.get(word1);
- List<String> ph2 = wordList.get(word2);
- int b;
- int s;
- boolean big = true;
- if(ph1.size() > ph2.size()){
- b = ph2.size();
- s = ph1.size();
- big = false;
- }else{
- b = ph1.size();
- s = ph2.size();
- }
- b -=1;
- s -=1;
- for(int i = s; i >= 0; i--){
- if(!big){
- //System.out.println(ph1.get(i)+" "+ph2.get(b--));
- if(ph1.get(i).equals(ph2.get(b--)))
- matchingV++;
- else{
- return matchingV;
- }
- }
- else
- {
- if(ph1.get(b--).equals(ph2.get(i)))
- matchingV++;
- else{
- return matchingV;
- }
- }
- }
- }
- return 0;
- }
- public List<String> findRhymingSynonyms(String rootWord, String rhymeWord, int degree)
- {
- List<String> matchingW = new ArrayList<>();
- List<String> syns;
- try{
- syns = this.th.getSynonyms(rootWord);
- for (String word : syns) {
- if(rhymePhoneCount(word,rhymeWord) >= degree)
- matchingW.add(word);
- }
- }catch(ThesaurusException e){
- System.out.println(e.getMessage());
- }
- return matchingW;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement