2016-07-06 4 views
1

私はJavaで、テキスト内のキーワードの組み合わせの出現をチェックするシステムを開発しています。 たとえば、次の式をチェックします:(yellow || red) && sofa。 私はジョブを2つのステップに分けました。最初のものは、テキスト内の個々の単語を検出することです。 2番目の方法は、結果をブール式のチェックに使用する方法です。 短いWeb検索の後、Apache JEXLを選択します。Java Apache JEXLブール式の問題

// My web app contains a set of preconfigured keywords inserted by administrator: 
List<String> system_occurence =new ArrayList<String>() {{ 
    add("yellow"); 
    add("brown"); 
    add("red"); 
    add("kitchen"); 
    add("sofa");   
}}; 


// The method below check if some of system keywords are in the text 
List<String> occurence = getOccurenceKeywordsInTheText(); 
for (String word :occurrence){ 
    jexlContext.set(word,true); 
} 

// Set to false system keywords not in the text 
system_occurence.removeAll(occurence); 
for (String word :system_occurence){ 
    jexlContext.set(word,false); 
} 


// Value the boolean expression 
String jexlExp ="(yellow || red) && sofa"; 
JexlExpression e = jexl.createExpression(jexlExp_ws_keyword_matching); 

Boolean o = (Boolean) e.evaluate(jexlContext); 

上記の例では、ブール式で単純な単語を使用します。 ASCIIと非複合語では問題はありません。 変数名として使用できないため、ブール式内の非ASCIIキーワードと複合キーワードに問題があります。

// The below example fails, JEXL launch Exception 
String jexlExp ="(Lebron James || red) && sofa"; 

// The below example fails, JEXL launch Exception 
String jexlExp ="(òsdà || red) && sofa"; 

どうすれば解決できますか?それは私の方法ですか?

私の悪い英語のため申し訳ありません:)

答えて

0

は、アンダースコアのようないくつかの他の文字(_)とスペースを交換してください。

package jexl; 



    import org.apache.commons.jexl3.*; 

    import java.util.ArrayList; 
    import java.util.Arrays; 
    import java.util.List; 

    public class Test2 { 

    private static final JexlEngine JEXL_ENGINE = new JexlBuilder().cache(512).strict(false).silent(false).create(); 
    public static void main(String[] args) { 
     // My web app contains a set of preconfigured keywords inserted by administrator: 
     List<String> system_occurence =new ArrayList<String>() {{ 
      add("yellow"); 
      add("brown"); 
      add("red"); 
      add("kitchen"); 
      add("sofa"); 
     }}; 

     JexlContext jexlContext = new MapContext(); 

// The method below check if some of system keywords are in the text 
     List<String> occurence = Arrays.asList("kitchen"); 
     for (String word :occurence){ 
      jexlContext.set(word,true); 
     } 

// Set to false system keywords not in the text 
     system_occurence.removeAll(occurence); 
     for (String word :system_occurence){ 
      jexlContext.set(word,false); 
     } 


// Value the boolean expression 
     String jexlExp ="(Lebron_James || red) && sofa"; 
     JexlExpression e = JEXL_ENGINE.createExpression(jexlExp); 

     Boolean o = (Boolean) e.evaluate(jexlContext); 

     System.out.println(o); 
    } 

}