Search in sources :

Example 1 with SentimentInformation

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();
    }
}
Also used : SentimentDependencyGraph(org.grupolys.samulan.util.SentimentDependencyGraph) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) BufferedReader(java.io.BufferedReader) SentimentInformation(org.grupolys.samulan.util.SentimentInformation) FileReader(java.io.FileReader) OutputStreamWriter(java.io.OutputStreamWriter) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Example 2 with SentimentInformation

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);
}
Also used : SentimentInformation(org.grupolys.samulan.util.SentimentInformation) ArrayList(java.util.ArrayList) OperationValue(org.grupolys.samulan.util.OperationValue)

Example 3 with SentimentInformation

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;
    }
}
Also used : SentimentDependencyNode(org.grupolys.samulan.util.SentimentDependencyNode) SentimentInformation(org.grupolys.samulan.util.SentimentInformation) ArrayList(java.util.ArrayList) Operation(org.grupolys.samulan.analyser.operation.Operation)

Example 4 with SentimentInformation

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()));
    }
}
Also used : SentimentInformation(org.grupolys.samulan.util.SentimentInformation) OperationValue(org.grupolys.samulan.util.OperationValue)

Example 5 with SentimentInformation

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);
}
Also used : SentimentInformation(org.grupolys.samulan.util.SentimentInformation) ArrayList(java.util.ArrayList) OperationValue(org.grupolys.samulan.util.OperationValue)

Aggregations

SentimentInformation (org.grupolys.samulan.util.SentimentInformation)15 ArrayList (java.util.ArrayList)10 OperationValue (org.grupolys.samulan.util.OperationValue)6 SentimentDependencyNode (org.grupolys.samulan.util.SentimentDependencyNode)4 SentimentDependencyGraph (org.grupolys.samulan.util.SentimentDependencyGraph)3 BufferedReader (java.io.BufferedReader)2 BufferedWriter (java.io.BufferedWriter)2 FileNotFoundException (java.io.FileNotFoundException)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 OutputStreamWriter (java.io.OutputStreamWriter)2 PrintWriter (java.io.PrintWriter)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Writer (java.io.Writer)2 Operation (org.grupolys.samulan.analyser.operation.Operation)2 QueuedOperationInformation (org.grupolys.samulan.util.QueuedOperationInformation)2 HashMap (java.util.HashMap)1 CoNLLReader (org.grupolys.nlputils.parser.CoNLLReader)1 DependencyNode (org.grupolys.nlputils.parser.DependencyNode)1 DefaultOperation (org.grupolys.samulan.analyser.operation.DefaultOperation)1