Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. public class PronouncingDictionary
  4. {
  5. private Map<String, List<String>> wordList = new HashMap();
  6. private Map<String, Boolean> isVowel = new HashMap();
  7. private Thesaurus th;
  8.  
  9. PronouncingDictionary(String wordlistFile, String phonemeFile)
  10. {
  11. try{
  12. BufferedReader word;
  13. BufferedReader phoneme;
  14. word = new BufferedReader(new FileReader(wordlistFile));
  15. phoneme = new BufferedReader(new FileReader(phonemeFile));
  16. while(!word.ready()){}
  17. while(!phoneme.ready()){}
  18.  
  19. String[] wordArr;
  20. String wordLine;
  21. String phLine;
  22. while((phLine = phoneme.readLine()) != null){
  23. String[] phArr = phLine.split(" ");
  24. String lword = phArr[0];
  25. if(phArr[1].equals("Vowel")){
  26. isVowel.put(lword,true);
  27. }
  28. else{
  29. isVowel.put(lword, false);
  30. }
  31. }
  32. while((wordLine = word.readLine()) != null){
  33. if(wordLine.contains(";;;"))
  34. continue;
  35. List<String> phonemeList = new ArrayList<>();
  36. wordArr = wordLine.split(" ");
  37. wordArr[1] = wordArr[1].replace(" ","");
  38. for (int i =1;i < wordArr.length;i++) {
  39. String w = wordArr[i];
  40. if(w.length() > 2)
  41. {
  42. w = w.substring(0,2);
  43. //System.out.println(w);
  44. }
  45. phonemeList.add(w);
  46. }
  47. wordList.put(wordArr[0].toLowerCase(),phonemeList);
  48.  
  49. }
  50. phoneme.close();
  51. }catch(IOException e){
  52. System.out.println("Error occured while reading / accessing files");
  53. }
  54. }
  55. public void setTh(Thesaurus th)
  56. {
  57. this.th = th;
  58. }
  59. int rhymePhoneCount(String word1, String word2) throws ThesaurusException
  60. {
  61. String[] words = word1.split(" ");
  62. int matchingV = 0;
  63. // System.out.println(words.length);
  64. for (int z =words.length-1; z >= 0 ;z-- ) {
  65. word1 = words[z].toLowerCase();
  66.  
  67. if(this.wordList.get(word2) == null){ //machiavellic is not in the list, should we just continue
  68. throw new ThesaurusException(word2+" does not exist in the list");
  69. }
  70. if(wordList.get(word1) == null)
  71. continue;
  72. List<String> ph1 = wordList.get(word1);
  73. List<String> ph2 = wordList.get(word2);
  74.  
  75. int b;
  76. int s;
  77. boolean big = true;
  78. if(ph1.size() > ph2.size()){
  79. b = ph2.size();
  80. s = ph1.size();
  81. big = false;
  82. }else{
  83. b = ph1.size();
  84. s = ph2.size();
  85. }
  86. b -=1;
  87. s -=1;
  88. for(int i = s; i >= 0; i--){
  89. if(!big){
  90. //System.out.println(ph1.get(i)+" "+ph2.get(b--));
  91. if(ph1.get(i).equals(ph2.get(b--)))
  92. matchingV++;
  93. else{
  94. return matchingV;
  95. }
  96. }
  97. else
  98. {
  99. if(ph1.get(b--).equals(ph2.get(i)))
  100. matchingV++;
  101. else{
  102. return matchingV;
  103. }
  104. }
  105. }
  106. }
  107. return 0;
  108. }
  109.  
  110. public List<String> findRhymingSynonyms(String rootWord, String rhymeWord, int degree)
  111. {
  112. List<String> matchingW = new ArrayList<>();
  113. List<String> syns;
  114. try{
  115. syns = this.th.getSynonyms(rootWord);
  116. for (String word : syns) {
  117. if(rhymePhoneCount(word,rhymeWord) >= degree)
  118. matchingW.add(word);
  119. }
  120. }catch(ThesaurusException e){
  121. System.out.println(e.getMessage());
  122. }
  123. return matchingW;
  124.  
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement