package com.linguamathematica.oa4j;
import java.net.URL;
import java.util.Set;
import static java.lang.String.format;
import static java.lang.System.out;
import static java.util.Arrays.asList;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static com.linguamathematica.oa4j.Base.newSet;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import com.linguamathematica.oa4j.Analysis.Decisiveness;
import com.linguamathematica.oa4j.Analysis.OfferingGuidance;
import com.linguamathematica.oa4j.Analysis.Polarity;
import com.linguamathematica.oa4j.Analysis.RequestingGuidance;
import com.linguamathematica.oa4j.Analysis.Temporality;
import com.linguamathematica.oa4j.DemographicsAnalysis.Age;
import com.linguamathematica.oa4j.DemographicsAnalysis.Education;
import com.linguamathematica.oa4j.DemographicsAnalysis.Gender;
import com.linguamathematica.oa4j.DemographicsAnalysis.Language;
import com.linguamathematica.oa4j.StylesAnalysis.Contrast;
import com.linguamathematica.oa4j.StylesAnalysis.Flamboyance;
import com.linguamathematica.oa4j.StylesAnalysis.Slang;
import com.linguamathematica.oa4j.TopicsAnalysis.Domain;
import com.linguamathematica.oa4j.TopicsAnalysis.Noun;
public class AnalysisServiceIntegrationTest extends AbstractAnalysisServiceTest
{
@Test
public void returns_CorrectAnalysis_GivenTextB() throws Exception
{
final TopicsAnalysis topics = topicsAnalysisForTextB();
final ActionsAnalysis actions = actionsAnalysis(action(topic("set up", 3), of(Decisiveness.LOW, 1),
of(OfferingGuidance.NOT_AT_ALL, 1), of(RequestingGuidance.NOT_AT_ALL, 1), type("create", 1),
of(Temporality.PAST, 1)));
final DemographicsAnalysis demographics = demographics(of(Age.ADULT, 0), of(Gender.NEUTRAL, 0),
of(Education.PRE_SECONDARY, 1), of(Language.ENGLISH, 20.38));
final StylesAnalysis styles = style(of(Polarity.NEGATIVE, -.54), of(Polarity.NEGATIVE, -.54),
of(Polarity.NEGATIVE, -.54), of(OfferingGuidance.NOT_AT_ALL, 1), of(RequestingGuidance.NOT_AT_ALL, 1),
of(Decisiveness.LOW, 1), of(Slang.NO_SLANG, 0), of(Flamboyance.SOMEWHAT_FLAMBOYANT, 3),
of(Contrast.NOT_AT_ALL, 0), of(Temporality.PAST, 1));
final Analysis expected = new Analysis(demographics, styles, actions, topics);
assertThat(analysisOf(loadFrom("TestText_B.txt")), is(as(expected)));
}
@Test
public void returns_CorrectAnalysis_GivenURL() throws Exception
{
final TopicsAnalysis topics = topicsAnalysisForURL();
final ActionsAnalysis actions = actionsAnalysis(//@formatter:off
action(topic("reach this web page", 3), of(Decisiveness.LOW, 1),
of(OfferingGuidance.NOT_AT_ALL, 1), of(RequestingGuidance.NOT_AT_ALL, 1), type("move", 1),of(Temporality.RECENT_PAST, 2)),
action(topic("see RFC", 3), of(Decisiveness.LOW, 1),
of(OfferingGuidance.TO_SOME_EXTENT, 2), of(RequestingGuidance.NOT_AT_ALL, 1), type("attend", 1),of(Temporality.NA, 0)));//@formatter:on
final DemographicsAnalysis demographics = demographics(of(Age.ADULT, 0), of(Gender.NEUTRAL, 0),
of(Education.SECONDARY, 2), of(Language.ENGLISH, 22.68));
final StylesAnalysis styles = style(of(Polarity.NEUTRAL, 0), of(Polarity.NEUTRAL, 0), of(Polarity.NEUTRAL, 0),
of(OfferingGuidance.TO_SOME_EXTENT, 2), of(RequestingGuidance.NOT_AT_ALL, 1), of(Decisiveness.LOW, 1),
of(Slang.NO_SLANG, 0), of(Flamboyance.SOMEWHAT_FLAMBOYANT, 3), of(Contrast.NOT_AT_ALL, 0),
of(Temporality.PRESENT, 3));
final Analysis expected = new Analysis(demographics, styles, actions, topics);
assertThat(analysisOf(new URL("http", "www.example.com", "")), is(as(expected)));
}
@Before
public void setUpEveryTime() throws Exception
{
service = new DefaultAnalysisService(key);
}
@Ignore
@Test
public void spikeToGetRealOAResponsesToAText() throws Exception
{
final HTTPService service = new HTTPService(new URL("http", "portaltnx20.openamplify.com",
"/AmplifyWeb_v21/AmplifyThis"));
for (final String testFile : asList("TestText_A.txt", "TestText_B.txt"))
{
out.println(service.query(format("apiKey=%s&analysis=all&inputText=%s&outputFormat=XML", key,
encoded(loadFrom(testFile)))));
}
}
@Ignore
@Test
public void spikeToGetRealOAResponsesToAURL() throws Exception
{
final HTTPService service = new HTTPService(new URL("http", "portaltnx20.openamplify.com",
"/AmplifyWeb_v21/AmplifyThis"));
for (final URL url : asList(new URL("http://www.google.co.uk"),
new URL("http://code.google.com/p/translate4j/")))
{
out.println(service
.query(format("apiKey=%s&analysis=all&sourceURL=%s&outputFormat=XML", key, encoded(url))));
}
}
@SuppressWarnings("unchecked")
private static TopicsAnalysis topicsAnalysisForTextB()
{
final Set<Domain> domains = newSet();
final Set<Noun> topTopics = newSet(//@formatter:off
topTopic(topic("UK", 7), of(Polarity.NEGATIVE, -.39), of(OfferingGuidance.NOT_AT_ALL, 1),
of(RequestingGuidance.NOT_AT_ALL, 1), ner("Location", "country")),
topTopic(topic("base", 7), of(Polarity.NEGATIVE, -.39), of(OfferingGuidance.NOT_AT_ALL, 1),
of(RequestingGuidance.NOT_AT_ALL, 1)),
topTopic(topic("Helmand", 4), of(Polarity.NEUTRAL, 0), of(OfferingGuidance.NOT_AT_ALL, 1),
of(RequestingGuidance.NOT_AT_ALL, 1), ner("Location", "province")),
topTopic(topic("Kandahar", 4), of(Polarity.NEGATIVE, -.2), of(OfferingGuidance.NOT_AT_ALL, 1),
of(RequestingGuidance.NOT_AT_ALL, 1), ner("Location", "city")),
topTopic(topic("province", 2), of(Polarity.NEGATIVE, -.54), of(OfferingGuidance.NOT_AT_ALL, 1),
of(RequestingGuidance.NOT_AT_ALL, 1)));//@formatter:on
final Set<Noun> properNouns = newSet(//@formatter:off
properNoun(topic("UK", 7), of(Polarity.NEGATIVE, -.39), of(OfferingGuidance.NOT_AT_ALL, 1),
of(RequestingGuidance.NOT_AT_ALL, 1), ner("Location", "country")),
properNoun(topic("Helmand", 4), of(Polarity.NEUTRAL, 0), of(OfferingGuidance.NOT_AT_ALL, 1),
of(RequestingGuidance.NOT_AT_ALL, 1), ner("Location", "province")),
properNoun(topic("Kandahar", 4), of(Polarity.NEGATIVE, -.2), of(OfferingGuidance.NOT_AT_ALL, 1),
of(RequestingGuidance.NOT_AT_ALL, 1), ner("Location", "city")));//@formatter:on
final Set<Analysis.Result<String>> locations = newSet(location("UK", 7), location("Helmand", 4),
location("Kandahar", 4));
return new TopicsAnalysis(properNouns, topTopics, domains, locations);
}
private static TopicsAnalysis topicsAnalysisForURL()
{
final Set<Domain> domains = newSet(//@formatter:off
new Domain(topic("Internet", 9), new Domain(topic("web page", 4),Domain.NULL)),
new Domain(topic("Computers", 5), new Domain(topic("domain name", 4),Domain.NULL)));
final Set<Noun> topTopics = newSet(
topTopic(topic("example",4), of(Polarity.NEUTRAL, 0), of(OfferingGuidance.NOT_AT_ALL, 1),
of(RequestingGuidance.NOT_AT_ALL, 1)),
topTopic(topic("RFC", 6), of(Polarity.NEUTRAL, 0), of(OfferingGuidance.TO_SOME_EXTENT, 2),
of(RequestingGuidance.NOT_AT_ALL, 1) ,ner("NamedEntity","unknown")),
topTopic(topic("Page",4), of(Polarity.NEUTRAL, 0), of(OfferingGuidance.NOT_AT_ALL, 1),
of(RequestingGuidance.NOT_AT_ALL, 1),ner("Person","female")),
topTopic(topic("Web", 4), of(Polarity.NEUTRAL, 0), of(OfferingGuidance.NOT_AT_ALL, 1),
of(RequestingGuidance.NOT_AT_ALL, 1)),
topTopic(topic("domain name", 4), of(Polarity.NEUTRAL, 0), of(OfferingGuidance.NOT_AT_ALL, 1),
of(RequestingGuidance.NOT_AT_ALL, 1)));
final Set<Noun> properNouns = newSet(
properNoun(topic("RFC", 6), of(Polarity.NEUTRAL, 0), of(OfferingGuidance.TO_SOME_EXTENT, 2),
of(RequestingGuidance.NOT_AT_ALL, 1) ,ner("NamedEntity","unknown")),
properNoun(topic("Page",4), of(Polarity.NEUTRAL, 0), of(OfferingGuidance.NOT_AT_ALL, 1),
of(RequestingGuidance.NOT_AT_ALL, 1),ner("Person","female")),
properNoun(topic("Web", 4), of(Polarity.NEUTRAL, 0), of(OfferingGuidance.NOT_AT_ALL, 1),
of(RequestingGuidance.NOT_AT_ALL, 1)));//@formatter:on
final Set<Analysis.Result<String>> locations = newSet();
return new TopicsAnalysis(properNouns, topTopics, domains, locations);
}
}