package com.linguamathematica.oa4j;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.Scanner;
import java.util.Set;
import static java.lang.String.format;
import static java.lang.String.valueOf;
import static java.lang.System.getProperty;
import static java.net.URLEncoder.encode;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.when;
import static com.linguamathematica.oa4j.Base.newSet;
import org.hamcrest.Matcher;
import org.junit.Ignore;
import com.linguamathematica.oa4j.Analysis.Decisiveness;
import com.linguamathematica.oa4j.Analysis.NER;
import com.linguamathematica.oa4j.Analysis.OfferingGuidance;
import com.linguamathematica.oa4j.Analysis.Polarity;
import com.linguamathematica.oa4j.Analysis.PolarityResult;
import com.linguamathematica.oa4j.Analysis.RequestingGuidance;
import com.linguamathematica.oa4j.Analysis.Result;
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;
@Ignore
public abstract class AbstractAnalysisServiceTest
{
protected static final String key = "your key here";
protected AnalysisService service;
protected HTTPService httpService;
protected ActionsAnalysis actionsAnalysisOf(final String textB2)
{
return service.analyse(textB2).getActionsAnalysis();
}
protected Analysis analysisOf(final String text)
{
return service.analyse(text);
}
protected DemographicsAnalysis demographicAnalysisOf(final String text)
{
return service.analyse(text).getDemographicAnalysis();
}
protected Set<Domain> domainsIn(final String textB2)
{
return service.analyse(textB2).getTopicsAnalysis().getDomains();
}
protected Set<Analysis.Result<String>> locationsIn(final String textB2)
{
return service.analyse(textB2).getTopicsAnalysis().getLocations();
}
protected void mockResponseFor(final String text, final String fileName) throws UnsupportedEncodingException,
FileNotFoundException
{
when(
httpService.query(format("apiKey=%s&analysis=all&inputText=%s&outputFormat=XML", key,
encode(text, "UTF-8")))).thenReturn(loadFrom(fileName));
}
protected Set<Noun> properNounsIn(final String textB2)
{
return service.analyse(textB2).getTopicsAnalysis().getProperNouns();
}
protected StylesAnalysis styleAnalysisOf(final String text)
{
return service.analyse(text).getStylesAnalysis();
}
protected Set<Noun> topTopicsIn(final String textB2)
{
return service.analyse(textB2).getTopicsAnalysis().getTopTopics();
}
protected Analysis analysisOf(final URL url)
{
return service.analyse(url);
}
protected static ActionsAnalysis.Action action(final Result<String> topic, final Result<Decisiveness> decisiveness,
final Result<OfferingGuidance> offeringGuidance, final Result<RequestingGuidance> requestingGuidance,
final Result<String> type, final Result<Temporality> temporality)
{
return new ActionsAnalysis.Action(topic, decisiveness, offeringGuidance, requestingGuidance, type,
newSet(temporality));
}
protected static ActionsAnalysis actionsAnalysis(final ActionsAnalysis.Action action,
final ActionsAnalysis.Action... otherActions)
{
return new ActionsAnalysis(newSet(action, otherActions));
}
protected static <T> Matcher<T> are(final Matcher<T> equalTo)
{
return is(equalTo);
}
protected static <T> Matcher<T> as(final T expected)
{
return equalTo(expected);
}
protected static DemographicsAnalysis demographics(final Result<Age> result, final Result<Gender> result2,
final Result<Education> result3, final Result<Language> result4)
{
return new DemographicsAnalysis(result, result2, result3, result4);
}
protected static String loadFrom(final String fileName) throws FileNotFoundException
{
final Scanner scanner = new Scanner(new File(getProperty("user.dir") + "/src/test/resources/" + fileName));
final StringBuilder builder = new StringBuilder();
while (scanner.hasNextLine())
{
builder.append(scanner.nextLine()).append(getProperty("line.separator"));
}
return builder.toString();
}
protected static Result<String> location(final String name, final double score)
{
return new Result<String>(name, score);
}
protected static String makeExceedinglyLargeText()
{
return valueOf(new char[50001]);
}
protected static Analysis.NER ner(final String type, final String subtype)
{
return new Analysis.NER(type, subtype);
}
protected static <E extends Enum<E>> Analysis.Result<E> of(final E value, final double score)
{
return new Analysis.Result<E>(value, score);
}
protected static TopicsAnalysis.Noun properNoun(final Result<String> topic, final PolarityResult polarity,
final Result<OfferingGuidance> offeringGuidance, final Result<RequestingGuidance> RequestingGuidance,
final NER ner)
{
return new Noun(topic, polarity, offeringGuidance, RequestingGuidance, ner);
}
protected static TopicsAnalysis.Noun properNoun(final Result<String> topic, final Result<Polarity> polarity,
final Result<OfferingGuidance> offeringGuidance, final Result<RequestingGuidance> RequestingGuidance,
final NER ner)
{
return new Noun(topic, new PolarityResult(polarity, polarity, polarity), offeringGuidance, RequestingGuidance,
ner);
}
protected static Analysis.Result<String> topic(final String string, final double i)
{
return new Analysis.Result<String>(string, i);
}
protected static StylesAnalysis style(final Result<Polarity> min, final Result<Polarity> mean,
final Result<Polarity> max, final Result<OfferingGuidance> guidanceOffer,
final Result<RequestingGuidance> guidanceRequest, final Result<Decisiveness> decisiveness,
final Result<Slang> slang, final Result<Flamboyance> flamboyance, final Result<Contrast> contrast,
final Result<Temporality> temporality)
{
return new StylesAnalysis(new PolarityResult(min, mean, max), guidanceOffer, guidanceRequest, decisiveness,
slang, flamboyance, contrast, newSet(temporality));
}
protected static Noun topTopic(final Result<String> result, final PolarityResult polarity,
final Result<OfferingGuidance> offeringGuidance, final Result<RequestingGuidance> requestingGuidance)
{
return properNoun(result, polarity, offeringGuidance, requestingGuidance, NER.NULL);
}
protected static Noun topTopic(final Result<String> result, final PolarityResult polarity,
final Result<OfferingGuidance> offeringGuidance, final Result<RequestingGuidance> requestingGuidance,
final NER ner)
{
return properNoun(result, polarity, offeringGuidance, requestingGuidance, ner);
}
protected static Noun topTopic(final Result<String> result, final Result<Polarity> polarity,
final Result<OfferingGuidance> offeringGuidance, final Result<RequestingGuidance> requestingGuidance)
{
return properNoun(result, polarity, offeringGuidance, requestingGuidance, NER.NULL);
}
protected static Noun topTopic(final Result<String> result, final Result<Polarity> polarity,
final Result<OfferingGuidance> offeringGuidance, final Result<RequestingGuidance> requestingGuidance,
final NER ner)
{
return properNoun(result, polarity, offeringGuidance, requestingGuidance, ner);
}
protected static Analysis.Result<String> type(final String string, final double i)
{
return topic(string, i);
}
protected static String encoded(final String string) throws UnsupportedEncodingException
{
return encode(string, "UTF-8");
}
protected static String encoded(final URL url) throws UnsupportedEncodingException
{
return encoded(url.toString());
}
protected static Noun properNoun(final Result<String> topic, final Result<Polarity> polarity, final Result<OfferingGuidance> offeringGuidance, final Result<RequestingGuidance> requestingGuidance)
{
return properNoun(topic, polarity, offeringGuidance, requestingGuidance, NER.NULL);
}
}