PageRenderTime 35ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/solr/solrj/src/java/org/apache/solr/client/solrj/response/SpellCheckResponse.java

http://github.com/apache/lucene-solr
Java | 272 lines | 194 code | 39 blank | 39 comment | 30 complexity | 9849e295cf9efe954181109269c111f8 MD5 | raw file
Possible License(s): LGPL-2.1, CPL-1.0, MPL-2.0-no-copyleft-exception, JSON, Apache-2.0, AGPL-1.0, GPL-2.0, GPL-3.0, MIT, BSD-3-Clause
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.solr.client.solrj.response;
  18. import org.apache.solr.common.util.NamedList;
  19. import java.util.ArrayList;
  20. import java.util.LinkedHashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. /**
  24. * Encapsulates responses from SpellCheckComponent
  25. *
  26. *
  27. * @since solr 1.3
  28. */
  29. public class SpellCheckResponse {
  30. private boolean correctlySpelled;
  31. private List<Collation> collations;
  32. private List<Suggestion> suggestions = new ArrayList<>();
  33. Map<String, Suggestion> suggestionMap = new LinkedHashMap<>();
  34. public SpellCheckResponse(NamedList<Object> spellInfo) {
  35. @SuppressWarnings("unchecked")
  36. NamedList<Object> sugg = (NamedList<Object>) spellInfo.get("suggestions");
  37. if (sugg == null) {
  38. correctlySpelled = true;
  39. return;
  40. }
  41. for (int i = 0; i < sugg.size(); i++) {
  42. String n = sugg.getName(i);
  43. @SuppressWarnings("unchecked")
  44. Suggestion s = new Suggestion(n, (NamedList<Object>) sugg.getVal(i));
  45. suggestionMap.put(n, s);
  46. suggestions.add(s);
  47. }
  48. Boolean correctlySpelled = (Boolean) spellInfo.get("correctlySpelled");
  49. if (correctlySpelled != null) {
  50. this.correctlySpelled = correctlySpelled;
  51. }
  52. @SuppressWarnings("unchecked")
  53. NamedList<Object> coll = (NamedList<Object>) spellInfo.get("collations");
  54. if (coll != null) {
  55. // The 'collationInternalRank' values are ignored so we only care 'collation's.
  56. List<Object> collationInfo = coll.getAll("collation");
  57. collations = new ArrayList<>(collationInfo.size());
  58. for (Object o : collationInfo) {
  59. if (o instanceof String) {
  60. collations.add(new Collation()
  61. .setCollationQueryString((String) o));
  62. } else if (o instanceof NamedList) {
  63. @SuppressWarnings("unchecked")
  64. NamedList<Object> expandedCollation = (NamedList<Object>) o;
  65. String collationQuery
  66. = (String) expandedCollation.get("collationQuery");
  67. long hits = ((Number) expandedCollation.get("hits")).longValue();
  68. @SuppressWarnings("unchecked")
  69. NamedList<String> misspellingsAndCorrections
  70. = (NamedList<String>) expandedCollation.get("misspellingsAndCorrections");
  71. Collation collation = new Collation();
  72. collation.setCollationQueryString(collationQuery);
  73. collation.setNumberOfHits(hits);
  74. for (int ii = 0; ii < misspellingsAndCorrections.size(); ii++) {
  75. String misspelling = misspellingsAndCorrections.getName(ii);
  76. String correction = misspellingsAndCorrections.getVal(ii);
  77. collation.addMisspellingsAndCorrection(new Correction(
  78. misspelling, correction));
  79. }
  80. collations.add(collation);
  81. } else {
  82. throw new AssertionError(
  83. "Should get Lists of Strings or List of NamedLists here.");
  84. }
  85. }
  86. }
  87. }
  88. public boolean isCorrectlySpelled() {
  89. return correctlySpelled;
  90. }
  91. public List<Suggestion> getSuggestions() {
  92. return suggestions;
  93. }
  94. public Map<String, Suggestion> getSuggestionMap() {
  95. return suggestionMap;
  96. }
  97. public Suggestion getSuggestion(String token) {
  98. return suggestionMap.get(token);
  99. }
  100. public String getFirstSuggestion(String token) {
  101. Suggestion s = suggestionMap.get(token);
  102. if (s==null || s.getAlternatives().isEmpty()) return null;
  103. return s.getAlternatives().get(0);
  104. }
  105. /**
  106. * <p>
  107. * Return the first collated query string. For convenience and backwards-compatibility. Use getCollatedResults() for full data.
  108. * </p>
  109. * @return first collated query string
  110. */
  111. public String getCollatedResult() {
  112. return collations==null || collations.size()==0 ? null : collations.get(0).collationQueryString;
  113. }
  114. /**
  115. * <p>
  116. * Return all collations.
  117. * Will include # of hits and misspelling-to-correction details if "spellcheck.collateExtendedResults was true.
  118. * </p>
  119. * @return all collations
  120. */
  121. public List<Collation> getCollatedResults() {
  122. return collations;
  123. }
  124. public static class Suggestion {
  125. private String token;
  126. private int numFound;
  127. private int startOffset;
  128. private int endOffset;
  129. private int originalFrequency;
  130. private List<String> alternatives = new ArrayList<>();
  131. private List<Integer> alternativeFrequencies;
  132. public Suggestion(String token, NamedList<Object> suggestion) {
  133. this.token = token;
  134. for (int i = 0; i < suggestion.size(); i++) {
  135. String n = suggestion.getName(i);
  136. if ("numFound".equals(n)) {
  137. numFound = (Integer) suggestion.getVal(i);
  138. } else if ("startOffset".equals(n)) {
  139. startOffset = (Integer) suggestion.getVal(i);
  140. } else if ("endOffset".equals(n)) {
  141. endOffset = (Integer) suggestion.getVal(i);
  142. } else if ("origFreq".equals(n)) {
  143. originalFrequency = (Integer) suggestion.getVal(i);
  144. } else if ("suggestion".equals(n)) {
  145. @SuppressWarnings("unchecked")
  146. List list = (List)suggestion.getVal(i);
  147. if (list.size() > 0 && list.get(0) instanceof NamedList) {
  148. // extended results detected
  149. @SuppressWarnings("unchecked")
  150. List<NamedList> extended = (List<NamedList>)list;
  151. alternativeFrequencies = new ArrayList<>();
  152. for (NamedList nl : extended) {
  153. alternatives.add((String)nl.get("word"));
  154. alternativeFrequencies.add((Integer)nl.get("freq"));
  155. }
  156. } else {
  157. @SuppressWarnings("unchecked")
  158. List<String> alts = (List<String>) list;
  159. alternatives.addAll(alts);
  160. }
  161. }
  162. }
  163. }
  164. public String getToken() {
  165. return token;
  166. }
  167. public int getNumFound() {
  168. return numFound;
  169. }
  170. public int getStartOffset() {
  171. return startOffset;
  172. }
  173. public int getEndOffset() {
  174. return endOffset;
  175. }
  176. public int getOriginalFrequency() {
  177. return originalFrequency;
  178. }
  179. /** The list of alternatives */
  180. public List<String> getAlternatives() {
  181. return alternatives;
  182. }
  183. /** The frequencies of the alternatives in the corpus, or null if this information was not returned */
  184. public List<Integer> getAlternativeFrequencies() {
  185. return alternativeFrequencies;
  186. }
  187. }
  188. public static class Collation {
  189. private String collationQueryString;
  190. private List<Correction> misspellingsAndCorrections = new ArrayList<>();
  191. private long numberOfHits;
  192. public long getNumberOfHits() {
  193. return numberOfHits;
  194. }
  195. public void setNumberOfHits(long numberOfHits) {
  196. this.numberOfHits = numberOfHits;
  197. }
  198. public String getCollationQueryString() {
  199. return collationQueryString;
  200. }
  201. public Collation setCollationQueryString(String collationQueryString) {
  202. this.collationQueryString = collationQueryString;
  203. return this;
  204. }
  205. public List<Correction> getMisspellingsAndCorrections() {
  206. return misspellingsAndCorrections;
  207. }
  208. public Collation addMisspellingsAndCorrection(Correction correction) {
  209. this.misspellingsAndCorrections.add(correction);
  210. return this;
  211. }
  212. }
  213. public static class Correction {
  214. private String original;
  215. private String correction;
  216. public Correction(String original, String correction) {
  217. this.original = original;
  218. this.correction = correction;
  219. }
  220. public String getOriginal() {
  221. return original;
  222. }
  223. public void setOriginal(String original) {
  224. this.original = original;
  225. }
  226. public String getCorrection() {
  227. return correction;
  228. }
  229. public void setCorrection(String correction) {
  230. this.correction = correction;
  231. }
  232. }
  233. }