Last active
July 13, 2017 10:21
-
-
Save cogmission/65e21203465a0aa4bb1e17dfd8deba59 to your computer and use it in GitHub Desktop.
Cortical.io Retina Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.stream.Collectors; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import io.cortical.retina.client.FullClient; | |
import io.cortical.retina.model.ExpressionFactory; | |
import io.cortical.retina.model.ExpressionFactory.ExpressionModel; | |
import io.cortical.retina.model.Fingerprint; | |
import io.cortical.retina.model.Metric; | |
import io.cortical.retina.model.Model; | |
import io.cortical.retina.model.Term; | |
import io.cortical.retina.model.Text; | |
import io.cortical.retina.rest.ApiException; | |
/** | |
* Example class which exercises the Cortical.io Retina API | |
* | |
* This is a very simple example to get "first-timers" off the ground. | |
* | |
* @author cogmission | |
*/ | |
public class TextExample { | |
public static final String ENGLISH_SERVER = "http://api.cortical.io/rest"; | |
public static final String ENGLISH_RETINA = "en_associative"; | |
private FullClient client; | |
/** | |
* Constructs a new {@code TextExample} | |
* @param apiKey | |
*/ | |
public TextExample(String apiKey) { | |
client = new FullClient(apiKey, ENGLISH_SERVER, ENGLISH_RETINA); | |
} | |
/** | |
* Returns a {@link Fingerprint} of the specified String | |
* @param s the String | |
* @return a Fingerprint | |
*/ | |
public Fingerprint getFingerprintForTerm(String s) { | |
Fingerprint retVal = null; | |
try { | |
Model term = new Term(s); | |
retVal = client.getFingerprintForExpression(term); | |
} catch(ApiException a) { | |
} catch(JsonProcessingException j) { | |
} | |
return retVal; | |
} | |
/** | |
* Returns a {@link Fingerprint} of the specified 2 terms which are combined | |
* into a "subtraction" expression (Subtract "Fruit" from "Apple" to get computer context). | |
* | |
* @param term1 the first term String | |
* @param term2 the second term String | |
* @return a Fingerprint | |
*/ | |
public Fingerprint getFingerprintForExpression(String term1, String term2) { | |
Fingerprint retVal = null; | |
try { | |
Model[] terms = new Model[] { new Term(term1), new Term(term2) }; | |
ExpressionModel expression = ExpressionFactory.sub(terms); | |
retVal = client.getFingerprintForExpression(expression); | |
} catch(ApiException a) { | |
} catch(JsonProcessingException j) { | |
} | |
return retVal; | |
} | |
/** | |
* Returns a {@link Fingerprint} of the specified String which is a | |
* body of text. | |
* | |
* @param s the String | |
* @return a Fingerprint | |
*/ | |
public Fingerprint getFingerprintForText(String text) { | |
Fingerprint retVal = null; | |
try { | |
retVal = client.getFingerprintForText(text); | |
} catch(ApiException a) { | |
} | |
return retVal; | |
} | |
/** | |
* We show a comparison between text and a term here, but it could be between | |
* 2 texts, or 2 terms or an Expression. | |
* | |
* @param text | |
* @param term | |
* @return | |
*/ | |
public Metric getMetricForComparison(String text, String term) { | |
Metric retVal = null; | |
try { | |
retVal = client.compare(new Text(text), new Term(term)); | |
} catch(ApiException a) { | |
} catch(JsonProcessingException j) { | |
} | |
return retVal; | |
} | |
/** | |
* We show a comparison between text and a term here, but it could be between | |
* 2 texts, or 2 terms or an Expression. | |
* | |
* @param text | |
* @param term | |
* @return | |
*/ | |
public Metric getMetricForModelComparison(Model model1, Model model2) { | |
Metric retVal = null; | |
try { | |
retVal = client.compare(model1, model2); | |
} catch(ApiException a) { | |
} catch(JsonProcessingException j) { | |
} | |
return retVal; | |
} | |
/** | |
* Main Entry point. | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
if(args.length < 1) { | |
System.out.println("USAGE: java -classpath </path/to/your/retina/jar/file> TextExample <API KEY>"); | |
System.exit(1); | |
} | |
String apiKey = args[0]; | |
TextExample example = new TextExample(apiKey); | |
////////////////// | |
// Term // | |
////////////////// | |
String term1 = "Apple"; | |
Fingerprint fp1 = example.getFingerprintForTerm(term1); | |
System.out.println(Arrays.stream(fp1.getPositions()).boxed().collect(Collectors.toList()) + "\n"); | |
////////////////// | |
// Expression // | |
////////////////// | |
String term2 = "Fruit"; | |
Fingerprint fp2 = example.getFingerprintForExpression(term1, term2); | |
System.out.println(Arrays.stream(fp2.getPositions()).boxed().collect(Collectors.toList()) + "\n"); | |
////////////////// | |
// Text // | |
////////////////// | |
String text = | |
"Apple is an American multinational technology company headquartered in Cupertino, California " + | |
"that designs, develops, and sells consumer electronics, computer software, and online services. " + | |
"The company's hardware products include the iPhone smartphone, the iPad tablet computer, the Mac " + | |
"personal computer, the iPod portable media player, the Apple smartwatch, and the Apple TV digital media player."; | |
Fingerprint fp3 = example.getFingerprintForText(text); | |
System.out.println(Arrays.stream(fp3.getPositions()).boxed().collect(Collectors.toList()) + "\n"); | |
////////////////// | |
// Compare // | |
////////////////// | |
Metric metric1 = example.getMetricForComparison(text, term1); | |
System.out.println("Euclidean Distance = " + metric1.getEuclideanDistance()); | |
ExpressionModel expression = ExpressionFactory.sub(new Term(term1), new Term(term2)); | |
Metric metric2 = example.getMetricForModelComparison(new Text(text), expression); | |
System.out.println("Euclidean Distance = " + metric2.getEuclideanDistance()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment