How to split a Thai sentence, which does not use spaces, into words?

后端 未结 4 1134
不思量自难忘°
不思量自难忘° 2021-02-19 06:46

How to split word from Thai sentence? English we can split word by space.

Example: I go to school, split = [\'I\', \'go\', \'to\' ,\'school\']

4条回答
  •  长情又很酷
    2021-02-19 07:16

    In 2006, someone contributed code to the Apache Lucene project to make this work.

    Their approach (written in Java) was to use the BreakIterator class, calling getWordInstance() to get a dictionary-based word iterator for the Thai language. Note also that there is a stated dependency on the ICU4J project. I have pasted the relevant section of their code below:

      private BreakIterator breaker = null;
      private Token thaiToken = null;
    
      public ThaiWordFilter(TokenStream input) {
        super(input);
        breaker = BreakIterator.getWordInstance(new Locale("th"));
      }
    
      public Token next() throws IOException {
        if (thaiToken != null) {
          String text = thaiToken.termText();
          int start = breaker.current();
          int end = breaker.next();
          if (end != BreakIterator.DONE) {
            return new Token(text.substring(start, end), 
                thaiToken.startOffset()+start,
                thaiToken.startOffset()+end, thaiToken.type());
          }
          thaiToken = null;
        }
        Token tk = input.next();
        if (tk == null) {
          return null;
        }
        String text = tk.termText();
        if (UnicodeBlock.of(text.charAt(0)) != UnicodeBlock.THAI) {
          return new Token(text.toLowerCase(), 
                           tk.startOffset(), 
                           tk.endOffset(), 
                           tk.type());
        }
        thaiToken = tk;
        breaker.setText(text);
        int end = breaker.next();
        if (end != BreakIterator.DONE) {
          return new Token(text.substring(0, end), 
              thaiToken.startOffset(), 
              thaiToken.startOffset()+end,
              thaiToken.type());
        }
        return null;
      }
    

提交回复
热议问题