Search in sources :

Example 1 with Operation

use of org.grupolys.samulan.analyser.operation.Operation 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 2 with Operation

use of org.grupolys.samulan.analyser.operation.Operation in project uuusa by aghie.

the class SyntacticRuleBasedAnalyser method queueNodeOperations.

private void queueNodeOperations(List<Operation> operations, SentimentInformation si, SentimentDependencyNode node) {
    for (Operation o : operations) {
        short levelToApply = o.getRule().getLevelsup();
        if (o.getOperationName().equals(Operation.DEFAULT)) {
            si.setSemanticOrientation(getSemanticOrientation(node, o));
            int isPositiveSentiment = si.getSemanticOrientation() > 0 ? 1 : 0;
            int isNegativeSentiment = si.getSemanticOrientation() < 0 ? 1 : 0;
            si.setPositiveSentiment(Math.max(Math.abs(si.getSemanticOrientation()) * (isPositiveSentiment), SentimentInformation.SENTISTRENGTH_NEUTRAL));
            si.setNegativeSentiment(Math.max(Math.abs(si.getSemanticOrientation()) * (isNegativeSentiment), SentimentInformation.SENTISTRENGTH_NEUTRAL));
        }
        si.addQueueOperation(new QueuedOperationInformation(levelToApply, o));
    }
}
Also used : QueuedOperationInformation(org.grupolys.samulan.util.QueuedOperationInformation) Operation(org.grupolys.samulan.analyser.operation.Operation)

Example 3 with Operation

use of org.grupolys.samulan.analyser.operation.Operation in project uuusa by aghie.

the class RuleManager method readRules.

