source: trunk/java-client/org/nzdl/gsdl/service/NzdlResultSet.java@ 2316

Last change on this file since 2316 was 2313, checked in by daven, 23 years ago

added method for sorting the term frequency
Map so that we display them in descending order,
most frequent first. I suppose this could be a preference

  • but someone else can do that :-)
  • Property svn:keywords set to Author Date Id Revision
File size: 9.4 KB
Line 
1/*
2 * NzdlResultSet.java
3 * Copyright (C) 2001 New Zealand Digital Library Project
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20//the package we're in
21package org.nzdl.gsdl.service;
22
23import java.util.Set;
24import java.util.Map;
25import java.util.List;
26import java.util.ArrayList;
27import java.util.Map.*;
28import java.util.HashMap;
29import java.util.TreeMap;
30import java.util.SortedMap;
31import java.util.TreeSet;
32import java.util.HashSet;
33import java.util.ListIterator;
34import java.util.Iterator;
35import java.util.Comparator;
36
37/**
38 * An object that holds results extracted from a response by
39 * {@link NzdlResponse#getResultSet() NzdlResponse.getResultSet()}.
40 * Easily interrogated by a user.
41 * @author Stuart Yeates ([email protected])
42 * @author Aziz Mahoui ([email protected])
43 * @author Gordon Paynter ([email protected])
44 * @author Brett Sheeran ([email protected]) (comments)
45 */
46public class NzdlResultSet extends java.lang.Object {
47
48 private List m_resultSet = null;
49 private long m_numDocs = -1;
50 private long m_resultType = -1;
51 private List m_queryTerms = null;
52 private List m_docIDs = null;
53
54 /* The key is the query term, the value its frequency */
55 private TreeMap m_frequencies = null;
56
57 /*
58 * The key is the docID, the value is the doc corresponding
59 * meta data which is also a Map where the key is the meta tag
60 * and the value is a set of values for that tag
61 */
62 private Map m_docToMetaDataMap = null;
63
64 /**
65 * Creates an empty instance of NzdlResultSet.
66 */
67 public NzdlResultSet() {
68 m_resultSet = new ArrayList();
69 m_docToMetaDataMap = new HashMap();
70 m_docIDs = new ArrayList();
71 }
72
73 public String getTermFrequencyString() {
74 return constructTermFrequencyString(null);
75 }
76
77 /*
78 * display term frequency results in descending frequency order
79 */
80
81 public String getOrderedTermFrequencyString() {
82 class DescendingOrder implements Comparator {
83 public int compare(Object o1, Object o2) {
84 return -((Comparable)o1).compareTo(o2);
85 }
86 }
87 return constructTermFrequencyString(new DescendingOrder());
88 }
89
90
91 private String constructTermFrequencyString(Comparator comparator) {
92
93 TreeMap freqOrder = new TreeMap(comparator);
94 Iterator iter = m_frequencies.entrySet().iterator();
95 while (iter.hasNext()) {
96 Map.Entry mapEntry = (Map.Entry) iter.next();
97 freqOrder.put( mapEntry.getValue(), mapEntry.getKey());
98 }
99
100 StringBuffer buf = new StringBuffer();
101 iter = freqOrder.keySet().iterator();
102 while(iter.hasNext()) {
103 Object next = iter.next();
104 System.err.println("" + next);
105 buf.append(freqOrder.get(next)).append(':').append(next).append(' ').append(' ');
106 }
107 return buf.toString();
108 }
109
110 /**
111 * Adds a {@link NzdlQueryHit NzdlQueryHit} to a result set.
112 * @param hit - is an instance of NzdlQueryHit.
113 */
114 public void add( NzdlQueryHit _hit ) {
115 m_resultSet.add( _hit ) ;
116 String id = _hit.getDocumentID();
117 m_docIDs.add( id );
118 m_docToMetaDataMap.put( id, _hit.getMetaData() );
119 }
120
121 /**
122 * Sets the number of documents in a NzdlResultSet.
123 * @param num the number of documents in the result set
124 */
125 public void setNumOfDocs( long _num ) {
126 m_numDocs = _num;
127 }
128
129
130 /**
131 * Sets the result type.
132 * @param type an integer representing the result type.
133 */
134 public void setResultType( long _type ) {
135 m_resultType = _type;
136 }
137
138 /**
139 * Sets the Query terms of a NzdlResultSet.
140 * @param terms a list of words within the query string.
141 */
142 public void setQueryTerms( List _terms ) {
143 m_queryTerms = _terms;
144 }
145
146// public void setTermFrequency( String _term, int _freq ) {
147// m_frequencies.put( _term, new Integer(_freq) );
148// }
149
150 /**
151 * Sets the term frequencies within a NzdlResultSet.
152 * @param freqs a map of the term frequencies. The map key is the query term.
153 */
154 public void setTermFrequencies( TreeMap _freqs ) {
155 m_frequencies = _freqs;
156 }
157
158 /**
159 * Returns the number of documents that matched a query. <br>
160 * Pre: A {@link NzdlResponse NzdlResponse} object has been created. A
161 * {@link NzdlService NzdlService} object has filled the NzdlResponse
162 * object with results from servicing a {@link NzdlRequest NzdlRequest}.<br>
163 * @return The number of documents that matched a query. This is limited
164 * by the maxDocs set in {@link NzdlQuery NzdlQuery}
165 */
166 public long getNumOfDocs() {
167 return m_numDocs;
168 }
169
170 /**
171 * Returns the result type for the NzdlResultSet. <br>
172 * Pre: A {@link NzdlResponse NzdlResponse} object has been created. A
173 * {@link NzdlService NzdlService} object has filled the NzdlResponse
174 * object with results from servicing a {@link NzdlRequest NzdlRequest}.<br>
175 * @return result type as an integer.
176 */
177 public long getResultType() {
178 return m_resultType;
179 }
180
181 /**
182 * Returns a list of the words within the query string. <br>
183 * Pre: A {@link NzdlResponse NzdlResponse} object has been created. A
184 * {@link NzdlService NzdlService} object has filled the NzdlResponse
185 * object with results from servicing a {@link NzdlRequest NzdlRequest}.<br>
186 * @return A list of words within the query string.
187 */
188 public List getQueryTerms() {
189 return m_queryTerms;
190 }
191
192 /**
193 * Returns the number of hits for each word within the query string. <br>
194 * Pre: A {@link NzdlResponse NzdlResponse} object has been created. A
195 * {@link NzdlService NzdlService} object has filled the NzdlResponse
196 * object with results from servicing a {@link NzdlRequest NzdlRequest}.<br>
197 * @return A map of the frequency of each query word. The map key is the query term..
198 */
199 public Map getTermFrequencies() {
200 return m_frequencies;
201 }
202
203// public int getTermFrequency( String _term ) {
204// return ((Integer)m_frequencies.get(_term)).intValue();
205// }
206
207// public List getDocumentIDs() {
208// NzdlQueryHit hit = null;
209// //Set docIDs = new HashSet();
210// List docIDs = new ArrayList();
211// for (ListIterator itr = m_resultSet.listIterator(); itr.hasNext(); ) {
212// hit = (NzdlQueryHit) itr.next();
213// docIDs.add( hit.getDocumentID() );
214// }
215// return docIDs;
216// }
217
218 /**
219 * Returns a list of Document ID's in the NzdlResultSet. <br>
220 * Pre: A {@link NzdlResponse NzdlResponse} object has been created. A
221 * {@link NzdlService NzdlService} object has filled the NzdlResponse
222 * object with results from servicing a {@link NzdlRequest NzdlRequest}.<br>
223 * @return A list of Document ID's in the NzdlResultSet.
224 */
225 public List getDocumentIDs() {
226 return m_docIDs;
227 }
228
229// public Set getMetaData( String _docID, String _metaTag ) {
230// NzdlQueryHit hit = null;
231// for (ListIterator itr = m_resultSet.listIterator(); itr.hasNext(); ) {
232// hit = (NzdlQueryHit) itr.next();
233// if ( _docID.equals( hit.getDocumentID() ) ) {
234// return hit.getMetaDataValues( _metaTag );
235// }
236// }
237// return null;
238// }
239
240 /**
241 * Returns metatag values for a document. <br>
242 * Pre: A {@link NzdlResponse NzdlResponse} object has been created. A
243 * {@link NzdlService NzdlService} object has filled the NzdlResponse
244 * object with results from servicing a {@link NzdlRequest NzdlRequest}.<br>
245 * @return A map of metatag values in the specified document. The map key
246 * is the metatag field such as Author or Title.
247 */
248 public Set getMetaData( String _docID, String _metaTag ) {
249 Map metaData = (Map) m_docToMetaDataMap.get( _docID );
250 if (metaData != null)
251 return (Set) metaData.get( _metaTag );
252 else
253 return new HashSet();
254 }
255
256 /**
257 * Returns all values for a particular metatag in the result set. <br>
258 * Pre: A {@link NzdlResponse NzdlResponse} object has been created. A
259 * {@link NzdlService NzdlService} object has filled the NzdlResponse
260 * object with results from servicing a {@link NzdlRequest NzdlRequest}.<br>
261 * @param metaTag Descriptive data such as Author, Title or Date.
262 * @return A map of all values for a particular metatag in all documents
263 * in the NzdlResultSet. The map key is document ID.
264 */
265 public Map getMetaData( String _metaTag ) {
266 Map metaData = new HashMap();
267 for (ListIterator itr = m_resultSet.listIterator(); itr.hasNext(); ) {
268 NzdlQueryHit hit = (NzdlQueryHit) itr.next();
269 metaData.put( hit.getDocumentID(), hit.getMetaDataValues(_metaTag) );
270 }
271 return metaData;
272 }
273
274 /**
275 * Returns all values for all metatags in the result set. <br>
276 * Pre: A {@link NzdlResponse NzdlResponse} object has been created. A
277 * {@link NzdlService NzdlService} object has filled the NzdlResponse
278 * object with results from servicing a {@link NzdlRequest NzdlRequest}.<br>
279 * @return The metatag values for all metatags in all documents
280 * in the NzdlResultSet. The map key is document ID.
281 */
282 public Map getAllMetaData( ) {
283 return m_docToMetaDataMap;
284 }
285
286}
Note: See TracBrowser for help on using the repository browser.