News:

Facebook killed the radio star. And by radio star, I mean the premise of distributed forums around the internet. And that got got by Instagram/SnapChat. And that got got by TikTok. Where the fuck is the internet we once knew?

Main Menu

Define a word -- Java

Started by iago, April 13, 2005, 01:24:34 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

iago

I'm using this in a JavaOp2 plugin, but it's pretty good on its own, in my opinion:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Vector;

/*
* Created on Apr 12, 2005
* By iago
*/

public class Define
{
    /** Get the list of definitions for the word */
    public static String []define(String word) throws IOException
    {
        word = word.replaceAll(" ", "+");
       
        Vector ret = new Vector();
        String text = getPage(word);
       
        boolean stop = false;
        while(stop == false)
        {
            int ddIndex = text.indexOf("<DD>");
            int liIndex = text.indexOf("<LI>");
           
            if(liIndex >= 0 && (ddIndex >= 0 ? liIndex < ddIndex : true))
            {
                text = text.substring(liIndex + 4);
                String def = text.replaceAll("<\\/LI>.*", "");
                ret.add(def.replaceAll("<.*?>", "").trim());
                text = text.substring(text.indexOf("</LI>") + 5);
            }
            else if(ddIndex >= 0 && (liIndex >= 0 ? ddIndex < liIndex : true))
            {
                text = text.substring(ddIndex + 4);
                String def = text.replaceAll("<\\/DD>.*", "");
                ret.add(def.replaceAll("<.*?>", "").trim());
                text = text.substring(text.indexOf("</DD>") + 5);
            }
            else
            {
                stop = true;
            }
        }
       
        return (String []) ret.toArray(new String[ret.size()]);
    }
   
    private static String getPage(String word) throws IOException
    {
        HttpURLConnection conn = (HttpURLConnection) new URL("http", "dictionary.reference.com", 80, "/search?q=" + word).openConnection();

        conn.connect();

        if (conn.getResponseCode() != HttpURLConnection.HTTP_OK)
        {
            conn.disconnect();
            throw new IOException("Unable to find the result: Server returned error " + conn.getResponseCode());
        }
       
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        StringBuffer fullTextBuf = new StringBuffer();
        String line;
        while((line = in.readLine()) != null)
        {
            fullTextBuf.append(line);
        }
       
        conn.disconnect();
       
        return fullTextBuf.toString();
    }
   
    public static void main(String []args) throws IOException
    {
        while(true)
        {
            System.out.print("Please enter a word to define --> ");
            String word = new BufferedReader(new InputStreamReader(System.in)).readLine();
            String []defs = define(word);
           
            System.out.println(defs.length + " definitions found");
            for(int i = 0; i < defs.length; i++)
                System.out.println((i + 1) + ": " + defs[i]);
        }
    }
}


Here is it being used:
Quote
Please enter a word to define --> donkey
3 definitions found
1: The domesticated ass (Equus asinus).
2: Slang. An obstinate person.
3: Slang. A stupid person.
Please enter a word to define --> squint
13 definitions found
1: To look with the eyes partly closed, as in bright sunlight.
2: To look or glance sideways.
3: To have an indirect reference or inclination.
4: To be affected with strabismus.
5: To cause to squint.
6: To close (the eyes) partly while looking.
7: The act or an instance of squinting.
8: A sideways glance.
9: An oblique reference or inclination.
10: See strabismus.
11: A hagioscope.
12: Looking obliquely or askance.
13: Squint-eyed.
Please enter a word to define --> mooga
0 definitions found

Quik

Quote[20:21:13] xar: i was just thinking about the time iago came over here and we made this huge bomb and light up the sky for 6 min
[20:21:15] xar: that was funny

iago

Apparently (from vL forum) there's a better way to do this.  But eh, this works :)

Joe

VB port.

'Created on Apr 12, 2005 by iago
'Ported to VBS Apr 13, 2005 by JoeTheOdd

