Advertisement
applebiscuit

WordSolver Old Code

Jan 20th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.47 KB | None | 0 0
  1. /*
  2. ArrayList<ArrayList<String>> myPossibilities = new ArrayList<>();//array of permutations seperated in characters
  3. ArrayList<String> myHand = LetterList(string);//get list of letters from user
  4. myPossibilities.add(myHand);
  5. Permutation(myHand, myPossibilities, 0);//find every permutation of input letters
  6. final ArrayList<String> myFinalHand = Merger(myPossibilities); //convert words that were broken down to characters to an array of strings
  7.  
  8. return Single.fromCallable(new Callable<String>() {
  9. @Override
  10. public String call() throws Exception {
  11. int currentvalue = 0;
  12. String currentword = "";
  13. ArrayList<String> validwords = new ArrayList<String>();
  14. for (String word : myFinalHand) {
  15. retrofit2.Response<WordResponse> response = oxford.lookupword(word).execute();
  16. if (response.isSuccessful()) {
  17. int tempvalue = Calculator(word);
  18. if(tempvalue > currentvalue){
  19. currentvalue = tempvalue;
  20. currentword = word;
  21. }
  22. }
  23. }
  24. return currentword;
  25. }
  26. });
  27. // ArrayList myDictionary = Dictionary();//get words for text file
  28. //String Solution = DeScramble(myFinalHand, myDictionary);//find matches between user input and text file, and output highest value word
  29.  
  30. //ArrayList Solution = DictionaryAPI(myPossibilities, 0);
  31. /*
  32. if(Solution.isEmpty()){
  33. return "No word can be created";
  34. }
  35. else{
  36. return Solution + " " + Calculator(Solution);
  37. //System.out.println("The highest value word you can make is:" + Solution + " " + "with a value of " + Calculator(Solution));
  38. }
  39. */
  40. //return Solution;
  41.  
  42. /*
  43. static ArrayList<String> LetterList(String string) {
  44. ArrayList<String> Hand = new ArrayList<>();
  45. Scanner scan = new Scanner(System.in);
  46.  
  47. //System.out.println("Enter all of the desired letter. Press ENTER when finished.");
  48. // String s = scan.nextLine();
  49.  
  50. string = string.replaceAll("\\d+", ""); //remove numbers
  51. string = string.toLowerCase(); //convert all to lowercase
  52. String[] splitStrings = string.split("(?!^)");
  53. for (int i = 0; i < splitStrings.length; i++) {
  54. if (splitStrings[i].equals(",") || splitStrings[i].equals(" ")) { //remove objects other than letters
  55. } else {
  56. Hand.add(splitStrings[i]);
  57. }
  58. }
  59. return Hand;
  60. }
  61.  
  62. static void Permutation(ArrayList hand, ArrayList possibilities, int start) {
  63. for (int i = 0; i < hand.size() - 1; i++) {
  64. ArrayList tempArray = new ArrayList(hand);
  65. Object tempValue = tempArray.get(start);
  66. tempArray.set(start, tempArray.get(i));
  67. tempArray.set(i, tempValue);
  68. if (possibilities.indexOf(tempArray) == -1) {
  69. possibilities.add(tempArray);
  70. }
  71. if (start < hand.size() - 1) {
  72. Permutation(tempArray, possibilities, start + 1);
  73. }
  74. }
  75. }
  76.  
  77. static ArrayList<String> Merger(ArrayList<ArrayList<String>> list) {
  78. ArrayList<String> mergedHand = new ArrayList<>(); //array of every permutation in seperate strings
  79. for (int i = 0; i < list.size(); i++) {
  80. ArrayList tempArray = (ArrayList) list.get(i);
  81. String tempString = "";
  82. for (int j = 0; j < tempArray.size(); j++) {
  83. tempString += tempArray.get(j);
  84. }
  85. mergedHand.add(tempString);
  86. }
  87. return mergedHand;
  88. }
  89.  
  90. public static Single<String> getValidWords(final ArrayList<String> list) {
  91. return Single.fromCallable(new Callable<String>() {
  92. @Override
  93. public String call() throws Exception {
  94. int currentvalue = 0;
  95. String currentword = "";
  96. ArrayList<String> validwords = new ArrayList<String>();
  97. for (String word : list) {
  98. retrofit2.Response<WordResponse> response = oxford.lookupword(word).execute();
  99. if (response.isSuccessful()) {
  100. //check if word is better than current answer
  101. }
  102. }
  103. return currentword;
  104. }
  105. });
  106. }
  107.  
  108. /*
  109. ArrayList Dictionary(){ //function to test input with WordList.txt
  110. ScrabbleHelper context = ScrabbleHelper.this;
  111. InputStream is = context.getResources().openRawResource(R.raw.wordlist);
  112.  
  113. ArrayList Dictionary = new ArrayList();
  114. //File WordList = new File("WordList.txt");
  115. String[] splitStrings;
  116.  
  117. Scanner line = new Scanner(is);
  118.  
  119. while (line.hasNextLine()) {
  120. String i = line.nextLine();
  121. splitStrings = i.split("");
  122. Dictionary.add(i);
  123. }
  124. line.close();
  125. return Dictionary;
  126. }
  127.  
  128. /*
  129. static String DeScramble(ArrayList list, ArrayList list2) {
  130. String PossibleAnswer = "";
  131. String TrueAnswer = "";
  132. ArrayList CheckedLetters = new ArrayList();
  133. int TrueValue = 0;
  134. for (int i = 0; i < list.size(); i++) {
  135. String word = (String) list.get(i);
  136. if (list2.contains(word)) {
  137. if (Calculator(word) > TrueValue) {
  138. TrueAnswer = word;
  139. TrueValue = Calculator(word);
  140. }
  141. }
  142.  
  143. }
  144. return TrueAnswer;
  145. }
  146. }
  147. */
  148.  
  149.  
  150. /*
  151. anagramica.findBest(string).enqueue(new Callback<WordResponse>() {
  152. @Override
  153. public void onResponse(Call<WordResponse> call, retrofit2.Response<WordResponse> response) {
  154. ArrayList<String> mypossibilities = new ArrayList<>(response.body().getBest());
  155. String solution = "";
  156. String possiblesolution = "";
  157. int solvalue = 0;
  158. int possiblesolvalue = 0;
  159. for (String word : mypossibilities) {
  160. possiblesolvalue = Calculator(word);
  161. if (possiblesolvalue > solvalue) {
  162. solvalue = possiblesolvalue;
  163. solution = word;
  164. }
  165. }
  166. }
  167.  
  168. @Override
  169. public void onFailure (Call < WordResponse > call, Throwable t){
  170. solution = "No Words Found";
  171. }
  172. }
  173.  
  174. );
  175. return solution;
  176. */
  177.  
  178. /*
  179. ArrayList<ArrayList<String>> myPossibilities = new ArrayList<>();//array of permutations seperated in characters
  180. ArrayList<String> myHand = LetterList(string);//get list of letters from user
  181. myPossibilities.add(myHand);
  182. Permutation(myHand, myPossibilities, 0);//find every permutation of input letters
  183. final ArrayList<String> myFinalHand = Merger(myPossibilities); //convert words that were broken down to characters to an array of strings
  184.  
  185. return Single.fromCallable(new Callable<String>() {
  186. @Override
  187. public String call() throws Exception {
  188. int currentvalue = 0;
  189. String currentword = "";
  190. ArrayList<String> validwords = new ArrayList<String>();
  191. for (String word : myFinalHand) {
  192. retrofit2.Response<WordResponse> response = oxford.lookupword(word).execute();
  193. if (response.isSuccessful()) {
  194. int tempvalue = Calculator(word);
  195. if(tempvalue > currentvalue){
  196. currentvalue = tempvalue;
  197. currentword = word;
  198. }
  199. }
  200. }
  201. return currentword;
  202. }
  203. });
  204. // ArrayList myDictionary = Dictionary();//get words for text file
  205. //String Solution = DeScramble(myFinalHand, myDictionary);//find matches between user input and text file, and output highest value word
  206.  
  207. //ArrayList Solution = DictionaryAPI(myPossibilities, 0);
  208. /*
  209. if(Solution.isEmpty()){
  210. return "No word can be created";
  211. }
  212. else{
  213. return Solution + " " + Calculator(Solution);
  214. //System.out.println("The highest value word you can make is:" + Solution + " " + "with a value of " + Calculator(Solution));
  215. }
  216. */
  217. //return Solution;
  218.  
  219.  
  220. /*
  221. static void init() {
  222. OkHttpClient client = new OkHttpClient.Builder()
  223. .addInterceptor(new Interceptor() {
  224. @Override
  225. public Response intercept(Chain chain) throws IOException {
  226. Request request = chain.request().newBuilder()
  227. .addHeader("app_id", "7ddb00d8")
  228. .addHeader("app_key", "9b38e8c1685a64d7b05dfb07c0666559")
  229. .build();
  230. return chain.proceed(request);
  231. }
  232. })
  233. .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
  234. .build();
  235. Retrofit retrofit = new Retrofit.Builder()
  236. .baseUrl("https://od-api.oxforddictionaries.com/api/v1/")
  237. .client(client)
  238. .addConverterFactory(GsonConverterFactory.create())
  239. .build();
  240. oxford = retrofit.create(Oxford.class);
  241. }
  242. */
  243.  
  244.  
  245. /*oxford.lookupword("fish").enqueue(new Callback<WordResponse>() {
  246. @Override
  247. public void onResponse(Call<WordResponse> call, retrofit2.Response<WordResponse> response) {
  248. if (response.isSuccessful()) {
  249. Snackbar.make(root, "word exists", Snackbar.LENGTH_SHORT).show();
  250. } else {
  251. Snackbar.make(root, "nope", Snackbar.LENGTH_SHORT).show();
  252. }
  253. }
  254.  
  255. @Override
  256. public void onFailure(Call<WordResponse> call, Throwable t) {//if person has no internet connection
  257. Snackbar.make(root, "failure", Snackbar.LENGTH_SHORT).show();
  258. t.printStackTrace();
  259. }
  260. });*/
  261.  
  262.  
  263. /*
  264. ScrabbleHelper.solve(message)
  265. .subscribeOn(Schedulers.io())
  266. .observeOn(AndroidSchedulers.mainThread())
  267. .subscribe(new SingleObserver<String>() {
  268. @Override
  269. public void onSubscribe(Disposable d) {
  270. }
  271. @Override
  272. public void onSuccess(String bestword) {
  273. Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);
  274. intent.putExtra(EXTRA_MESSAGE, bestword);
  275. startActivity(intent);
  276. //Snackbar.make(root, "Best scoring word is " + bestword, Snackbar.LENGTH_SHORT).show();
  277. }
  278.  
  279. @Override
  280. public void onError(Throwable e) {
  281. Snackbar.make(root, "failure", Snackbar.LENGTH_SHORT).show();
  282. e.printStackTrace();
  283. }
  284. });
  285. */
  286. //use solver function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement