Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Collections;
  4. import java.util.Comparator;
  5. import java.util.HashMap;
  6. import java.util.HashSet;
  7.  
  8.  
  9. //find a way to replace sorted ArrayLists that are equal with their original order; map doesnt work
  10. //because assigns both versions to the same original key
  11.  
  12. public class SubstringSort{
  13.  
  14. class syllsorter implements Comparator<ArrayList<String>>{
  15. public int compare(ArrayList<String> a, ArrayList<String> b){
  16. /***
  17. for(int i=0; i<a.size(); i++){
  18. int number = a.get(i).compareTo(b.get(i));
  19. if(number!=0){
  20. return number;
  21. }
  22. }
  23. if(a.size() > b.size()){
  24. return 1;
  25. }
  26. if(a.size() < b.size()){
  27. return -1;
  28. }
  29. else{
  30. return 0;
  31. }
  32. ***/
  33. int size = 0;
  34. if(a.size() <= b.size()){
  35. size = a.size();
  36. }
  37. if(a.size() > b.size()){
  38. size = b.size();
  39. }
  40. for(int i=0; i<size; i++){
  41. int number = a.get(i).compareTo(b.get(i));
  42. if(number!=0){
  43. return number;
  44. }
  45. }
  46. if(a.size() > b.size()){
  47. return 1;
  48. }
  49. if(a.size() < b.size()){
  50. return -1;
  51. }
  52. else{
  53. return 0;
  54. }
  55. }
  56. }
  57.  
  58. public void sort(ArrayList<ArrayList<String>> listoflists){
  59. Collections.sort(listoflists, new syllsorter());
  60. }
  61.  
  62. public String[] sortSubstrings(String[] wordList) {
  63. ArrayList<ArrayList<String>> Decomps = new ArrayList<ArrayList<String>>();
  64. ArrayList<ArrayList<String>> sortedDecomps = new ArrayList<ArrayList<String>>();
  65. HashMap<ArrayList<String>, ArrayList<String>> map = new HashMap<ArrayList<String>, ArrayList<String>>();
  66. for(int i=0; i<wordList.length; i++){
  67. ArrayList<String> decomp = syllseparation(wordList[i]);
  68. ArrayList<String> decomp2 = syllseparation(wordList[i]);
  69. Collections.sort(decomp);
  70. map.put(decomp2, decomp);
  71. Decomps.add(decomp2);
  72. sortedDecomps.add(decomp);
  73. }
  74. System.out.println(map);
  75. sort(sortedDecomps);
  76. System.out.println(sortedDecomps);
  77. HashSet<Integer> indexes = new HashSet<Integer>();
  78. for(int i=0; i<sortedDecomps.size()-1; i++){
  79. for(int j=i+1; j<sortedDecomps.size(); j++){
  80. if(sortedDecomps.get(i).equals(sortedDecomps.get(j))){
  81. indexes.add(Decomps.indexOf(Decomps.get(i)));
  82. indexes.add(Decomps.indexOf(Decomps.get(j)));
  83. }
  84. }
  85. }
  86. System.out.println(indexes);
  87. ArrayList<ArrayList<String>> sublist = new ArrayList<ArrayList<String>>();
  88. for(int i=0; i<sortedDecomps.size(); i++){
  89. if(indexes.contains(i)){
  90. for(ArrayList<String> x : map.keySet()){
  91. if(sortedDecomps.get(i) == map.get(x)){
  92. sublist.add(x);
  93. }
  94. }
  95. }
  96. }
  97. sort(sublist);
  98. System.out.println(sublist);
  99. for(int i=0; i<sortedDecomps.size(); i++){
  100. if(indexes.contains(i)){
  101. sortedDecomps.set(i, sublist.get(0));
  102. sublist.remove(0);
  103. }
  104. }
  105.  
  106. System.out.println(sortedDecomps);
  107.  
  108.  
  109. /***
  110. sort(sortedDecomps);
  111. System.out.println(sortedDecomps);
  112. System.out.println(sublist);
  113. sort(sublist);
  114. System.out.println(sublist);
  115. for(int i=0; i<sublist.size(); i++){
  116. ArrayList<String> test = sublist.get(i);
  117. Collections.sort(test);
  118. for(int j=0; j<sortedDecomps.size(); j++){
  119. if(test.equals(sortedDecomps.get(j))){
  120. sortedDecomps.set(sortedDecomps.indexOf(sortedDecomps.get(j)), sublist.get(i));
  121. break;
  122. }
  123. }
  124. }
  125. System.out.println(sortedDecomps);
  126. ***/
  127. ArrayList<String> temp = new ArrayList<String>();
  128. for(int i=0; i<sortedDecomps.size(); i++){
  129. for(ArrayList<String> x : map.keySet()){
  130. if(sortedDecomps.get(i) == map.get(x)){
  131. sortedDecomps.set(i, x);
  132. }
  133. }
  134. String str = "";
  135. for(int j=0; j<sortedDecomps.get(i).size(); j++){
  136. str += sortedDecomps.get(i).get(j);
  137. }
  138. temp.add(str);
  139. }
  140. System.out.println(temp);
  141.  
  142. String[] answer = new String[temp.size()];
  143. for(int i=0; i<temp.size(); i++){
  144. answer[i] = temp.get(i);
  145. }
  146. System.out.println(Arrays.toString(answer));
  147. return answer;
  148.  
  149.  
  150.  
  151. }
  152.  
  153. public ArrayList<String> syllseparation(String word){
  154. String vowels = "aeiou";
  155. String consonants = "bcdfghjklmnpqrstvwxyz";
  156. ArrayList<String> syllables = new ArrayList<String>();
  157. int count = 0;
  158. int startIndex = 0;
  159. for(int i=0; i<word.length(); i++){
  160. if(i==(word.length()-1)){
  161. String syll = word.substring(startIndex, word.length());
  162. syllables.add(syll);
  163. continue;
  164. }
  165. if(vowels.contains(""+word.charAt(i)) && consonants.contains(""+word.charAt(i+1))){
  166. if(count == 0){
  167. String syll = word.substring(startIndex, i+1);
  168. syllables.add(syll);
  169. startIndex = i+1;
  170. count++;
  171. }
  172. else{
  173. String syll = word.substring(startIndex, i+1);
  174. syllables.add(syll);
  175. startIndex = i+1;
  176. }
  177.  
  178. }
  179. }
  180. return syllables;
  181. }
  182. public static void main(String[] main){
  183. SubstringSort a = new SubstringSort();
  184. String[] wordList = {"ba", "ba", "ba"};
  185. a.sortSubstrings(wordList);
  186. }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement