Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package se.kth.id1020.tinyserachengine;
- /**
- *
- * @author stevenzhou
- */
- //import static se.kth.id1020.tinyserachengine.TinySearchEngine.binarysearch;
- import se.kth.id1020.TinySearchEngineBase;
- import java.util.ArrayList;
- /*Array & ArrayList have the same performance , but Array use fixed size and
- ArrayList have variable length. therefore ArrayList is a better option
- Note: Need to use objects instead of premitive data types */
- import java.util.List;
- import se.kth.id1020.Driver;
- //import java.util.jar.Attributes;
- import se.kth.id1020.util.Document;
- import se.kth.id1020.util.Word;
- import se.kth.id1020.util.Attributes;
- public class TinySearchEngine implements TinySearchEngineBase{
- //Create an ArrayList
- //use string for now
- ArrayList<wordAndAttributes> dataBank = new ArrayList<>();
- class wordAndAttributes implements Comparable <wordAndAttributes>{
- String word;
- // can use both word and string above
- ArrayList<Attributes> DocList = new ArrayList<>();
- public wordAndAttributes(String word, Attributes emil){
- this.word = word;
- DocList.add(emil);
- }
- public void addAttributes(Attributes emil){
- //do a binary search if it is possible
- DocList.add(emil);
- }
- @Override
- public int compareTo(wordAndAttributes o) {
- String a =new String(this.word);
- String b =new String (o.word);
- return a.compareTo(b);
- }
- }
- @Override
- public void insert(Word word, Attributes attr) {
- //throw new UnsupportedOperationException("Not supported yet.");
- //Need to start to build object first.
- wordAndAttributes datatempElement = new wordAndAttributes(word.word,attr);
- int position = binarysearch( datatempElement, dataBank);
- if(dataBank.isEmpty())
- dataBank.add(datatempElement);
- else{
- if(position<0){ // Element dont exists, also check if need to -1 for position(below)
- dataBank.add(-1*position-1, new wordAndAttributes(word.word, attr));
- }else{ // add the file info
- dataBank.get(position).addAttributes(datatempElement.DocList.get(0));
- }
- }
- }
- @Override
- public List<Document> search(String query) {
- // throw new UnsupportedOperationException("Not supported yet.");
- ArrayList<Document> doc = new ArrayList <>();
- int index;
- index = binarysearch(query, dataBank);
- if(index>0){
- //int i= dataBank.get(index).DocList.size();
- for(int i=0; i<dataBank.get(index).DocList.size(); i++){
- doc.add(dataBank.get(index).DocList.get(i).document);
- i--;
- }
- return doc;
- }
- System.out.println("Btter luck next time!");
- return null ;
- }
- // Binary Search Code from Lab 1
- // return the index of the key in the sorted array a[]; -1 if not found
- public static int binarysearch(Comparable key, ArrayList a) {
- return binarysearch(key, a, 0, a.size()-1);
- }
- public static int binarysearch(Comparable key, ArrayList<Comparable> a, int lo, int hi) {
- // possible key indices in [lo, hi)
- if (hi <= lo) return -(lo+1); // miss, word is not in the list
- int mid = lo + (hi - lo) / 2;
- int cmp = a.get(mid).compareTo(key);
- if (cmp > 0) return binarysearch(key, a, lo, mid);
- else if (cmp < 0) return binarysearch(key, a, mid + 1, hi);
- else return mid;
- }
- public static void main(String[] args) throws Exception {
- TinySearchEngineBase searchEngine = new TinySearchEngine();
- Driver.run(searchEngine);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement