Example usage for edu.stanford.nlp.ling.tokensregex TokenSequencePattern compile

List of usage examples for edu.stanford.nlp.ling.tokensregex TokenSequencePattern compile

Introduction

In this page you can find the example usage for edu.stanford.nlp.ling.tokensregex TokenSequencePattern compile.

Prototype

public static TokenSequencePattern compile(SequencePattern.PatternExpr nodeSequencePattern) 

Source Link

Document

Compiles a PatternExpr into a TokenSequencePattern.

Usage

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

License:Open Source License

@Override
protected List<Attribute> init(AbstractDefinition inputDefinition,
        ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader,
        SiddhiAppContext siddhiAppContext) {
    if (logger.isDebugEnabled()) {
        logger.debug("Initializing Query ...");
    }//  w  ww  . j a  v  a2s .co m

    if (attributeExpressionLength < 2) {
        throw new SiddhiAppCreationException(
                "Query expects at least two parameters. Received only " + attributeExpressionLength
                        + ".\nUsage: #nlp.findTokensRegexPattern(regex:string, text:string-variable)");
    }

    String regex;
    try {
        if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
            regex = (String) attributeExpressionExecutors[0].execute(null);
        } else {
            throw new SiddhiAppCreationException("First parameter should be a constant."
                    + ".\nUsage: #nlp.findTokensRegexPattern(regex:string, text:string-variable)");
        }
    } catch (ClassCastException e) {
        throw new SiddhiAppCreationException("First parameter should be of type string. Found "
                + attributeExpressionExecutors[0].getReturnType()
                + ".\nUsage: #nlp.findTokensRegexPattern(regex:string, text:string-variable)");
    }

    try {
        regexPattern = TokenSequencePattern.compile(regex);
    } catch (Exception e) {
        throw new SiddhiAppCreationException("Cannot parse given regex " + regex, e);
    }

    if (!(attributeExpressionExecutors[1] instanceof VariableExpressionExecutor)) {
        throw new SiddhiAppCreationException("Second parameter should be a variable."
                + ".\nUsage: #nlp.findTokensRegexPattern(regex:string, text:string-variable)");
    }

    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Query parameters initialized. Regex: %s Stream Parameters: %s", regex,
                inputDefinition.getAttributeList()));
    }

    initPipeline();

    ArrayList<Attribute> attributes = new ArrayList<Attribute>(1);

    attributes.add(new Attribute("match", Attribute.Type.STRING));
    attributeCount = regexPattern.getTotalGroups();
    for (int i = 1; i < attributeCount; i++) {
        attributes.add(new Attribute(groupPrefix + i, Attribute.Type.STRING));
    }
    return attributes;
}

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

License:Open Source License

@Override
protected List<Attribute> init(AbstractDefinition abstractDefinition,
        ExpressionExecutor[] attributeExpressionExecutors, ExecutionPlanContext executionPlanContext) {
    if (logger.isDebugEnabled()) {
        logger.debug("Initializing Query ...");
    }/*from   www .  ja  va2  s .c o m*/

    if (attributeExpressionLength < 2) {
        throw new ExecutionPlanCreationException(
                "Query expects at least two parameters. Received only " + attributeExpressionLength
                        + ".\nUsage: #nlp.findTokensRegexPattern(regex:string, text:string-variable)");
    }

    String regex;
    try {
        if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
            regex = (String) attributeExpressionExecutors[0].execute(null);
        } else {
            throw new ExecutionPlanCreationException("First parameter should be a constant."
                    + ".\nUsage: #nlp.findTokensRegexPattern(regex:string, text:string-variable)");
        }
    } catch (ClassCastException e) {
        throw new ExecutionPlanCreationException("First parameter should be of type string. Found "
                + attributeExpressionExecutors[0].getReturnType()
                + ".\nUsage: #nlp.findTokensRegexPattern(regex:string, text:string-variable)");
    }

    try {
        regexPattern = TokenSequencePattern.compile(regex);
    } catch (Exception e) {
        throw new ExecutionPlanCreationException("Cannot parse given regex " + regex, e);
    }

    if (!(attributeExpressionExecutors[1] instanceof VariableExpressionExecutor)) {
        throw new ExecutionPlanCreationException("Second parameter should be a variable."
                + ".\nUsage: #nlp.findTokensRegexPattern(regex:string, text:string-variable)");
    }

    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Query parameters initialized. Regex: %s Stream Parameters: %s", regex,
                abstractDefinition.getAttributeList()));
    }

    initPipeline();

    ArrayList<Attribute> attributes = new ArrayList<Attribute>(1);

    attributes.add(new Attribute("match", Attribute.Type.STRING));
    attributeCount = regexPattern.getTotalGroups();
    for (int i = 1; i < attributeCount; i++) {
        attributes.add(new Attribute(groupPrefix + i, Attribute.Type.STRING));
    }
    return attributes;
}

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

License:Open Source License

@Override
protected void init(Expression[] expressions, List<ExpressionExecutor> expressionExecutors,
        StreamDefinition inStreamDefinition, StreamDefinition outStreamDefinition, String elementId,
        SiddhiContext siddhiContext) {/*from   ww w .  jav a 2  s . c  o  m*/
    if (logger.isDebugEnabled()) {
        logger.debug("Initializing Query ...");
    }

    if (expressions.length < 2) {
        throw new QueryCreationException("Query expects at least two parameters. Received only "
                + expressions.length + ".\n" + "Usage: findTokensRegexPattern(regex:string, text:string)");
    }

    String regex;
    try {
        regex = ((StringConstant) expressions[0]).getValue();
    } catch (ClassCastException e) {
        logger.error("Error in reading parameter regex", e);
        throw new QueryCreationException(
                "First parameter should be of type string. Found " + Constants.getType(expressions[0]) + ".\n"
                        + "Usage: findTokensRegexPattern(regex:string, text:string)");
    }

    try {
        regexPattern = TokenSequencePattern.compile(regex);
    } catch (Exception e) {
        logger.error("Error in parsing token regex pattern", e);
        throw new QueryCreationException(
                "Cannot parse given regex " + regex + " Error: [" + e.getMessage() + "]");
    }

    if (expressions[1] instanceof Variable) {
        inStreamParamPosition = inStreamDefinition
                .getAttributePosition(((Variable) expressions[1]).getAttributeName());
    } else {
        throw new QueryCreationException(
                "Second parameter should be a variable. Found " + Constants.getType(expressions[1]) + ".\n"
                        + "Usage: findTokensRegexPattern(regex:string, text:string)");
    }

    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Query parameters initialized. Regex: %s Stream Parameters: %s", regex,
                inStreamDefinition.getAttributeList()));
    }

    initPipeline();

    if (outStreamDefinition == null) {
        this.outStreamDefinition = new StreamDefinition().name("tokensRegexPatternMatchStream");

        this.outStreamDefinition.attribute("match", Attribute.Type.STRING);

        // Get all groups in the regular expression and add them to the output stream definition attributes
        attributeCount = regexPattern.getTotalGroups();
        for (int i = 1; i < regexPattern.getTotalGroups(); i++) {
            this.outStreamDefinition.attribute(groupPrefix + i, Attribute.Type.STRING);
        }

        for (Attribute strDef : inStreamDefinition.getAttributeList()) {
            this.outStreamDefinition.attribute(strDef.getName(), strDef.getType());
        }
    }
}