/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
/*
Copyright 2010 Joe Robinson
*/
import java.net.URL;
import java.net.URLConnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class bladictionary {
public static void main(String[] args) throws IOException
{
String query = args[0];
String[] parts = null;
//URL which provides XML response
URL url = new URL("http://services.aonaware.com/DictService/DictService.asmx/Define?word="+query);
URLConnection urlc = url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
String line;
Boolean definition = false;
Boolean definition2 = false;
Boolean wn = false;
Boolean foldoc = false;
int fcount = 0;
String result = "";
//Read every line
while ((line = br.readLine()) != null)
{
//Get rid of whitespace before/after
line = line.trim();
//only read WordNet definitions
if (line.equals("wn"))
{
wn = true;
}
else if (line.equals("foldoc")&&fcount<1)
{
//Read tech definitions too
foldoc = true;
if(result.equals(""))
{
result = result+"tech: ";
}
else
{
result = result+"; tech: ";
}
fcount++;
}
if(wn||foldoc)
{
//Look for start of a definition
if (line.contains(""))
{
definition = true;
definition2 = true;
}
//If we found a definition tag
else if (definition)
{
//if it's the end of the word definition, stop reading
if(line.contains(""))
{
definition = false;
wn = false;
foldoc = false;
}
else
{
if(wn)
{
if (line.substring(0,2).equals("n ") || line.substring(0,2).equals("v ")
|| line.substring(0,4).equals("adj ") || line.substring(0,4).equals("adv "))
{
if(!result.equals(""))
{
result = result+"; ";
}
definition2 = true;
}
if(definition2)
{
if(Character.isLetter(line.charAt(0)))
{
result = result+line+" ";
}
else
{
definition2 = false;
}
}
}
//We only want one of these definitions
else if(foldoc && fcount <=1 && !line.equals("")&&definition2)
{
//Delete bits in <>s, don't need this
line = line.replaceAll("<.*?> ","");
line = line.replaceAll(" \\/.*?\\/ ","");
//Get rid of {}s around words
line = line.replaceAll("\\{","");
line = line.replaceAll("\\}","");
//Split it at a . - We only want the first sentence
parts=line.split("\\.");
if(parts.length>1)
{
//Special case - if it's more than 1 definition
//Keep reading the first one
if(parts[0].equals("1"))
{
result = result + parts[1].substring(1,parts[1].length())+" ";
}
else
{
definition2 = false;
result = result + parts[0] + " ";
}
}
else
{
result = result + line + " ";
}
}
}
}
}
}
if(!result.equals(""))
{
result = result.replaceAll("\\(.*?[;\\)] ?","");
result = result.replaceAll("\\{.*?[;\\}] ?","");
result = result.replaceAll("\\[.*?[;\\]] ?","");
result = result.replaceAll("\\/.*?[;\\/] ?","");
result = result.split("\\[")[0];
result = result.trim()+";";
System.out.println(result);
System.exit(0);
}
else
{
System.exit(1);
}
}
}