Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package se.kth.id1020.tinyserachengine;
  7.  
  8.  
  9. /**
  10. *
  11. * @author stevenzhou
  12. */
  13. //import static se.kth.id1020.tinyserachengine.TinySearchEngine.binarysearch;
  14. import se.kth.id1020.TinySearchEngineBase;
  15. import java.util.ArrayList;
  16. /*Array & ArrayList have the same performance , but Array use fixed size and
  17. ArrayList have variable length. therefore ArrayList is a better option
  18. Note: Need to use objects instead of premitive data types */
  19. import java.util.List;
  20. import se.kth.id1020.Driver;
  21. //import java.util.jar.Attributes;
  22. import se.kth.id1020.util.Document;
  23. import se.kth.id1020.util.Word;
  24. import se.kth.id1020.util.Attributes;
  25.  
  26.  
  27.  
  28.  
  29.  
  30. public class TinySearchEngine implements TinySearchEngineBase{
  31. //Create an ArrayList
  32. //use string for now
  33. ArrayList<wordAndAttributes> dataBank = new ArrayList<>();
  34.  
  35.  
  36. class wordAndAttributes implements Comparable <wordAndAttributes>{
  37. String word;
  38. // can use both word and string above
  39. ArrayList<Attributes> DocList = new ArrayList<>();
  40.  
  41. public wordAndAttributes(String word, Attributes emil){
  42. this.word = word;
  43. DocList.add(emil);
  44.  
  45. }
  46. public void addAttributes(Attributes emil){
  47. //do a binary search if it is possible
  48. DocList.add(emil);
  49. }
  50.  
  51. @Override
  52. public int compareTo(wordAndAttributes o) {
  53.  
  54. String a =new String(this.word);
  55. String b =new String (o.word);
  56. return a.compareTo(b);
  57. }
  58.  
  59. }
  60.  
  61.  
  62. @Override
  63. public void insert(Word word, Attributes attr) {
  64. //throw new UnsupportedOperationException("Not supported yet.");
  65. //Need to start to build object first.
  66. wordAndAttributes datatempElement = new wordAndAttributes(word.word,attr);
  67. int position = binarysearch( datatempElement, dataBank);
  68. if(dataBank.isEmpty())
  69. dataBank.add(datatempElement);
  70. else{
  71. if(position<0){ // Element dont exists, also check if need to -1 for position(below)
  72. dataBank.add(-1*position-1, new wordAndAttributes(word.word, attr));
  73. }else{ // add the file info
  74. dataBank.get(position).addAttributes(datatempElement.DocList.get(0));
  75. }
  76. }
  77.  
  78.  
  79. }
  80.  
  81.  
  82. @Override
  83. public List<Document> search(String query) {
  84. // throw new UnsupportedOperationException("Not supported yet.");
  85. ArrayList<Document> doc = new ArrayList <>();
  86. int index;
  87. index = binarysearch(query, dataBank);
  88. if(index>0){
  89. //int i= dataBank.get(index).DocList.size();
  90. for(int i=0; i<dataBank.get(index).DocList.size(); i++){
  91. doc.add(dataBank.get(index).DocList.get(i).document);
  92. i--;
  93. }
  94. return doc;
  95. }
  96. System.out.println("Btter luck next time!");
  97. return null ;
  98. }
  99.  
  100.  
  101.  
  102. // Binary Search Code from Lab 1
  103. // return the index of the key in the sorted array a[]; -1 if not found
  104. public static int binarysearch(Comparable key, ArrayList a) {
  105.  
  106. return binarysearch(key, a, 0, a.size()-1);
  107. }
  108. public static int binarysearch(Comparable key, ArrayList<Comparable> a, int lo, int hi) {
  109. // possible key indices in [lo, hi)
  110. if (hi <= lo) return -(lo+1); // miss, word is not in the list
  111. int mid = lo + (hi - lo) / 2;
  112. int cmp = a.get(mid).compareTo(key);
  113. if (cmp > 0) return binarysearch(key, a, lo, mid);
  114. else if (cmp < 0) return binarysearch(key, a, mid + 1, hi);
  115. else return mid;
  116. }
  117.  
  118.  
  119. public static void main(String[] args) throws Exception {
  120. TinySearchEngineBase searchEngine = new TinySearchEngine();
  121. Driver.run(searchEngine);
  122. }
  123.  
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement