Clan x86
Technical (Development, Security, etc.) => General Programming => Topic started by: iago 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:
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:
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
-
Brilliant.
-
Apparently (from vL forum) there's a better way to do this. But eh, this works :)
-
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
-
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 (http://www.javaop.com/~joe/java/Define.class)
Source (http://www.javaop.com/~joe/java/Define.java)
iAGO INSTALL PICO ON DARKSIDE
-
pico and nano suck balls.
That binary/source is really just a waste of iago's bandwidth.
-
Yeah, it's a waste of space. Learn vi or go home.
-
One day, me and all the other pico supporters, shall take over this here forum. That or be converted to.. *gasp*.. vi.
-
I bow to no man.
-
What are paco, vi, and everything else?
-
What are paco, vi, and everything else?
Text editors
-
pico*, vi (or vim) and emacs are all text editors for the command line in Linux.
-
Emacs has an (beautiful if I may say so myself) X version.