Posted By


obrienm on 08/29/14

Tagged


Statistics


Viewed 191 times
Favorited by 0 user(s)

Related snippets


Doublets - Doublets


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

Doublets Project


Copy this code and paste it in your HTML
  1. import java.util.Set;
  2.  
  3. /**
  4.  * Contains the general algorithm to search for doublets.
  5.  *
  6.  * @author Alia Robinson and Matthew O'Brien
  7.  * Created Mar 18, 2011.
  8.  */
  9. public class Doublets {
  10. Links dictionary; // Dictionary is used for links
  11.  
  12. /**
  13. * Takes in the given instance of links and assigns it to the dicitionary.
  14. *
  15. * @param The given instance of links
  16. */
  17. public Doublets(Links links) {
  18. dictionary = links;
  19. }
  20.  
  21. /**
  22. * Finds the chain of words from the 'start' to the 'end'
  23. *
  24. * @param start - start word
  25. * @param end - end word
  26. * @param mgr - chain manager
  27. * @return Calls the recursive helper method which will ultimately return
  28. * the chain.
  29. */
  30. public Chain getChain(String start, String end, ChainManager mgr) {
  31. Chain startChain = new Chain();
  32. startChain = startChain.addLast(start);
  33. mgr.add(startChain);
  34. mgr.setMaxSize(1);
  35.  
  36. Chain nextChain = startChain;
  37. while (nextChain != null) {
  38. Set<String> candidateSet = dictionary.getCandidates(nextChain
  39. .getLast());
  40. if (candidateSet == null) {
  41. return null;
  42. }
  43. // Adds all candidates of the current word to the given chain and
  44. // adds it to the chain manager.
  45. for (String candidate : candidateSet) {
  46. // Chain is complete
  47. if (candidate.equals(end)) {
  48. return nextChain.addLast(candidate);
  49. } else if (!nextChain.contains(candidate)) {
  50. mgr.add(nextChain.addLast(candidate));
  51. }
  52. }
  53. nextChain = mgr.next();
  54. }
  55. return null;
  56. }
  57.  
  58. /**
  59. * Checks to see whether the given word is in the dictionary.
  60. *
  61. * @param word - word that is being searched for
  62. * @return a boolean indicating whether it is in the dictionary or not
  63. */
  64. public boolean isInDictionary(String word){
  65. return dictionary.allCandidates.containsKey(word);
  66. }
  67.  
  68. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.