ReqSuggest.java :  » UnTagged » ot-sims » fr » insa » lyon » ot » sims » semantique » Android Open Source

Android Open Source » UnTagged » ot sims 
ot sims » fr » insa » lyon » ot » sims » semantique » ReqSuggest.java
package fr.insa.lyon.ot.sims.semantique;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class ReqSuggest {
  
  public static String suggest(String word) {
        String cWord = word;
    String urlReq = "http://www.google.com/search?hl=fr&q=";
    try {
      urlReq += URLEncoder.encode(word, "UTF-8");
      urlReq += "&meta=";
      URL url = new URL(urlReq);
      URLConnection urlconn = url.openConnection();
      urlconn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.1; fr; rv:1.9.2) Gecko/20100115 Firefox/3.6");
      
      BufferedReader rd = new BufferedReader(new InputStreamReader(urlconn.getInputStream())); 
      String line;
      String resp = "";
      while ((line = rd.readLine()) != null) {
        resp += line;
      }
      rd.close(); 
      
      int beginIndex = resp.indexOf("class=spell>")+12;
      if (beginIndex > 11) {
        int endIndex = resp.indexOf("</a>", beginIndex);
        String respWord = resp.substring(beginIndex, endIndex);
        cWord = respWord.replaceAll("<b><i>", "").replaceAll("</i></b>", "");        
      }
    } catch (Exception e) {
      System.out.println(e);
    }
    return cWord;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.