public void readRules(String pathXML) {
    Operation o;
    try {
        File file = new File(pathXML);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(file);
        doc.getDocumentElement().normalize();
        // readAnalyserConfiguration(doc.getElementsByTagName("conf"));
        NodeList nodeLst = doc.getElementsByTagName(OPERATION_FIELD);
        for (int s = 0; s < nodeLst.getLength(); s++) {
            Node fstNode = nodeLst.item(s);
            if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
                Element fstElement = (Element) fstNode;
                Set<String> forms = new HashSet<String>(Arrays.asList(this.getXMLNodeValue(fstElement, FORM).split(",")));
                Set<String> postags = new HashSet<String>(Arrays.asList(this.getXMLNodeValue(fstElement, POSTAG).split(",")));
                Set<String> dependencies = new HashSet<String>(Arrays.asList(this.getXMLNodeValue(fstElement, DEPENDENCY).split(",")));
                short levelsup = new Short(this.getXMLNodeValue(fstElement, LEVELSUP));
                short priority = new Short(this.getXMLNodeValue(fstElement, PRIORITY));
                HashSet<String> validHead = new HashSet<String>(Arrays.asList(this.getXMLNodeValue(fstElement, VALID_HEAD).split(",")));
                // TODO process regex
                postags.remove("*");
                // TODO process regex
                dependencies.remove("*");
                // TODO process regex
                validHead.remove("*");
                // INTENSIFICATION RULES WITH SENTIDATA
                if (forms.contains(SENTIDATA_BOOSTER)) {
                    if (forms.size() == 1) {
                        // System.out.println("getClassValues: "+this.d.getClassValues());
                        if (this.d.getClassValues().get(Operation.WEIGHT) != null) {
                            addRules(fstElement, this.d.getClassValues().get(Operation.WEIGHT).keySet(), postags, dependencies, levelsup, priority, validHead);
                        }
                    } else {
                        System.err.println("We cannot handle ");
                    }
                } else // NEGATION RULES WITH SENTIDATA
                if (forms.contains(SENTIDATA_NEGATION)) {
                    if (forms.size() == 1) {
                        addRules(fstElement, this.d.getNegatingWords(), postags, dependencies, levelsup, priority, validHead);
                    } else {
                        System.err.println("We cannot handle this kind of rules " + forms);
                    }
                } else // OTHER RULES
                {
                    // TODO process regex
                    forms.remove("*");
                    if (forms.contains(SENTIDATA_ADVERSATIVE)) {
                        forms.addAll(this.d.getAdversativeWords());
                    }
                    // System.out.println(forms+" "+priority);
                    addRules(fstElement, forms, postags, dependencies, levelsup, priority, validHead);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) SentimentDependencyNode(org.grupolys.samulan.util.SentimentDependencyNode) Node(org.w3c.dom.Node) DependencyNode(org.maltparser.core.syntaxgraph.node.DependencyNode) Element(org.w3c.dom.Element) DefaultOperation(org.grupolys.samulan.analyser.operation.DefaultOperation) ShiftOperation(org.grupolys.samulan.analyser.operation.ShiftOperation) WeightingOperation(org.grupolys.samulan.analyser.operation.WeightingOperation) Operation(org.grupolys.samulan.analyser.operation.Operation) Document(org.w3c.dom.Document) OperationNotFoundException(org.grupolys.samulan.util.exceptions.OperationNotFoundException) OperationNotApplicableException(org.grupolys.samulan.util.exceptions.OperationNotApplicableException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) File(java.io.File) HashSet(java.util.HashSet)

Example 4 with Operation

use of org.grupolys.samulan.analyser.operation.Operation in project uuusa by aghie.

the class RuleManager method getOperations.

/**
 * @param dg
 * @param address
 * @return
 */
public List<Operation> getOperations(SentimentDependencyGraph dg, short address) {
    SentimentDependencyNode currentNode = dg.getNode(address);
    List<Operation> operations = new ArrayList<Operation>();
    for (Rule r : rules) {
        if (r.match(dg, currentNode)) {
            Operation o = r.getOperation();
            // System.out.println("currentNode: "+currentNode+" r: "+r+" o: "+o);
            if (dg.getNode(address).getSi() == null)
                dg.getNode(address).setSi(new SentimentInformation());
            if (!o.getOperationName().equals(Operation.DEFAULT)) {
                dg.getNode(address).getSi().setType(o.getOperationName());
                if (o.getStrategy() instanceof NChildrenStrategy) {
                    ((NChildrenStrategy) o.getStrategy()).setReference(address);
                }
                if (o.getStrategy() instanceof FirstSubjectiveChildrenStrategy) {
                    ((FirstSubjectiveChildrenStrategy) o.getStrategy()).setReference(address);
                }
            }
            operations.add(r.getOperation());
        }
    }
    if (operations.contains(getOperation(Operation.DEFAULT)) && operations.size() > 1)
        operations.remove(getOperation(Operation.DEFAULT));
    return operations;
}
Also used : SentimentDependencyNode(org.grupolys.samulan.util.SentimentDependencyNode) NChildrenStrategy(org.grupolys.samulan.analyser.operation.NChildrenStrategy) ArrayList(java.util.ArrayList) SentimentInformation(org.grupolys.samulan.util.SentimentInformation) DefaultOperation(org.grupolys.samulan.analyser.operation.DefaultOperation) ShiftOperation(org.grupolys.samulan.analyser.operation.ShiftOperation) WeightingOperation(org.grupolys.samulan.analyser.operation.WeightingOperation) Operation(org.grupolys.samulan.analyser.operation.Operation) Rule(org.grupolys.samulan.rule.Rule) FirstSubjectiveChildrenStrategy(org.grupolys.samulan.analyser.operation.FirstSubjectiveChildrenStrategy)

Example 5 with Operation

use of org.grupolys.samulan.analyser.operation.Operation in project uuusa by aghie.

the class RuleManager method addRules.

private void addRules(Element fstElement, Set<String> sentiForms, Set<String> postags, Set<String> dependencies, short levelsup, short priority, Set<String> validHead) {
    for (String sentiForm : sentiForms) {
        Operation o = getOperationFinal(this.getXMLNodeValue(fstElement, TYPE), sentiForm);
        if (o != null) {
            Set<String> patternForms = new HashSet<String>();
            Set<Pattern> patterns = new HashSet<Pattern>();
            if (isNonRegexForm(sentiForm)) {
                patterns = new HashSet<Pattern>(Arrays.asList(Pattern.compile(sentiForm)));
            } else {
                patternForms = new HashSet<String>(Arrays.asList(sentiForm));
            }
            Rule r = new Rule(patterns, patternForms, postags, dependencies, levelsup, priority, validHead, o);
            this.addRule(r);
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) DefaultOperation(org.grupolys.samulan.analyser.operation.DefaultOperation) ShiftOperation(org.grupolys.samulan.analyser.operation.ShiftOperation) WeightingOperation(org.grupolys.samulan.analyser.operation.WeightingOperation) Operation(org.grupolys.samulan.analyser.operation.Operation) Rule(org.grupolys.samulan.rule.Rule) HashSet(java.util.HashSet)

Aggregations

Operation (org.grupolys.samulan.analyser.operation.Operation)5 DefaultOperation (org.grupolys.samulan.analyser.operation.DefaultOperation)3 ShiftOperation (org.grupolys.samulan.analyser.operation.ShiftOperation)3 WeightingOperation (org.grupolys.samulan.analyser.operation.WeightingOperation)3 SentimentDependencyNode (org.grupolys.samulan.util.SentimentDependencyNode)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Rule (org.grupolys.samulan.rule.Rule)2 SentimentInformation (org.grupolys.samulan.util.SentimentInformation)2 File (java.io.File)1 Pattern (java.util.regex.Pattern)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 FirstSubjectiveChildrenStrategy (org.grupolys.samulan.analyser.operation.FirstSubjectiveChildrenStrategy)1 NChildrenStrategy (org.grupolys.samulan.analyser.operation.NChildrenStrategy)1 QueuedOperationInformation (org.grupolys.samulan.util.QueuedOperationInformation)1 OperationNotApplicableException (org.grupolys.samulan.util.exceptions.OperationNotApplicableException)1 OperationNotFoundException (org.grupolys.samulan.util.exceptions.OperationNotFoundException)1 DependencyNode (org.maltparser.core.syntaxgraph.node.DependencyNode)1 Document (org.w3c.dom.Document)1