
IntlRuleBasedBreakIterator 클래스의 getBinaryRules 메소드는 BinaryRules 객체를 반환하며, 이 객체는 텍스트를 분할하는 규칙을 포함합니다.
BinaryRules 객체는 다음과 같은 속성을 가지고 있습니다.
- rules: 텍스트를 분할하는 규칙을 포함하는 컬렉션입니다.
- type: BinaryRules 객체의 유형을 나타내는 정수입니다.
BinaryRules 객체는 다음과 같은 메소드를 제공합니다.
- getRules(): 텍스트를 분할하는 규칙을 포함하는 컬렉션을 반환합니다.
- getType(): BinaryRules 객체의 유형을 반환합니다.
IntlRuleBasedBreakIterator 클래스의 getBinaryRules 메소드를 사용하여 텍스트를 분할할 때 고려해야 하는 사항은 다음과 같습니다.
- 텍스트를 분할하는 규칙을 정의해야 합니다.
- 텍스트를 분할하는 규칙을 BinaryRules 객체에 추가해야 합니다.
- BinaryRules 객체를 IntlRuleBasedBreakIterator 클래스에 전달해야 합니다.
- IntlRuleBasedBreakIterator 클래스는 BinaryRules 객체를 사용하여 텍스트를 분할합니다.
예제 코드는 다음과 같습니다.
#hostingforum.kr
java
import java.text.BreakIterator;
import java.text.InternationalBreakIterator;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 텍스트를 분할하는 규칙을 정의합니다.
List rules = new ArrayList<>();
rules.add(new Rule(" ", BreakIterator.BREADTH));
rules.add(new Rule(" ", BreakIterator.WEAK));
// BinaryRules 객체를 생성합니다.
BinaryRules binaryRules = new BinaryRules();
binaryRules.setRules(rules);
binaryRules.setType(BreakIterator.BREADTH);
// IntlRuleBasedBreakIterator 클래스를 생성합니다.
BreakIterator breakIterator = new InternationalBreakIterator(binaryRules);
// 텍스트를 분할합니다.
String text = "Hello World!";
int start = breakIterator.first();
while (BreakIterator.DONE != start) {
int end = breakIterator.next();
System.out.println(breakIterator.getText(start, end));
start = end;
}
}
}
class Rule {
private String pattern;
private int type;
public Rule(String pattern, int type) {
this.pattern = pattern;
this.type = type;
}
public String getPattern() {
return pattern;
}
public int getType() {
return type;
}
}
class BinaryRules {
private List rules;
private int type;
public List getRules() {
return rules;
}
public void setRules(List rules) {
this.rules = rules;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}
2025-07-15 15:14