/
WikiSearch.java
176 lines (153 loc) · 4.32 KB
/
WikiSearch.java
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
174
175
176
package com.allendowney.thinkdast;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import redis.clients.jedis.Jedis;
/**
* Represents the results of a search query.
*
*/
public class WikiSearch {
// map from URLs that contain the term(s) to relevance score
private Map<String, Integer> map;
/**
* Constructor.
*
* @param map
*/
public WikiSearch(Map<String, Integer> map) {
this.map = map;
}
/**
* Looks up the relevance of a given URL.
*
* @param url
* @return
*/
public Integer getRelevance(String url) {
Integer relevance = map.get(url);
return relevance==null ? 0: relevance;
}
/**
* Prints the contents in order of term frequency.
*
* @param
*/
private void print() {
List<Entry<String, Integer>> entries = sort();
for (Entry<String, Integer> entry: entries) {
System.out.println(entry);
}
}
/**
* Computes the union of two search results.
*
* @param that
* @return New WikiSearch object.
*/
public WikiSearch or(WikiSearch that) {
Map<String, Integer> union = new HashMap<String, Integer>(map);
for (String term: that.map.keySet()) {
int relevance = totalRelevance(this.getRelevance(term), that.getRelevance(term));
union.put(term, relevance);
}
return new WikiSearch(union);
}
/**
* Computes the intersection of two search results.
*
* @param that
* @return New WikiSearch object.
*/
public WikiSearch and(WikiSearch that) {
Map<String, Integer> intersection = new HashMap<String, Integer>();
for (String term: map.keySet()) {
if (that.map.containsKey(term)) {
int relevance = totalRelevance(this.map.get(term), that.map.get(term));
intersection.put(term, relevance);
}
}
return new WikiSearch(intersection);
}
/**
* Computes the intersection of two search results.
*
* @param that
* @return New WikiSearch object.
*/
public WikiSearch minus(WikiSearch that) {
Map<String, Integer> difference = new HashMap<String, Integer>(map);
for (String term: that.map.keySet()) {
difference.remove(term);
}
return new WikiSearch(difference);
}
/**
* Computes the relevance of a search with multiple terms.
*
* @param rel1: relevance score for the first search
* @param rel2: relevance score for the second search
* @return
*/
protected int totalRelevance(Integer rel1, Integer rel2) {
// simple starting place: relevance is the sum of the term frequencies.
return rel1 + rel2;
}
/**
* Sort the results by relevance.
*
* @return List of entries with URL and relevance.
*/
public List<Entry<String, Integer>> sort() {
// NOTE: this can be done more concisely in Java 8. See
// http://stackoverflow.com/questions/109383/sort-a-mapkey-value-by-values-java
// make a list of entries
List<Entry<String, Integer>> entries =
new LinkedList<Entry<String, Integer>>(map.entrySet());
// make a Comparator object for sorting
Comparator<Entry<String, Integer>> comparator = new Comparator<Entry<String, Integer>>() {
@Override
public int compare(Entry<String, Integer> e1, Entry<String, Integer> e2) {
return e1.getValue().compareTo(e2.getValue());
}
};
// sort and return the entries
Collections.sort(entries, comparator);
return entries;
}
/**
* Performs a search and makes a WikiSearch object.
*
* @param term
* @param index
* @return
*/
public static WikiSearch search(String term, JedisIndex index) {
Map<String, Integer> map = index.getCounts(term);
return new WikiSearch(map);
}
public static void main(String[] args) throws IOException {
// make a JedisIndex
Jedis jedis = JedisMaker.make();
JedisIndex index = new JedisIndex(jedis);
// search for the first term
String term1 = "java";
System.out.println("Query: " + term1);
WikiSearch search1 = search(term1, index);
search1.print();
// search for the second term
String term2 = "programming";
System.out.println("Query: " + term2);
WikiSearch search2 = search(term2, index);
search2.print();
// compute the intersection of the searches
System.out.println("Query: " + term1 + " AND " + term2);
WikiSearch intersection = search1.and(search2);
intersection.print();
}
}