public static Env getNewEnv() { Env env = new Env(new TokenSequenceParser()); env.initDefaultBindings(); return env; }
env.setDefaultResultsAnnotationExtractor(TimeExpression.TimeExpressionConverter); env.setDefaultTokensAnnotationKey(CoreAnnotations.NumerizedTokensAnnotation.class); env.setDefaultResultAnnotationKey(TimeExpression.Annotation.class); env.setDefaultNestedResultsAnnotationKey(TimeExpression.ChildrenAnnotation.class); env.setDefaultTokensAggregators(CoreMapAttributeAggregator.DEFAULT_NUMERIC_TOKENS_AGGREGATORS); env.bind("nested", TimeExpression.ChildrenAnnotation.class); env.bind("time", new TimeFormatter.TimePatternExtractRuleCreator()); env.setDefaultStringPatternFlags(Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE); env.bind("options", options); env.bind("TIME_REF", SUTime.TIME_REF); env.bind("TIME_REF_UNKNOWN", SUTime.TIME_REF_UNKNOWN); env.bind("TIME_UNKNOWN", SUTime.TIME_UNKNOWN); env.bind("TIME_NONE", SUTime.TIME_NONE); env.bind("ERA_AD", SUTime.ERA_AD); env.bind("ERA_BC", SUTime.ERA_BC); env.bind("ERA_UNKNOWN", SUTime.ERA_UNKNOWN); env.bind("HALFDAY_AM", SUTime.HALFDAY_AM); env.bind("HALFDAY_PM", SUTime.HALFDAY_PM); env.bind("HALFDAY_UNKNOWN", SUTime.HALFDAY_UNKNOWN); env.bind("RESOLVE_TO_THIS", SUTime.RESOLVE_TO_THIS); env.bind("RESOLVE_TO_PAST", SUTime.RESOLVE_TO_PAST); env.bind("RESOLVE_TO_FUTURE", SUTime.RESOLVE_TO_FUTURE); env.bind("RESOLVE_TO_CLOSEST", SUTime.RESOLVE_TO_CLOSEST); env.bind("numcomptype", CoreAnnotations.NumericCompositeTypeAnnotation.class); env.bind("numcompvalue", CoreAnnotations.NumericCompositeValueAnnotation.class);
public Value evaluate(Env env, Object... args) { Value value = valueExpr.evaluate(env, args); if (args != null) { if (args.length == 1 && args[0] instanceof CoreMap) { CoreMap cm = (CoreMap) args[0]; Class annotationKey = EnvLookup.lookupAnnotationKey(env, varName); if (annotationKey != null) { cm.set(annotationKey, (value != null)? value.get():null); return value; } } } if (bindAsValue) { env.bind(varName, value); } else { env.bind(varName, (value != null)? value.get():null); if (TYPE_REGEX == value.getType()) { try { Object vobj = value.get(); if (vobj instanceof String) { env.bindStringRegex(varName, (String) vobj); } else if (vobj instanceof Pattern) { env.bindStringRegex(varName, ((Pattern) vobj).pattern()); } } catch (Exception ex) {} } } return value; }
private void initEnv() { env = TokenSequencePattern.getNewEnv(); env.setDefaultTokensAnnotationKey(CoreAnnotations.NumerizedTokensAnnotation.class); // Do case insensitive matching env.setDefaultStringMatchFlags(Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE); env.setDefaultStringPatternFlags(Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE); try { Units.registerUnits(env, options.unitsFilename); } catch (IOException ex) { throw new RuntimeException("Error loading units from " + options.unitsFilename, ex); } try { UnitPrefix.registerPrefixes(env, options.prefixFilename); } catch (IOException ex) { throw new RuntimeException("Error loading prefixes from " + options.prefixFilename, ex); } env.bind("options", options); env.bind("numcomptype", CoreAnnotations.NumericCompositeTypeAnnotation.class); env.bind("numcompvalue", CoreAnnotations.NumericCompositeValueAnnotation.class); }
public static String getKeyForClass(Class classR) { String key =class2KeyMapping.get(classR); if(key == null){ for(Map.Entry<String, Object> vars: ConstantsAndVariables.globalEnv.getVariables().entrySet()){ if(vars.getValue().equals(classR)){ key = vars.getKey().toLowerCase(); class2KeyMapping.put(classR, key); break; } } } if(key == null){ key = classR.getSimpleName().toLowerCase(); class2KeyMapping.put(classR, key); ConstantsAndVariables.globalEnv.bind(key, classR); } return key; }
private static AnnotationExtractRuleCreator lookupExtractRuleCreator(Env env, String ruleType) { if (env != null) { Object obj = env.get(ruleType); if (obj != null && obj instanceof AnnotationExtractRuleCreator) { return (AnnotationExtractRuleCreator) obj; } } if (ruleType == null) { return DEFAULT_EXTRACT_RULE_CREATOR; } else { return registeredRuleTypes.get(ruleType); } }
@Override public Value evaluate(Env env, Object... args) { Expression exp = null; String varName = value; if (args != null) { if (args.length == 1 && args[0] instanceof CoreMap) { CoreMap cm = (CoreMap) args[0]; if (VAR_SELF.equals(varName)) { return createValue(varName, cm); } Class annotationKey = EnvLookup.lookupAnnotationKey(env, varName); if (annotationKey != null) { return createValue(varName, cm.get(annotationKey)); } } } if (VAR_SELF.equals(varName)) { return createValue(varName, env.peek(varName)); } Object obj = env.get(varName); if (obj != null) { exp = asExpression(env, obj); } Value v = exp != null? exp.evaluate(env, args): null; if (v == null) { log.info("Unknown variable: " + varName); } return v; } public Expression assign(Expression expr) {
value = value.substring(1, value.length() - 1); value = value.replaceAll("\\\\/", "/"); // Unescape forward slash String regex = (env != null) ? env.expandStringRegex(value) : value; int flags = (env != null) ? env.defaultStringPatternFlags : 0; p.add(c, newStringRegexPattern(regex, flags)); boolean ok = false; if (env != null) { Object custom = env.get(value); if (custom != null) { p.add(c, (NodePattern) custom);
public TokensRegexAnnotator(String name, Properties props) { String prefix = (name == null)? "": name + '.'; String[] files = PropertiesUtils.getStringArray(props, prefix + "rules"); env = TokenSequencePattern.getNewEnv(); env.bind("options", options); if (PropertiesUtils.getBool(props, prefix+"caseInsensitive")) { System.err.println("using case insensitive!"); env.setDefaultStringMatchFlags(NodePattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE); env.setDefaultStringPatternFlags(Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE); } if (files.length != 0) { extractor = CoreMapExpressionExtractor.createExtractorFromFiles(env, files); } else { extractor = null; } verbose = PropertiesUtils.getBool(props, prefix + "verbose", false); options.setTokenOffsets = PropertiesUtils.getBool(props, prefix + "setTokenOffsets", options.setTokenOffsets); options.extractWithTokens = PropertiesUtils.getBool(props, prefix + "extractWithTokens", options.extractWithTokens); options.flatten = PropertiesUtils.getBool(props, prefix + "flatten", options.flatten); String matchedExpressionsAnnotationKeyName = props.getProperty(prefix + "matchedExpressionsAnnotationKey"); if (matchedExpressionsAnnotationKeyName != null) { options.matchedExpressionsAnnotationKey = EnvLookup.lookupAnnotationKeyWithClassname(env, matchedExpressionsAnnotationKeyName); if (options.matchedExpressionsAnnotationKey == null) { String propName = prefix + "matchedExpressionsAnnotationKey"; throw new RuntimeException("Cannot determine annotation key for " + propName + '=' + matchedExpressionsAnnotationKeyName); } } }
public static Class getDefaultNestedResultsAnnotationKey(Env env) { if (env != null) { Class obj = env.getDefaultNestedResultsAnnotationKey(); if (obj != null) { return obj; } } return null; }
public static Function<MatchedExpression,?> getDefaultResultAnnotationExtractor(Env env) { if (env != null) { Function<MatchedExpression,?> obj = env.getDefaultResultsAnnotationExtractor(); if (obj != null) { return obj; } } return null; }
public static Map<Class, CoreMapAttributeAggregator> getDefaultTokensAggregators(Env env) { if (env != null) { Map<Class, CoreMapAttributeAggregator> obj = env.getDefaultTokensAggregators(); if (obj != null) { return obj; } } return CoreMapAttributeAggregator.DEFAULT_NUMERIC_TOKENS_AGGREGATORS; }
public static List<Class> getDefaultResultAnnotationKey(Env env) { if (env != null) { List<Class> obj = env.getDefaultResultAnnotationKey(); if (obj != null) { return obj; } } return null; }
public static Class getDefaultTextAnnotationKey(Env env) { if (env != null) { Class obj = env.getDefaultTextAnnotationKey(); if (obj != null) { return obj; } } return CoreAnnotations.TextAnnotation.class; }
public Pattern getStringPattern(String regex) { String expanded = expandStringRegex(regex); return Pattern.compile(expanded, defaultStringPatternFlags); }
public void unbind(String name) { bind(name, null); }
/** * Creates a default instance with the specified environment. * (use the default tokens annotation key as specified in the environment) * @param env Environment to use for binding variables and applying rules */ public CoreMapExpressionExtractor(Env env) { this.stages = new HashMap<>();//Generics.newHashMap(); this.env = env; this.tokensAnnotationKey = EnvLookup.getDefaultTokensAnnotationKey(env); this.collapseExtractionRules = false; if (env != null) { this.collapseExtractionRules = Objects.equals((Boolean) env.get("collapseExtractionRules"), true); if (env.get("verbose") != null) verbose = (env.get("verbose") != null) && Objects.equals((Boolean) env.get("verbose"), true); } }
private void initEnv() { env = TokenSequencePattern.getNewEnv(); env.setDefaultTokensAnnotationKey(CoreAnnotations.NumerizedTokensAnnotation.class); // Do case insensitive matching env.setDefaultStringMatchFlags(Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE); env.setDefaultStringPatternFlags(Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE); try { Units.registerUnits(env, options.unitsFilename); } catch (IOException ex) { throw new RuntimeException("Error loading units from " + options.unitsFilename, ex); } try { UnitPrefix.registerPrefixes(env, options.prefixFilename); } catch (IOException ex) { throw new RuntimeException("Error loading prefixes from " + options.prefixFilename, ex); } env.bind("options", options); env.bind("numcomptype", CoreAnnotations.NumericCompositeTypeAnnotation.class); env.bind("numcompvalue", CoreAnnotations.NumericCompositeValueAnnotation.class); }
public Value evaluate(Env env, Object... args) { Expression exp = null; String varName = value; if (args != null) { if (args.length == 1 && args[0] instanceof CoreMap) { CoreMap cm = (CoreMap) args[0]; if (VAR_SELF.equals(varName)) { return createValue(varName, cm); } Class annotationKey = EnvLookup.lookupAnnotationKey(env, varName); if (annotationKey != null) { return createValue(varName, cm.get(annotationKey)); } } } if (VAR_SELF.equals(varName)) { return createValue(varName, env.peek(varName)); } Object obj = env.get(varName); if (obj != null) { exp = asExpression(env, obj); } Value v = exp != null? exp.evaluate(env, args): null; if (v == null) { System.err.println("Unknown variable: " + varName); } return v; } public Expression assign(Expression expr) {