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; }
public void unbind(String name) { bind(name, null); }
private static void initEnv(Env env) { // Custom binding for numeric values expressions env.bind("numtype", CoreAnnotations.NumericTypeAnnotation.class); env.bind("numvalue", CoreAnnotations.NumericValueAnnotation.class); env.bind("numcomptype", CoreAnnotations.NumericCompositeTypeAnnotation.class); env.bind("numcompvalue", CoreAnnotations.NumericCompositeValueAnnotation.class); env.bind("$NUMCOMPTERM", " [ { numcomptype::EXISTS } & !{ numcomptype:NUMBER_RANGE } ] "); env.bind("$NUMTERM", " [ { numtype::EXISTS } & !{ numtype:NUMBER_RANGE } ] "); env.bind("$NUMRANGE", " [ { numtype:NUMBER_RANGE } ] "); // TODO: Improve code to only recognize integers env.bind("$INTTERM", " [ { numtype::EXISTS } & !{ numtype:NUMBER_RANGE } & !{ word:/.*\\.\\d+.*/} ] "); env.bind("$POSINTTERM", " [ { numvalue>0 } & !{ word:/.*\\.\\d+.*/} ] "); env.bind("$ORDTERM", " [ { numtype:ORDINAL } ] "); env.bind("$BEFORE_WS", " [ { before:/\\s*/ } | !{ before::EXISTS} ]"); env.bind("$AFTER_WS", " [ { after:/\\s*/ } | !{ after::EXISTS} ]"); env.bind("$BEFORE_AFTER_WS", " [ $BEFORE_WS & $AFTER_WS ]"); }
@Override public void bind(Env env) { if (holidays != null) { for (Map.Entry<String, JollyHoliday> holidayEntry : holidays.entrySet()) { JollyHoliday jh = holidayEntry.getValue(); env.bind(varPrefix + holidayEntry.getKey(), jh); } } }
public void bind(String name, SequencePattern pattern) { bind(name, pattern.getPatternExpr()); }
public static void registerPrefix(Env env, UnitPrefix prefix) { env.bind(prefix.getName().toUpperCase(), prefix); }
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 void readClassesInEnv(String s, Map<String, Env> env, Env globalEnv) throws ClassNotFoundException { for(String line: IOUtils.readLines(s)){ String[] toks = line.split("###"); if(toks.length == 3){ String label = toks[0]; String name = toks[1]; Class c = Class.forName(toks[2]); if(!env.containsKey(label)) env.put(label, TokenSequencePattern.getNewEnv()); env.get(label).bind(name, c); }else if(toks.length ==2){ String name = toks[0]; Class c = Class.forName(toks[1]); assert c!=null : " Why is name for " + toks[1] + " null"; globalEnv.bind(name, c); }else throw new RuntimeException("Ill formed env file!"); } }
public static void registerUnit(Env env, Unit unit) { env.bind((unit.getType() + "_" + unit.getName()).toUpperCase(), unit); }
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 void registerDerivedUnit(Env env, Unit unit, String derivedType, String suffix, String symbolSuffix) { Unit derivedUnit = new Unit(unit.getName() + " " + suffix, unit.getSymbol() + symbolSuffix, derivedType); env.bind(derivedType + "_" + unit.getName().toUpperCase() + "_" + suffix.toUpperCase(), derivedUnit); }
public KBPTokensregexExtractor(String tokensregexDir, boolean verbose) { if (verbose) logger.log("Creating TokensRegexExtractor"); // Create extractors for (RelationType rel : RelationType.values()) { String relFileNameComponent = rel.canonicalName.replaceAll(":", "_"); String path = tokensregexDir + File.separator + relFileNameComponent.replaceAll("/", "SLASH") + ".rules"; if (IOUtils.existsInClasspathOrFileSystem(path)) { List<String> listFiles = new ArrayList<>(); listFiles.add(tokensregexDir + File.separator + "defs.rules"); listFiles.add(path); if (verbose) logger.log("Rule files for relation " + rel + " is " + path); Env env = TokenSequencePattern.getNewEnv(); env.bind("collapseExtractionRules", true); env.bind("verbose", verbose); CoreMapExpressionExtractor extr = CoreMapExpressionExtractor.createExtractorFromFiles(env, listFiles).keepTemporaryTags(); rules.put(rel, extr); } } }
env.bind("nested", TimeExpression.ChildrenAnnotation.class); env.bind("time", new TimeFormatter.TimePatternExtractRuleCreator()); 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); env.bind("temporal", TimeExpression.Annotation.class); env.bind("::IS_TIMEX_DATE", new TimexTypeMatchNodePattern(SUTime.TimexType.DATE)); env.bind("::IS_TIMEX_DURATION", new TimexTypeMatchNodePattern(SUTime.TimexType.DURATION)); env.bind("::IS_TIMEX_TIME", new TimexTypeMatchNodePattern(SUTime.TimexType.TIME)); env.bind("::IS_TIMEX_SET", new TimexTypeMatchNodePattern(SUTime.TimexType.SET)); env.bind("::IS_TIME_UNIT", new MatchedExpressionValueTypeMatchNodePattern("TIMEUNIT"));
env.get(label).bind(en.getKey(), en.getValue()); env.get(label).bind(en.getKey(), en.getValue()); env.get(label).bind("$FILLER", "/" + StringUtils.join(PatternFactory.fillerWords, "|") + "/"); env.get(label).bind("$STOPWORD", stopStr); env.get(label).bind("$MOD", "[{tag:/JJ.*/}]"); if (matchLowerCaseContext){ env.get(label).setDefaultStringMatchFlags(NodePattern.CASE_INSENSITIVE | NodePattern.UNICODE_CASE); env.get(label).setDefaultStringPatternFlags(Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE); env.get(label).bind("OTHERSEM", PatternsAnnotations.OtherSemanticLabel.class); env.get(label).bind("grandparentparsetag", CoreAnnotations.GrandparentAnnotation.class);
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); } } }
@Override public void bind(Env env) { if (holidays != null) { for (String s:holidays.keySet()) { JollyHoliday jh = holidays.get(s); env.bind(varPrefix + s, jh); } } }
@Override public void bind(Env env) { if (holidays != null) { for (Map.Entry<String, JollyHoliday> holidayEntry : holidays.entrySet()) { JollyHoliday jh = holidayEntry.getValue(); env.bind(varPrefix + holidayEntry.getKey(), jh); } } }