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

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

Introduction

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

Prototype

public Set<String> getNodeNames() 

Source Link

Document

Returns the set of names for named nodes in this 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   w w w . ja v  a2  s  .com*/
                    }

                    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));
    }/*w w  w.  j a  va  2s. com*/

    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;
}