1 |
| package photospace.search; |
2 |
| |
3 |
| import java.io.*; |
4 |
| import org.apache.lucene.analysis.*; |
5 |
| import org.apache.lucene.analysis.Token; |
6 |
| import org.apache.lucene.analysis.standard.*; |
7 |
| import com.carbonfive.gis.*; |
8 |
| |
9 |
| public final class MetaAnalyzer extends StandardAnalyzer |
10 |
| { |
11 |
| private double latitude; |
12 |
| private double longitude; |
13 |
| private double radius; |
14 |
| private boolean hasPoint; |
15 |
| |
16 |
57
| public TokenStream tokenStream(String fieldName, Reader reader)
|
17 |
| { |
18 |
0
| if ("labels".equals(fieldName)) return new WhitespaceTokenizer(reader);
|
19 |
57
| if ("point".equals(fieldName))
|
20 |
| { |
21 |
3
| TokenStream result = new PositionTokenizer(reader);
|
22 |
3
| Token token;
|
23 |
3
| try
|
24 |
| { |
25 |
3
| token = result.next();
|
26 |
3
| if (token != null) latitude = Double.parseDouble(token.termText());
|
27 |
3
| token = result.next();
|
28 |
3
| if (token != null) longitude = Double.parseDouble(token.termText());
|
29 |
3
| hasPoint = true;
|
30 |
| } |
31 |
| catch (IOException e) |
32 |
| { |
33 |
0
| throw new RuntimeException("Exception parsing query tokens", e);
|
34 |
| } |
35 |
| |
36 |
3
| return new StandardTokenizer(new StringReader(""));
|
37 |
| } |
38 |
54
| if ("radius".equals(fieldName))
|
39 |
| { |
40 |
2
| TokenStream result = new NumberTokenizer(reader);
|
41 |
2
| Token token;
|
42 |
2
| try
|
43 |
| { |
44 |
2
| token = result.next();
|
45 |
2
| if (token != null) radius = Double.parseDouble(token.termText());
|
46 |
| } |
47 |
| catch (IOException e) |
48 |
| { |
49 |
0
| throw new RuntimeException("Exception parsing query tokens", e);
|
50 |
| } |
51 |
| |
52 |
2
| return new StandardTokenizer(new StringReader(""));
|
53 |
| } |
54 |
52
| if ("zip".equals(fieldName))
|
55 |
| { |
56 |
1
| TokenStream result = new NumberTokenizer(reader);
|
57 |
1
| Token token;
|
58 |
1
| try
|
59 |
| { |
60 |
1
| token = result.next();
|
61 |
1
| if (token != null)
|
62 |
| { |
63 |
1
| SpatialLocation location = ZipCodeManager.getLocation(token.termText());
|
64 |
1
| if (location != null)
|
65 |
| { |
66 |
1
| latitude = location.getLatitudeAsDegrees();
|
67 |
1
| longitude = location.getLongitudeAsDegrees();
|
68 |
1
| hasPoint = true;
|
69 |
1
| if (radius == 0) radius = 2;
|
70 |
| } |
71 |
| } |
72 |
| } |
73 |
| catch (IOException e) |
74 |
| { |
75 |
0
| throw new RuntimeException("Exception parsing query tokens", e);
|
76 |
| } |
77 |
| |
78 |
1
| return new StandardTokenizer(new StringReader(""));
|
79 |
| } |
80 |
47
| if (DocumentFactory.TEXT_FIELD.equals(fieldName)) return super.tokenStream(fieldName, reader);
|
81 |
| |
82 |
4
| return new KeywordTokenizer(reader);
|
83 |
| } |
84 |
| |
85 |
4
| public double getLatitude()
|
86 |
| { |
87 |
4
| return latitude;
|
88 |
| } |
89 |
| |
90 |
4
| public double getLongitude()
|
91 |
| { |
92 |
4
| return longitude;
|
93 |
| } |
94 |
| |
95 |
3
| public double getRadius()
|
96 |
| { |
97 |
3
| return radius;
|
98 |
| } |
99 |
| |
100 |
9
| public boolean hasPoint()
|
101 |
| { |
102 |
9
| return hasPoint;
|
103 |
| } |
104 |
| |
105 |
| |
106 |
| public static class KeywordTokenizer extends CharTokenizer |
107 |
| { |
108 |
4
| public KeywordTokenizer(Reader in)
|
109 |
| { |
110 |
4
| super(in);
|
111 |
| } |
112 |
| |
113 |
28
| public boolean isTokenChar(char c)
|
114 |
| { |
115 |
28
| return true;
|
116 |
| } |
117 |
| } |
118 |
| |
119 |
| public static class PositionTokenizer extends CharTokenizer |
120 |
| { |
121 |
3
| public PositionTokenizer(Reader in)
|
122 |
| { |
123 |
3
| super(in);
|
124 |
| } |
125 |
| |
126 |
30
| public boolean isTokenChar(char c)
|
127 |
| { |
128 |
30
| return !(',' == c);
|
129 |
| } |
130 |
| } |
131 |
| |
132 |
| public static class NumberTokenizer extends CharTokenizer |
133 |
| { |
134 |
3
| public NumberTokenizer(Reader in)
|
135 |
| { |
136 |
3
| super(in);
|
137 |
| } |
138 |
| |
139 |
12
| public boolean isTokenChar(char c)
|
140 |
| { |
141 |
12
| return c == '.' || (c >= '0' && c <= '9');
|
142 |
| } |
143 |
| } |
144 |
| } |