엘라스틱서치
은전한닢 오픈소스를 이용한 형태소 분석기 예제
까오기
2023. 6. 22. 17:11
검색에서 필요한 형태소 분석기로 설치형이 라이브러리 추가 만으로 이용 가능하며 쉽고 빠른 성능을 보여 줍니다.
참고
- http://eunjeon.blogspot.com/2013/02/blog-post.html
은전한닢 프로젝트를 소개합니다.
은전한닢 프로젝트: 오픈 소스 한국어 / 한글 형태소 분석기 Lucene/Solr, ElasticSearch 플러그인
eunjeon.blogspot.com
- https://bitbucket.org/eunjeon/seunjeon
Bitbucket
bitbucket.org
1. Dependency 추가
<dependency>
<groupId>org.bitbucket.eunjeon</groupId>
<artifactId>seunjeon_2.12</artifactId>
<version>1.5.0</version>
<exclusions>
<exclusion>
<groupId>com.jsuereth</groupId>
<artifactId>sbt-pgp</artifactId>
</exclusion>
</exclusions>
</dependency>
2. 샘플 코드
@Slf4j
public class TextAnalyzer {
/**
* 형태소 분석 테스트
* @param text
*/
private void testParseJava(String text) {
log.info("############# testParseJava : {}", text);
Iterable<LNode> iterable = Analyzer.parseJava(text);
print(iterable);
}
private void print(Iterable<LNode> iterable) {
iterable.forEach(node -> {
log.info("Cost : {}", node.morpheme().getCost());
log.info("Feature : {}", node.morpheme().getFeature());
log.info("FeatureHead : {}", node.morpheme().getFeatureHead());
log.info("Surface : {}", node.morpheme().getSurface());
});
}
/**
* 복합어 분해
* @param text
*/
private void testParseJavaDecompound(String text) {
log.info("############# testParseJavaDecompound : {}", text);
Analyzer.parseJava(text).forEach(compoundNode -> {
print(compoundNode.deCompoundJava());
});
}
/**
* 활용어 원형
* @param text
*/
private void testDeInflectJava(String text) {
log.info("############# testDeInflectJava : {}", text);
Analyzer.parseJava(text).forEach(itemNode -> {
print(itemNode.deInflectJava());
});
}
/**
* 어절 분석 테스트
* @param text
*/
private void testParseEojeolJava(String text) {
log.info("############# testParseEojeolJava : {}", text);
Iterable<Eojeol> iterable = Analyzer.parseEojeolJava(text);
iterable.forEach(eojeol -> {
print(eojeol.nodesJava());
});
}
private void setUserDictionaries() {
List<String> words = Arrays.asList("리알로에","롤랑+가로스","떢뽂이","더바디샾","크로거다일","홍어+회","보늬밤","암소+갈비","소+갈비","컬+바벨","애견+발+세정제","다나은","소바바","디스커브리","클라펠","쪼임이","이너닷","에어리+써커");
List<String> dictionary = appendStringLength(words);
Analyzer.setUserDict(dictionary.iterator());
}
private List<String> appendStringLength(List<String> strings) {
return strings.stream().map(word -> {
int length = word.length();
return word + "," + (-100*length);
}).collect(Collectors.toList());
}
public static void main(String[] args) {
TextAnalyzer analyzer = new TextAnalyzer();
analyzer.testParseJava("아버지가방에들어가신다");
analyzer.testParseEojeolJava("아버지가방에들어가신다");
analyzer.setUserDictionaries();
analyzer.testParseJavaDecompound("홍어회와 암소갈비를떢뽂이와 돼지갈비 소갈비를 함께 먹어보자");
analyzer.testParseJavaDecompound("홍어회와암소갈비를떢뽂이와돼지갈비소갈비를함께먹어보자");
analyzer.testDeInflectJava("홍어회와암소갈비를떢뽂이와돼지갈비소갈비를함께먹어보자");
}
}
위에 예제는 사용자사전을 추가하고 복합어 등을 형태소 분석하는 예로 다양한 활용이 가능합니다.