src/preprocess/TermExtractor.java
/* 
	 * Extract terms using alchemy API service
	 */
Extract terms using alchemy API service
Extract terms using alchemy API service
private JSONArray alchemyTermExtractor(String tweet, int extractionType, boolean onlyText) throws Exception {
		HttpURLConnection connection;
		String keywordUrl = "http://access.alchemyapi.com/calls/text/TextGetRankedKeywords";
		String conceptUrl = "http://access.alchemyapi.com/calls/text/TextGetRankedConcepts";
		URL url;
		if (extractionType == CONCEPT_TAGGING) {
		url = new URL(conceptUrl);
		} else {
			url = new URL(keywordUrl);
		}
		connection = (HttpURLConnection) url.openConnection();
		connection.setDoOutput(true);
		connection.setDoInput(true);
		connection.setInstanceFollowRedirects(false);
		connection.setAllowUserInteraction(true);
		 String data = URLEncoder.encode("apikey", "UTF-8") + "=" + URLEncoder.encode(Constants.ALCHEMY_API_KEY, "UTF-8");
		    data += "&" + URLEncoder.encode("text", "UTF-8") + "=" + URLEncoder.encode(tweet, "UTF-8");
		    data += "&" + URLEncoder.encode("outputMode", "UTF-8") + "=" + URLEncoder.encode("json", "UTF-8");
		    if (extractionType == CONCEPT_TAGGING) {
		    	data += "&" + URLEncoder.encode("maxRetrieve", "UTF-8") + "=" + URLEncoder.encode("12", "UTF-8");
		    }
		    OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
		    wr.write(data);
		    wr.flush();
		    BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		    String line;
		    String jsonReponse = "";
		    while ((line = rd.readLine()) != null) {
		    	jsonReponse += line;
		    }
		    JSONObject obj = null;
		    JSONArray terms = null;
		    try{
		    obj = new JSONObject(jsonReponse);
		    if (extractionType == CONCEPT_TAGGING)
		    	terms = (JSONArray) obj.get("concepts");
		    else
		    	terms = (JSONArray) obj.get("keywords");
		    } catch(Exception e) {
		    	System.err.println(e.getMessage());
		    	System.out.println("skipping...");
		    	wr.close();
				rd.close();
		    	return new JSONArray();
		    }
		    if (onlyText) {
			    JSONArray ret = new JSONArray();
			    for (int i = 0; i < terms.length(); i++) {
					ret.put(((JSONObject)terms.get(i)).get("text"));
				}
			    terms = ret;
		    }
		    wr.close();
			rd.close();
		    return terms;
	}
private JSONArray alchemyTermExtractor ( String tweet , int extractionType , boolean onlyText ) throws Exception { HttpURLConnection connection ; String keywordUrl = "http://access.alchemyapi.com/calls/text/TextGetRankedKeywords" ; String conceptUrl = "http://access.alchemyapi.com/calls/text/TextGetRankedConcepts" ; URL url ; if ( extractionType == CONCEPT_TAGGING ) { url = new URL ( conceptUrl ) ; } else { url = new URL ( keywordUrl ) ; } connection = ( HttpURLConnection ) url . openConnection ( ) ; connection . setDoOutput ( true ) ; connection . setDoInput ( true ) ; connection . setInstanceFollowRedirects ( false ) ; connection . setAllowUserInteraction ( true ) ; String data = URLEncoder . encode ( "apikey" , "UTF-8" ) + "=" + URLEncoder . encode ( Constants . ALCHEMY_API_KEY , "UTF-8" ) ; data += "&" + URLEncoder . encode ( "text" , "UTF-8" ) + "=" + URLEncoder . encode ( tweet , "UTF-8" ) ; data += "&" + URLEncoder . encode ( "outputMode" , "UTF-8" ) + "=" + URLEncoder . encode ( "json" , "UTF-8" ) ; if ( extractionType == CONCEPT_TAGGING ) { data += "&" + URLEncoder . encode ( "maxRetrieve" , "UTF-8" ) + "=" + URLEncoder . encode ( "12" , "UTF-8" ) ; } OutputStreamWriter wr = new OutputStreamWriter ( connection . getOutputStream ( ) ) ; wr . write ( data ) ; wr . flush ( ) ; BufferedReader rd = new BufferedReader ( new InputStreamReader ( connection . getInputStream ( ) ) ) ; String line ; String jsonReponse = "" ; while ( ( line = rd . readLine ( ) ) != null ) { jsonReponse += line ; } JSONObject obj = null ; JSONArray terms = null ; try { obj = new JSONObject ( jsonReponse ) ; if ( extractionType == CONCEPT_TAGGING ) terms = ( JSONArray ) obj . get ( "concepts" ) ; else terms = ( JSONArray ) obj . get ( "keywords" ) ; } catch ( Exception e ) { System . err . println ( e . getMessage ( ) ) ; System . out . println ( "skipping..." ) ; wr . close ( ) ; rd . close ( ) ; return new JSONArray ( ) ; } if ( onlyText ) { JSONArray ret = new JSONArray ( ) ; for ( int i = 0 ; i < terms . length ( ) ; i ++ ) { ret . put ( ( ( JSONObject ) terms . get ( i ) ) . get ( "text" ) ) ; } terms = ret ; } wr . close ( ) ; rd . close ( ) ; return terms ; }
Extract terms using alchemy API service
Extract terms using alchemy API service
// else do term extraction
 // Get the response
 // Process line...
 //ps.print(line);
 // for skipping relevance
 //terms.join(",");
