Alternatively, join us on Discord.

Java code to spellcheck via google. (Without google api)

Started by c4ooo, May 31, 2017, 08:44:21 pm

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

c4ooo

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)

    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

Powered by EzPortal