Peter Klügl <pkluegl@...> writes:
>
> Hi,
>
> Am 04.12.2013 18:33, schrieb Sebastian:
> > Hi,
> >
> > I'm highly interested in ruta, and its potential applications in
industrial
> > applications. Right know I'm trying to create a simple toy condition
> > extension that is simply a case insensitive INLIST condition. It is
> > completely based on the InListCondition class, I also declared an
> > implementation of the IRutaConditionExtension interface.
> >
> > With primitve types everything seems to work great, except when the
> > condition is used with a variable :
> >
> > STRINGLIST MonthsList = {"january", ...};
> > DECLARE Month;
> > ANY{INSENSITIVEINLIST(MonthsList) -> MARK(Month)};
> >
> > I get a class cast exception when the condition is being created,
because
> > MonthsList is a SimpleTypeExpression and I'm expecting a
StringListExpression.
> >
> > Am I doing something wrong ? I suppose there is a way to resolve the
> > variable to the actual list, but I missed it somehow.
> >
>
> It's hard to say what went wrong. My first guess would be that there is
> a problem in your extension. I just verified that INLIST works at all (I
> haven't used it myself for a long time).
>
> The example works with INLIST:
>
> STRINGLIST MonthsList = {"january"};
> DECLARE Month;
> ANY{INLIST(MonthsList) -> MARK(Month)};
>
> Can you post the stacktrace of the exception? Or can you send me the
> source code of your extension (in case you do not want to post it on a
> public mailing list)?
>
> Anyways, the usage of INLIST makes only sense if you want to work on
> dynamic dictionaries that may change during rule execution. Have you
> taken a look at the MARKFAST or TRIE action?
> http://uima.apache.org/d/ruta-
current/tools.ruta.book.html#ugr.tools.ruta.language.actions.markfast
> They already have options for case-insensitivity and are overall faster
> and more powerful.
>
> Best,
>
> Peter
>
> PS: You can, of course, also post a feature request on JIRA for adding a
> case-insensitivity to the INLIST condition
>
> > Any ideas on how that could be done?
> >
> > Regards
> >
> > Sebastian
> >
>
>
Hi Peter,
Before giving the code, let me explain why I'm interested in a case
insensitive inlist.
As far as I understand the behaviour of MARKFAST, it cannot be used with
more complex conditions than list containment. The problem with TRIE is that
it requires an external resource that is somewhat read from the file system,
whereas I'm interested in somehow embedding resources in jars and reading
them using classloader getResource capabilities (maybe I missed something
there too).
But you are right, I don't need a dynamic dictionary :)
Anyway, here's how I declared it :
public class CIInListCondition extends TerminalRutaCondition {
private StringListExpression stringList;
public CIInListCondition(StringListExpression list) {
super();
this.stringList = list;
}
@Override
public EvaluatedCondition eval(AnnotationFS annotation,
RuleElement element, RutaStream stream, InferenceCrowd crowd) {
String coveredText = annotation.getCoveredText();
if (StringUtils.isEmpty(coveredText))
return new EvaluatedCondition(this, false);
List<String> sList = stringList.getList(element.getParent(),
stream);
return new EvaluatedCondition(this,
sList.contains(coveredText.toLowerCase()));
}
public StringListExpression getStringList() {
return stringList;
}
}
And the associated extension
public class CIInListConditionExtension implements IRutaConditionExtension {
private final String[] knownExtensions = new String[] {
"INSENSITIVEINLIST" };
private final Class<?>[] extensions = new Class[] {
CIInListCondition.class };
...
@Override
public AbstractRutaCondition createCondition(String name,
List<RutaExpression> args) throws RutaParseException {
if (args != null && args.size() == 1) {
System.out.println(args.get(0).getClass().getName()); // prints
org.apache.uima.ruta.expression.type.SimpleTypeExpression
System.out.println(((SimpleTypeExpression)args.get(0)).getTypeString()); //
prints MonthsList
if (!(args.get(0) instanceof StringListExpression)) {
}
} else {
throw new RutaParseException(
"INSENSITIVEINLIST accepts exactly a
StringListExpression as arguments");
}
return new CIInListCondition((StringListExpression) args.get(0)); //
It Fails here
}
And here's the stack trace :
java.lang.ClassCastException:
org.apache.uima.ruta.expression.type.SimpleTypeExpression cannot be cast to
org.apache.uima.ruta.expression.list.StringListExpression
at
dictanova.genesis.textpreprocessing.ruta.CIInListConditionExtension.createCo
ndition(CIInListConditionExtension.java:68)
Regards,
Sebastian
|