Search-Engine/Lucene 2019. 1. 29. 23:04

Lucene - 분석기(Analyzer)로 분석한 토큰(Token)결과 출력




루씬에서 색인을 하기위해서는 선행과정이 있다. 물론 문서안에 정의된 여러개의 필드에 적용한 속성에 따라 다르긴 하지만 ANALYZE속성을 적용한 필드인 경우에는 색인하기 이전에 텍스트를 토큰으로 추출하고 그 토큰에 여러가지 메타정보(start,end 정수/위치증가값 등등의 데이터)를 섞은 텀으로 만든 후에 색인에 들어간다. 여기에서 보여줄 예제는 색인을 위한 텍스트에 분석기의 분석과정을 적용 후에 어떻게 토큰이 분리되는지 확인하는 간단한 예제이다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.lucene.study;
 
 
 
import org.apache.lucene.analysis.core.SimpleAnalyzer;
 
import org.apache.lucene.analysis.core.StopAnalyzer;
 
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
 
import org.apache.lucene.analysis.standard.StandardAnalyzer;
 
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
 
import org.apache.lucene.analysis.tokenattributes.CharTermAttributeImpl;
 
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
 
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
 
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
 
import org.apache.lucene.util.AttributeImpl;
 
 
 
import java.io.IOException;
 
import java.io.StringReader;
 
 
 
 
 
import org.apache.lucene.analysis.*;
 
 
 
public class AnalyzerTest {
 
    
 
    
 
    private static final String[] examples = {"The quick brown fox jumped over the lazy dog","XY&Z Corporation - zyz@example.com","안녕하세요? 피자 주문하려고 하는데요."};
 
    
 
    private static final Analyzer[] analyzers = new Analyzer[] {new WhitespaceAnalyzer(),new SimpleAnalyzer(),new StopAnalyzer(),new StandardAnalyzer()};
 
    
 
    
 
    public static void analyze(String text) throws IOException {
 
        System.out.println("Analyzing \"" +text+ "\"");
 
        System.out.println("\n");
 
        for(Analyzer analyzer : analyzers) {
 
            
 
            String name = analyzer.getClass().getSimpleName();
 
            System.out.println("  "+name+"  ");
 
            System.out.print("    ");
 
            AnalyzerUtils.displayTokens(analyzer,text);
 
            
 
            System.out.println("\n");
 
        }
 
    }
 
    public static void main(String[] args) throws IOException {
 
        // TODO Auto-generated method stub
 
        String[] strings = examples;
 
        
 
        for(String text:strings) {
 
            analyze(text);
 
        }
 
    }
 
 
 
}
 
 
 
class AnalyzerUtils{
 
    public static void displayTokens(Analyzer analyzer,String text) throws IOException {
 
        displayTokens(analyzer.tokenStream("content"new StringReader(text)));
 
    }
 
    
 
    public static void displayTokens(TokenStream stream) throws IOException {
 
        
 
        //텀 속성확인
 
        CharTermAttribute cattr = stream.addAttribute(CharTermAttribute.class);
 
        
 
        //위치증가값 속성 확인
 
        PositionIncrementAttribute postAtrr = stream.addAttribute(PositionIncrementAttribute.class);
 
        //오프셋위치확인
 
        OffsetAttribute offsetAttr = stream.addAttribute(OffsetAttribute.class);
 
        //텀타입 속성 확인
 
        TypeAttribute typeAttr = stream.addAttribute(TypeAttribute.class);
 
        
 
        
 
        stream.reset();
 
        
 
        int position = 0;
 
        
 
        while (stream.incrementToken()) {
 
            int increment = postAtrr.getPositionIncrement();
 
            
 
            position = position + increment;
 
            System.out.println();
 
            System.out.print(position + ": ");
 
            System.out.print("[ "+cattr.toString()+" : " + offsetAttr.startOffset()+"->"+offsetAttr.endOffset()+" : "+typeAttr.type()+" ]");
 
        }
 
 
 
        stream.end();
 
        stream.close();
 
        
 
    }
 
}
cs



posted by 여성게
: