Doublets - Links


/ Published in: Java
Save to your folder(s)

Doublets Project


Copy this code and paste it in your HTML
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.util.HashMap;
  5. import java.util.HashSet;
  6. import java.util.Map;
  7. import java.util.Set;
  8.  
  9. public class Links {
  10.  
  11. Map<String, Set<String>> allCandidates; // Maps each word to a set of possible candidates
  12.  
  13. /**
  14. * Constructs a Links object representing a database of all the words and their candidates.
  15. * @param filename - the name of the dictionary input file
  16. */
  17. public Links(String filename) {
  18. allCandidates = new HashMap<String, Set<String>>();
  19. try {
  20. populateCandidates(filename);
  21. } catch (IOException e) {
  22. System.out.println("Invalid dictionary number. Defaulting to 35");
  23. try {
  24. populateCandidates("english.cleaned.all.35.txt");
  25. } catch (IOException e1) {
  26. //should never reach here
  27. System.out.print("Dictionary file is missing!");
  28. }
  29. }
  30. }
  31.  
  32. /**
  33. * Getter method for candidates
  34. * @param word
  35. * @return set of possible candidates for word
  36. */
  37. public Set<String> getCandidates(String word) {
  38. return allCandidates.get(word);
  39. }
  40.  
  41. /**
  42. * Populates the map of words and their candidates using a BufferedReader
  43. * @param filename - dictionary
  44. * @throws IOException
  45. */
  46. private void populateCandidates(String filename)
  47. throws IOException {
  48. HashSet<String> set = new HashSet<String>(); // Temporarily stores all of the words in the file
  49. BufferedReader reader = new BufferedReader(new FileReader(filename));
  50. String word;
  51.  
  52. //Adds all the words in the dictionary to the set
  53. while ((word = reader.readLine()) != null) {
  54. set.add(word);
  55. }
  56.  
  57. //examines each word in the dictionary set
  58. for(String s : set){
  59. String tempString = s;
  60. HashSet<String> wordset = new HashSet<String>(); //set of candidates for this word
  61. //iterates through each possible value for each character of the word, finding all possible candidates
  62. for(int i = 0; i < s.length(); i++){
  63. String start = s.substring(0, i);
  64. String end = "";
  65. if(i < s.length() - 1)
  66. end = s.substring(i + 1);
  67. for(int j = 97; j <= 122; j++){
  68. tempString = start + (char)(j) + end;
  69. if(!tempString.equals(s) && set.contains(tempString)) //tests if it is a valid word in the dictionary and is not equal to itself
  70. wordset.add(tempString);
  71. }
  72. }
  73. if (wordset.isEmpty()) //return null if the set is empty
  74. allCandidates.put(s, null);
  75. else
  76. allCandidates.put(s, wordset);
  77.  
  78. }
  79. reader.close();
  80.  
  81. }
  82.  
  83. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.