'Get the list of definitions for the word
Function Define(Word as String) as String
    Dim Ret as String, Text as String, Stop as Boolean, Def as String
    Word = Replace(Word, " ", "+")
    Text = GetPage(Word)
       
    Stop = False
       
    Dim ddIndex as Integer, liIndex as Integer
    While Stop = False
        ddIndex = InStr(1, Text, "<DD>" 'int ddIndex = text.indexOf("<DD>");
        liIndex = InStr(1, Text, "<LI>" 'int liIndex = text.indexOf("<LI>");
       
        If liIndex >= 0 And (ddIndex >= 0 Or liIndex < ddIndex) 'if(liIndex >= 0 && (ddIndex >= 0 ? liIndex < ddIndex : true))
            Text = Mid(Text, liIndex + 4)                       'text = text.substring(liIndex + 4);

            'Not sure how to port this. Someone wanna look into it? --> String def = text.replaceAll("<\\/LI>.*", ""); 
            def = text      'Temporary implementation until correct one is made.

            'Not sure how to port this. Someone wanna look into it? --> ret.add(def.replaceAll("<.*?>", "").trim());
            ret = ret & def 'Temporary implementation until correct one is made.

            Ret = Ret & Mid(Text, InStr(1, Text, "</LI>") + 5)
        ElseIf ddIndex > 0 And liIndex >= 0 Or ddIndex < liIndex 'else if(ddIndex >= 0 && (liIndex >= 0 ? ddIndex < liIndex : true))
            Text = Mid(Text, InStr(1, Text, ddIndex + 4) 'text = text.substring(ddIndex + 4);
            'Not sure how to port this. Someone wanna look into it? --> ef = text.replaceAll("<\\/DD>.*", "");
            ef = text        'Temporary implmentation until correct one is made.

            'Not sure how to port this. Someone wanna look into it? --> ret.add(def.replaceAll("<.*?>", "").trim());
            ret = ret & def  'Temporary implementation until correct one is made.

            Text = Mid(Text, InStr(1, Text, "</DD>") + 5) 'text = text.substring(text.indexOf("</DD>") + 5);
        Else
            Stop = True
        End If
    Wend

    Define = Ret 'return (String []) ret.toArray(new String[ret.size()]);
End Function
   
Function GetPage(Word as String)
    GetPage = scInet.OpenURL("http://dictionary.com/search?q=" & word)
End Function
Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.


Joe

After an hour or so of juggling files arround using my own HTTP server as well as SFTP all arround North America, I finally have the source code ready for download and I have it compiled for the lazy ones.

Binary
Source

iAGO INSTALL PICO ON DARKSIDE
Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.


Newby

pico and nano suck balls.

That binary/source is really just a waste of iago's bandwidth.
- Newby
http://www.x86labs.org

Quote[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

Quote from: Rule on June 30, 2008, 01:13:20 PM
Quote from: CrAz3D on June 30, 2008, 10:38:22 AM
I'd bet that you're currently bloated like a water ballon on a hot summer's day.

That analogy doesn't even make sense.  Why would a water balloon be especially bloated on a hot summer's day? For your sake, I hope there wasn't too much logic testing on your LSAT. 

iago

Yeah, it's a waste of space.  Learn vi or go home.

Krazed

One day, me and all the other pico supporters, shall take over this here forum. That or be converted to.. *gasp*.. vi.
It is good to be good, but it is better to be lucky.

Mythix

Philosophy, n. A route of many roads leading from nowhere to nothing.

- Ambrose Bierce


deadly7

What are paco, vi, and everything else?
[17:42:21.609] <Ergot> Kutsuju you're girlfrieds pussy must be a 403 error for you
[17:42:25.585] <Ergot> FORBIDDEN

on IRC playing T&T++
<iago> He is unarmed
<Hitmen> he has no arms?!

on AIM with a drunk mythix:
(00:50:05) Mythix: Deadly
(00:50:11) Mythix: I'm going to fuck that red dot out of your head.
(00:50:15) Mythix: with my nine

Tuberload

I am prepared to be ridiculed for what I believe, are you?

Quik

pico*, vi (or vim) and emacs are all text editors for the command line in Linux.
Quote[20:21:13] xar: i was just thinking about the time iago came over here and we made this huge bomb and light up the sky for 6 min
[20:21:15] xar: that was funny

mynameistmp

Emacs has an (beautiful if I may say so myself) X version.