use of org.grupolys.samulan.util.SentimentInformation in project uuusa by aghie.
the class Samulan method analyse.
// Obtains the sentiment classification for line in a tsv file (it classifies new samples)
private static void analyse(String pathRawFiles, String encoding, Processor p, RuleBasedAnalyser rba, String pathOutput, String scale, boolean verbose, String pathToSaveParsedFile) {
String id, category, text, goldPolarity;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(pathRawFiles));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
StringBuilder sb = new StringBuilder();
String line;
Writer writer = null;
try {
if (pathOutput != null)
writer = new PrintWriter(pathOutput, encoding);
else
writer = new BufferedWriter(new OutputStreamWriter(System.out));
} catch (FileNotFoundException | UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Writer conllWriter = null;
if (pathToSaveParsedFile != null) {
try {
conllWriter = new PrintWriter(pathToSaveParsedFile, encoding);
} catch (FileNotFoundException | UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
line = br.readLine();
int conllTextId = 0;
while (line != null) {
String[] ls = line.split("\t");
text = ls[ls.length - 1];
List<SentimentDependencyGraph> sdgs = p.process(text);
// If the user has provided a path to save the graphs in a conll file we save them
if (pathToSaveParsedFile != null) {
conllTextId += 1;
conllWriter.write("### " + conllTextId + "\t" + ((ls.length > 1) ? ls[0] : "NotAvailable") + "\n");
// System.out.println("### "+conllTextId+"\t"+((ls.length > 1)? ls[0] : "NotAvailable" )+"\n");
for (SentimentDependencyGraph dg : sdgs) {
// System.out.println(dg.toConll()+"\n");
conllWriter.write(dg.toConll() + "\n");
}
}
List<SentimentInformation> sis = sdgs.stream().map((SentimentDependencyGraph dg) -> (rba.analyse(dg, (short) 0))).collect(Collectors.toList());
SentimentInformation siFinal = rba.merge(sis);
writer.write(printOutputScaled(siFinal, scale, rba.getAc().isBinaryNeutralAsNegative()) + "\t" + "\t" + text + "\n");
writer.flush();
if (verbose) {
sdgs.stream().forEach(sdg -> sdg.printLandscapeGraph((short) 0));
}
line = br.readLine();
}
br.close();
writer.close();
if (pathToSaveParsedFile != null) {
conllWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
use of org.grupolys.samulan.util.SentimentInformation in project uuusa by aghie.
the class BranchStrategy method apply.
public OperationValue apply(SentimentInformation head, List<SentimentInformation> children, Operation operation) {
List<SentimentInformation> newChildren = new ArrayList<SentimentInformation>();
SentimentInformation auxChild;
boolean branchFound = false;
for (SentimentInformation siChild : children) {
String deprel = siChild.getSentimentDependencyNode().getDeprel();
auxChild = new SentimentInformation(siChild);
if (deprel.equals(this.scope)) {
operation.updateSentiment(auxChild);
branchFound = true;
}
newChildren.add(auxChild);
}
if (!branchFound)
return null;
return new OperationValue(new SentimentInformation(head), newChildren);
}
use of org.grupolys.samulan.util.SentimentInformation in project uuusa by aghie.
the class SyntacticRuleBasedAnalyser method analyse.
/**
* It computes the SentimentInformation of sentence represented as a SentimentDependencyGraph
* @param dg: The sentence represented as a SentimentDependencyGraph
* @param address: (usually the dummy root, id=0)
* @return The SentimentInformation for the branch of dg rooted at address
*/
public SentimentInformation analyse(SentimentDependencyGraph dg, short address) {
List<Operation> operations = this.rm.getOperations(dg, address);
SentimentInformation siHead;
SentimentDependencyNode node = (SentimentDependencyNode) dg.getNode(address);
siHead = new SentimentInformation(0, node, dg, new ArrayList<QueuedOperationInformation>());
// Queing operations in node
queueNodeOperations(operations, siHead, node);
if (node.isLeaf()) {
siHead = this.calculate(siHead, new ArrayList<SentimentInformation>());
return siHead;
} else {
List<Short> children = node.getDependents();
List<SentimentInformation> siChildren = new ArrayList<SentimentInformation>();
for (Short child : children) {
SentimentInformation siChild = this.analyse(dg, (short) child);
siChildren.add(siChild);
}
siHead = this.calculate(siHead, siChildren);
return siHead;
}
}
use of org.grupolys.samulan.util.SentimentInformation in project uuusa by aghie.
the class HeadStrategy method apply.
public OperationValue apply(SentimentInformation head, List<SentimentInformation> children, Operation operation) {
SentimentInformation auxHead;
if (head.getSemanticOrientation() == 0 && !this.negateObjective) {
return null;
} else {
auxHead = new SentimentInformation(head);
operation.updateSentiment(auxHead);
return new OperationValue(auxHead, children.stream().map((SentimentInformation c) -> (new SentimentInformation(c))).collect(Collectors.toList()));
}
}
use of org.grupolys.samulan.util.SentimentInformation in project uuusa by aghie.
the class ChildrenStrategy method apply.
public OperationValue apply(SentimentInformation head, List<SentimentInformation> children, Operation operation) {
// TODO this is different from the original behaviour of the unsupervised system
List<SentimentInformation> newChildren = new ArrayList<SentimentInformation>();
SentimentInformation auxChild;
SentimentInformation auxHead = new SentimentInformation(head);
for (SentimentInformation siChild : children) {
// TODO still not doing what it is supposed to do
// copy constructor
auxChild = new SentimentInformation(siChild);
newChildren.add(auxChild);
}
operation.updateSentiment(auxHead);
return new OperationValue(auxHead, newChildren);
}
Aggregations