Author Topic: Define a word -- Java  (Read 7503 times)

0 Members and 1 Guest are viewing this topic.

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Define a word -- Java
« on: April 13, 2005, 01:24:34 pm »
I'm using this in a JavaOp2 plugin, but it's pretty good on its own, in my opinion:

Code: [Select]
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

Offline Quik

  • Webmaster Guy
  • x86
  • Hero Member
  • *****
  • Posts: 3262
  • \x51 \x75 \x69 \x6B \x5B \x78 \x38 \x36 \x5D
    • View Profile
Re: Define a word -- Java
« Reply #1 on: April 13, 2005, 06:52:56 pm »
Brilliant.
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

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Define a word -- Java
« Reply #2 on: April 13, 2005, 07:01:21 pm »
Apparently (from vL forum) there's a better way to do this.  But eh, this works :)

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: Define a word -- Java
« Reply #3 on: April 13, 2005, 07:33:43 pm »
VB port.

Code: [Select]
'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
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: Define a word -- Java
« Reply #4 on: June 07, 2005, 02:22:19 am »
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
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Newby

  • Moderator
  • Hero Member
  • *****
  • Posts: 10877
  • Thrash!
    • View Profile
Re: Define a word -- Java
« Reply #5 on: June 07, 2005, 02:28:31 am »
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

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. 

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Define a word -- Java
« Reply #6 on: June 07, 2005, 09:43:37 am »
Yeah, it's a waste of space.  Learn vi or go home.

Offline Krazed

  • x86
  • Hero Member
  • *****
  • Posts: 1822
    • View Profile
Re: Define a word -- Java
« Reply #7 on: June 08, 2005, 09:02:46 am »
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.

Offline Mythix

  • The Dude
  • x86
  • Hero Member
  • *****
  • Posts: 1569
  • Victory
    • View Profile
    • Dark-Wire
Re: Define a word -- Java
« Reply #8 on: June 08, 2005, 11:35:28 am »
I bow to no man.
Philosophy, n. A route of many roads leading from nowhere to nothing.

- Ambrose Bierce


Offline deadly7

  • 42
  • x86
  • Hero Member
  • *****
  • Posts: 6496
    • View Profile
Re: Define a word -- Java
« Reply #9 on: June 08, 2005, 08:19:06 pm »
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

Offline Tuberload

  • Neophyte
  • x86
  • Hero Member
  • *****
  • Posts: 530
    • View Profile
Re: Define a word -- Java
« Reply #10 on: June 08, 2005, 09:26:50 pm »
What are paco, vi, and everything else?

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

Offline Quik

  • Webmaster Guy
  • x86
  • Hero Member
  • *****
  • Posts: 3262
  • \x51 \x75 \x69 \x6B \x5B \x78 \x38 \x36 \x5D
    • View Profile
Re: Define a word -- Java
« Reply #11 on: June 08, 2005, 09:49:52 pm »
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

Offline mynameistmp

  • Full Member
  • ***
  • Posts: 111
  • Hi! I'm new here!
    • View Profile
Re: Define a word -- Java
« Reply #12 on: June 14, 2005, 02:47:53 am »
Emacs has an (beautiful if I may say so myself) X version.