2014-11-20 1 views
2

현재 icu4j를 기반으로 한 음역 웹 인터페이스를 만들고 있습니다. 사용자가 쿼리를 입력하는 스크립트 시스템을 자동으로 검색하는 가장 좋은 방법은 무엇입니까?UTF-8 입력에서 스크립트 시스템/알파벳을 감지하는 방법은 무엇입니까?

예. 입력이 본체이거나 عالمتاب 인 경우 어떤 스크립트 시스템에서이를 인식 할 수 있습니까?

+1

https : //로 GitHub의. co.kr/digital-preservation/utf8-validator –

답변

2

가장 간단한 방법은 첫 번째 문자의 스크립트를 확인하는 것입니다 :

static Character.UnicodeScript getScript(String s) { 
    if (s.isEmpty()) { 
     return null; 
    } 
    return Character.UnicodeScript.of(s.codePointAt(0)); 
} 

더 좋은 방법이 가장 자주 발생하는 스크립트를 발견하는 것입니다 :

static Character.UnicodeScript getScript(String s) { 
    int[] counts = new int[Character.UnicodeScript.values().length]; 

    Character.UnicodeScript mostFrequentScript = null; 
    int maxCount = 0; 

    int n = s.codePointCount(0, s.length()); 
    for (int i = 0; i < n; i = s.offsetByCodePoints(i, 1)) { 
     int codePoint = s.codePointAt(i); 
     Character.UnicodeScript script = Character.UnicodeScript.of(codePoint); 

     int count = ++counts[script.ordinal()]; 
     if (mostFrequentScript == null || count > maxCount) { 
      maxCount = count; 
      mostFrequentScript = script; 
     } 
    } 

    return mostFrequentScript; 
} 
+0

감사합니다. 나는 어떻게 of (int codePoint)라는 작은 메서드를 간과했다. [Javadoc] (https://docs.oracle.com/javase/7/docs/api/java/lang/Character.UnicodeScript.html#of%28int%29) – Andreas

관련 문제