* WalrusIRC

You need to have 5 posts and not be part of restricted usergroups in order to use the WalrusIRC embedded shoutbox. However, you can also access our IRC channel called #CodeWalrus via EFnet.

Author Topic: Java code to spellcheck via google. (Without google api)  (Read 326 times)

0 Members and 1 Guest are viewing this topic.

Offline c4ooo

  • Super User
  • CW Contest II Winner
  • *
  • Join Date: Aug 2015
  • Location: 127.0.0.1
  • Posts: 1012
  • Post Rating Ratio: +12/-4
  • Advanced forumer
  • Gender: Male
Java code to spellcheck via google. (Without google api)
« on: May 31, 2017, 08:44:21 pm »
This spell spellchecks a quarry by using the "showing results for" and "did you mean" parts of a google result. (Not well tested with the "//try using "Did you mean" breach)
Code: [Select]
    String spellcheck(String querry) {
        String html = "";
        try {
            String url = "http://www.google.com/search?q=";
            String charset = "UTF-8";
            String query = String.format("%s", URLEncoder.encode(querry, charset));
            URLConnection con = new URL(url + query).openConnection();
            con.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                html += inputLine;
            }
            in.close();
        } catch (Exception e) {
            System.out.println("Cant fetch page.");
            e.printStackTrace();
        }
        try { //try using "Showing results for"
            String span = "<span class=\"spell\">Showing results for</span>";
            String s = html.substring(html.indexOf(span) + span.length());
            s = s.substring(s.indexOf("<b>"), s.indexOf("</a>"));
            s = s.replaceAll("<b>", "").replaceAll("</b>", "").replaceAll("<i>", "").replaceAll("</i>", "");
            return s;
        } catch (Exception e) {
            try { //try using "Did you mean" (not well tested)
                String span = "<span class=\"spell ng\" style=\"margin-bottom:7px\">Did you mean:</span>";
                String s = html.substring(html.indexOf(span) + span.length());
                s = s.substring(s.indexOf("<b>"), s.indexOf("</a>"));
                s = s.replaceAll("<b>", "").replaceAll("</b>", "").replaceAll("<i>", "").replaceAll("</i>", "");
                return s;
            } catch (Exception e2) { //rip
                System.out.println(html);
                return "An error acured. This could happen if google thinks you spelled every thing correctly, or if you spelled stuff so bad even it cant understand :/";
            }
        }
    }
@DarkestEx you did want java right? lol



 


You can also use the following HTML or bulletin board code to share it on your page or forum signature!


Also do not forget to check our affiliates below.
Planet Casio TI-Planet Calc.news BroniesQC BosaikNet Velocity Games