2016-07-06 11 views
1

자바에서 텍스트의 키워드 조합을 확인하는 시스템을 개발 중입니다. 예를 들어 확인하려면 다음 표현식을 사용합니다. (yellow || red) && sofa. 나는 두 단계로 작업을 나눴다. 첫 번째는 텍스트에서 개별 단어를 감지하는 것입니다. 둘째는 부울 식을 확인하는 데 결과를 사용하는 것입니다. 짧은 웹 검색 후 Apache JEXL을 선택합니다.자바 아파치 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); 
    } 

}