/** * @author Hyacinthe MENIET * Created on 25 août 07 */ package net.dotmyself.restclient.yahoosearch; import java.io.IOException; import java.io.OutputStreamWriter; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.util.List; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; /** * A class designed to wrap the Yahoo! Term Extraction service. */ public class TermExtractionMapper { private static final String baseUrl = "http://api.search.yahoo.com/ContentAnalysisService/V1/termExtraction"; private static final String appId = "YahooDemo"; //replace with your own code private static final String urlEncoding = "ISO-8859-1"; /** * Retrieves the important words and phrases in a block of text. * @param context * The block of text that the terms will be extracted from. * @param query * A query to help identify the topic of the context. * @return * List of extracted String or null if * no String could be found. * @throws JAXBException * @throws IOException */ public static List getTerms (String context, String query) throws JAXBException, IOException { // builds de query string StringBuilder sb = new StringBuilder("appid="); sb.append(URLEncoder.encode(appId, urlEncoding)); if(context != null && context.length() > 1) { sb.append("&context="); sb.append(URLEncoder.encode(context, urlEncoding)); } if(query != null && query.length() > 1) { sb.append("&query="); sb.append(URLEncoder.encode(query, urlEncoding)); } // perform a POST connection URL url = new URL(baseUrl); URLConnection conn = url.openConnection(); conn.setDoOutput(true); OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); writer.write(sb.toString()); writer.flush(); // reads the response JAXBContext jbctx = JAXBContext.newInstance("net.dotmyself.restclient.yahoosearch"); Unmarshaller unmarshaller = jbctx.createUnmarshaller(); ResultSet rs = (ResultSet) unmarshaller.unmarshal(conn.getInputStream()); return (rs != null) ? rs.getResult() : null; } }