Advertisement
KillianMills

WebScraperSearch.java

Nov 21st, 2015
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.19 KB | None | 0 0
  1. import org.jsoup.Jsoup;
  2. import org.jsoup.nodes.Document;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5.  
  6. public class SearchAutomated{
  7.  
  8.     // used to change the link for dynamic results
  9.     public static double[] changeLink(double first, double second){
  10.  
  11.         double temp[] = new double[2];
  12.  
  13.         first = first + 0.2 ; // increment to 2
  14.         second = second +  0.1; // increment to 1
  15.  
  16.     first = Math.round(first * 100) / 100.0;
  17.     second = Math.round(second * 100) / 100.0;
  18.  
  19.         temp[0] = first;
  20.         temp[1] = second;
  21.  
  22.         return temp;
  23.  
  24.     }
  25.  
  26.     public static void innerLoop(int trec){
  27.  
  28.         try{
  29.  
  30.             double k, b;
  31.             k = 0.0;
  32.             b = 0.0;
  33.  
  34.             for(int j=0; j <10; j++){
  35.  
  36.                 double tempArray[] = changeLink(k, b);
  37.  
  38.                 k = tempArray[0];
  39.                 b = tempArray[1];
  40.  
  41.                 Document doc = Jsoup.connect("http://136.206.115.117:8080/IRModelGenerator/res." + trec +".BM25."+ k + "." + b).timeout(60000).maxBodySize(10*1024*1024).get();
  42.                 PrintWriter out = new PrintWriter("TREC_"+ trec +"_k_" + k + "b_" + b +"_results.csv"); //problem
  43.                 String article = doc.toString();
  44.  
  45.                 int currentLine = 301;
  46.                 int innerCounter = 0;
  47.  
  48.                 String stringHolder = article.substring(32, 35);
  49.                 //System.out.println(stringHolder);
  50.                 int checker = Integer.parseInt(stringHolder);
  51.                 //System.out.println(checker);
  52.  
  53.  
  54.                 for(int i=0; i<article.length(); i++){
  55.  
  56.                     if( innerCounter == 1000){
  57.  
  58.                         currentLine++;
  59.                         innerCounter = 0;
  60.                     }
  61.  
  62.                     String stringLine = Integer.toString(currentLine);
  63.  
  64.                     if(i != 0){
  65.                         stringHolder = article.substring(0, 3);
  66.                     }
  67.  
  68.                     checker = Integer.parseInt(stringHolder);
  69.  
  70.                     if(i == 0){
  71.                         article = article.substring(article.indexOf(stringLine) + 0);
  72.                     }
  73.  
  74.                     String subArticle = article.substring(0, article.indexOf("BM25." + k + "." + b) - 1); /////
  75.                     //System.out.println(subArticle);
  76.                     out.println(subArticle);
  77.                     out.flush();
  78.  
  79.                     innerCounter++;
  80.  
  81.                     // this brings us onto the next line
  82.                     article = article.substring(article.indexOf("BM25." + k + "." + b)+ 13);////
  83.  
  84.                     System.out.println("Inner Loop done:" + i);
  85.                 }
  86.  
  87.                 out.close();
  88.  
  89.                 System.out.println("OUTER Loop done:" + j);
  90.             }
  91.  
  92.         }
  93.  
  94.         catch(IOException e)
  95.         {
  96.             System.out.println("I could not connect");
  97.         }
  98.  
  99.         System.out.println("Program has finished running");
  100.     }
  101.  
  102.     public static void main(String[]args){
  103.  
  104.  
  105.         for(int trec=6; trec<9; trec++){
  106.  
  107.             System.out.println("I am working 1");
  108.             innerLoop(trec);
  109.             System.out.println("I am working 2");
  110.         }
  111.  
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement