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

Last change on this file since 2285 was 2283, checked in by say1, 23 years ago

added line showing the query term qrequencies

  • Property svn:keywords set to Author Date Id Revision
File size: 8.6 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.HashMap;
28import java.util.HashSet;
29import java.util.ListIterator;
30import java.util.Iterator;
31
32/**
33 * An object that holds results extracted from a response by
34 * {@link NzdlResponse#getResultSet() NzdlResponse.getResultSet()}.
35 * Easily interrogated by a user.
36 * @author Stuart Yeates ([email protected])
37 * @author Aziz Mahoui ([email protected])
38 * @author Gordon Paynter ([email protected])
39 * @author Brett Sheeran ([email protected]) (comments)
40 */
41public class NzdlResultSet extends java.lang.Object {
42
43 private List m_resultSet = null;
44 private long m_numDocs = -1;
45 private long m_resultType = -1;
46 private List m_queryTerms = null;
47 private List m_docIDs = null;
48
49 /* The key is the query term, the value its frequency */
50 private Map m_frequencies = null;
51
52 /*
53 * The key is the docID, the value is the doc corresponding
54 * meta data which is also a Map where the key is the meta tag
55 * and the value is a set of values for that tag
56 */
57 private Map m_docToMetaDataMap = null;
58
59 /**
60 * Creates an empty instance of NzdlResultSet.
61 */
62 public NzdlResultSet() {
63 m_resultSet = new ArrayList();
64 m_docToMetaDataMap = new HashMap();
65 m_docIDs = new ArrayList();
66 }
67
68 public String getTermFrequencyString() {
69 StringBuffer buf = new StringBuffer();
70 Iterator iter = m_frequencies.keySet().iterator();
71 while(iter.hasNext()) {
72 Object next = iter.next();
73 buf.append(' ').append(next).append(':').
74 append(m_frequencies.get(next));
75 }
76 return buf.toString();
77 }
78
79 /**
80 * Adds a {@link NzdlQueryHit NzdlQueryHit} to a result set.
81 * @param hit - is an instance of NzdlQueryHit.
82 */
83 public void add( NzdlQueryHit _hit ) {
84 m_resultSet.add( _hit ) ;
85 String id = _hit.getDocumentID();
86 m_docIDs.add( id );
87 m_docToMetaDataMap.put( id, _hit.getMetaData() );
88 }
89
90 /**
91 * Sets the number of documents in a NzdlResultSet.
92 * @param num the number of documents in the result set
93 */
94 public void setNumOfDocs( long _num ) {
95 m_numDocs = _num;
96 }
97
98
99 /**
100 * Sets the result type.
101 * @param type an integer representing the result type.
102 */
103 public void setResultType( long _type ) {
104 m_resultType = _type;
105 }
106
107 /**
108 * Sets the Query terms of a NzdlResultSet.
109 * @param terms a list of words within the query string.
110 */
111 public void setQueryTerms( List _terms ) {
112 m_queryTerms = _terms;
113 }
114
115// public void setTermFrequency( String _term, int _freq ) {
116// m_frequencies.put( _term, new Integer(_freq) );
117// }
118
119 /**
120 * Sets the term frequencies within a NzdlResultSet.
121 * @param freqs a map of the term frequencies. The map key is the query term.
122 */
123 public void setTermFrequencies( Map _freqs ) {
124 m_frequencies = _freqs;
125 }
126
127 /**
128 * Returns the number of documents that matched a query. <br>
129 * Pre: A {@link NzdlResponse NzdlResponse} object has been created. A
130 * {@link NzdlService NzdlService} object has filled the NzdlResponse
131 * object with results from servicing a {@link NzdlRequest NzdlRequest}.<br>
132 * @return The number of documents that matched a query. This is limited
133 * by the maxDocs set in {@link NzdlQuery NzdlQuery}
134 */
135 public long getNumOfDocs() {
136 return m_numDocs;
137 }
138
139 /**
140 * Returns the result type for the NzdlResultSet. <br>
141 * Pre: A {@link NzdlResponse NzdlResponse} object has been created. A
142 * {@link NzdlService NzdlService} object has filled the NzdlResponse
143 * object with results from servicing a {@link NzdlRequest NzdlRequest}.<br>
144 * @return result type as an integer.
145 */
146 public long getResultType() {
147 return m_resultType;
148 }
149
150 /**
151 * Returns a list of the words within the query string. <br>
152 * Pre: A {@link NzdlResponse NzdlResponse} object has been created. A
153 * {@link NzdlService NzdlService} object has filled the NzdlResponse
154 * object with results from servicing a {@link NzdlRequest NzdlRequest}.<br>
155 * @return A list of words within the query string.
156 */
157 public List getQueryTerms() {
158 return m_queryTerms;
159 }
160
161 /**
162 * Returns the number of hits for each word within the query string. <br>
163 * Pre: A {@link NzdlResponse NzdlResponse} object has been created. A
164 * {@link NzdlService NzdlService} object has filled the NzdlResponse
165 * object with results from servicing a {@link NzdlRequest NzdlRequest}.<br>
166 * @return A map of the frequency of each query word. The map key is the query term..
167 */
168 public Map getTermFrequencies() {
169 return m_frequencies;
170 }
171
172// public int getTermFrequency( String _term ) {
173// return ((Integer)m_frequencies.get(_term)).intValue();
174// }
175
176// public List getDocumentIDs() {
177// NzdlQueryHit hit = null;
178// //Set docIDs = new HashSet();
179// List docIDs = new ArrayList();
180// for (ListIterator itr = m_resultSet.listIterator(); itr.hasNext(); ) {
181// hit = (NzdlQueryHit) itr.next();
182// docIDs.add( hit.getDocumentID() );
183// }
184// return docIDs;
185// }
186
187 /**
188 * Returns a list of Document ID's in the NzdlResultSet. <br>
189 * Pre: A {@link NzdlResponse NzdlResponse} object has been created. A
190 * {@link NzdlService NzdlService} object has filled the NzdlResponse
191 * object with results from servicing a {@link NzdlRequest NzdlRequest}.<br>
192 * @return A list of Document ID's in the NzdlResultSet.
193 */
194 public List getDocumentIDs() {
195 return m_docIDs;
196 }
197
198// public Set getMetaData( String _docID, String _metaTag ) {
199// NzdlQueryHit hit = null;
200// for (ListIterator itr = m_resultSet.listIterator(); itr.hasNext(); ) {
201// hit = (NzdlQueryHit) itr.next();
202// if ( _docID.equals( hit.getDocumentID() ) ) {
203// return hit.getMetaDataValues( _metaTag );
204// }
205// }
206// return null;
207// }
208
209 /**
210 * Returns metatag values for a document. <br>
211 * Pre: A {@link NzdlResponse NzdlResponse} object has been created. A
212 * {@link NzdlService NzdlService} object has filled the NzdlResponse
213 * object with results from servicing a {@link NzdlRequest NzdlRequest}.<br>
214 * @return A map of metatag values in the specified document. The map key
215 * is the metatag field such as Author or Title.
216 */
217 public Set getMetaData( String _docID, String _metaTag ) {
218 Map metaData = (Map) m_docToMetaDataMap.get( _docID );
219 if (metaData != null)
220 return (Set) metaData.get( _metaTag );
221 else
222 return new HashSet();
223 }
224
225 /**
226 * Returns all values for a particular metatag in the result set. <br>
227 * Pre: A {@link NzdlResponse NzdlResponse} object has been created. A
228 * {@link NzdlService NzdlService} object has filled the NzdlResponse
229 * object with results from servicing a {@link NzdlRequest NzdlRequest}.<br>
230 * @param metaTag Descriptive data such as Author, Title or Date.
231 * @return A map of all values for a particular metatag in all documents
232 * in the NzdlResultSet. The map key is document ID.
233 */
234 public Map getMetaData( String _metaTag ) {
235 Map metaData = new HashMap();
236 for (ListIterator itr = m_resultSet.listIterator(); itr.hasNext(); ) {
237 NzdlQueryHit hit = (NzdlQueryHit) itr.next();
238 metaData.put( hit.getDocumentID(), hit.getMetaDataValues(_metaTag) );
239 }
240 return metaData;
241 }
242
243 /**
244 * Returns all values for all metatags in the result set. <br>
245 * Pre: A {@link NzdlResponse NzdlResponse} object has been created. A
246 * {@link NzdlService NzdlService} object has filled the NzdlResponse
247 * object with results from servicing a {@link NzdlRequest NzdlRequest}.<br>
248 * @return The metatag values for all metatags in all documents
249 * in the NzdlResultSet. The map key is document ID.
250 */
251 public Map getAllMetaData( ) {
252 return m_docToMetaDataMap;
253 }
254
255}
Note: See TracBrowser for help on using the repository browser.