Example usage for edu.stanford.nlp.semgraph.semgrex SemgrexMatcher getMatch

List of usage examples for edu.stanford.nlp.semgraph.semgrex SemgrexMatcher getMatch

Introduction

In this page you can find the example usage for edu.stanford.nlp.semgraph.semgrex SemgrexMatcher getMatch.

Prototype

public abstract IndexedWord getMatch();

Source Link

Document

Get the last matching node -- that is, the node that matches the root node of the pattern.

Usage

From source file:org.wso2.extension.siddhi.gpl.execution.nlp.SemgrexPatternStreamProcessor.java

License:Open Source License

@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor,
        StreamEventCloner streamEventCloner, ComplexEventPopulater complexEventPopulater) {
    synchronized (this) {
        while (streamEventChunk.hasNext()) {
            StreamEvent streamEvent = streamEventChunk.next();

            Annotation document = pipeline
                    .process(attributeExpressionExecutors[1].execute(streamEvent).toString());

            for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class)) {
                SemanticGraph graph = sentence
                        .get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class);
                SemgrexMatcher matcher = regexPattern.matcher(graph);

                while (matcher.find()) {
                    Object[] data = new Object[attributeCount];
                    data[0] = matcher.getMatch().value();

                    for (String nodeName : matcher.getNodeNames()) {
                        if (namedElementParamPositions.containsKey(nodeName)) {
                            data[namedElementParamPositions.get(nodeName)] = matcher.getNode(nodeName) == null
                                    ? null
                                    : matcher.getNode(nodeName).word();
                        }//from   ww w .ja v  a  2 s .c o m
                    }

                    for (String relationName : matcher.getRelationNames()) {
                        if (namedElementParamPositions.containsKey(relationName)) {
                            data[namedElementParamPositions.get(relationName)] = matcher
                                    .getRelnString(relationName);
                        }
                    }
                    StreamEvent newStreamEvent = streamEventCloner.copyStreamEvent(streamEvent);
                    complexEventPopulater.populateComplexEvent(newStreamEvent, data);
                    streamEventChunk.insertBeforeCurrent(newStreamEvent);
                }
            }
            streamEventChunk.remove();
        }
    }
    nextProcessor.process(streamEventChunk);
}

From source file:org.wso2.siddhi.extension.nlp.SemgrexPatternTransformProcessor.java

License:Open Source License

@Override
protected InStream processEvent(InEvent inEvent) {
    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Event received. Regex:%s Event:%s", regexPattern.pattern(), inEvent));
    }//from www.ja va2s .c om

    Object[] inStreamData = inEvent.getData();

    Annotation document = pipeline.process((String) inEvent.getData(inStreamParamPosition));

    InListEvent transformedListEvent = new InListEvent();

    for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class)) {
        SemanticGraph graph = sentence
                .get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class);
        SemgrexMatcher matcher = regexPattern.matcher(graph);

        while (matcher.find()) {
            Object[] outStreamData = new Object[inStreamData.length + attributeCount];
            outStreamData[0] = matcher.getMatch().value();

            for (String nodeName : matcher.getNodeNames()) {
                if (namedElementParamPositions.containsKey(nodeName)) {
                    outStreamData[namedElementParamPositions.get(nodeName)] = matcher.getNode(nodeName) == null
                            ? null
                            : matcher.getNode(nodeName).word();
                }
            }

            for (String relationName : matcher.getRelationNames()) {
                if (namedElementParamPositions.containsKey(relationName)) {
                    outStreamData[namedElementParamPositions.get(relationName)] = matcher
                            .getRelnString(relationName);
                }
            }

            System.arraycopy(inStreamData, 0, outStreamData, attributeCount, inStreamData.length);
            transformedListEvent
                    .addEvent(new InEvent(inEvent.getStreamId(), System.currentTimeMillis(), outStreamData));
        }
    }

    return transformedListEvent;
}