001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.impl;
018:
019: import java.util.Arrays;
020: import java.util.Collections;
021: import java.util.Comparator;
022: import java.util.List;
023:
024: import org.compass.core.CompassTermFreq;
025: import org.compass.core.CompassTermFreqsBuilder;
026: import org.compass.core.engine.SearchEngineInternalSearch;
027: import org.compass.core.engine.SearchEngineTermFrequencies;
028: import org.compass.core.mapping.ResourceMapping;
029: import org.compass.core.mapping.ResourcePropertyLookup;
030: import org.compass.core.spi.InternalCompassSession;
031:
032: /**
033: * @author kimchy
034: */
035: public class DefaultCompassTermFreqsBuilder implements
036: CompassTermFreqsBuilder {
037:
038: private InternalCompassSession session;
039:
040: private String[] propertyNames;
041:
042: private int size;
043:
044: private String[] aliases;
045:
046: private String[] subIndexes;
047:
048: private int minNorm = -1;
049:
050: private int maxNorm = -1;
051:
052: private Sort sort = Sort.FREQ;
053:
054: public DefaultCompassTermFreqsBuilder(
055: InternalCompassSession session, String[] names) {
056: this .session = session;
057: this .propertyNames = new String[names.length];
058: for (int i = 0; i < names.length; i++) {
059: ResourcePropertyLookup lookup = session.getMapping()
060: .getResourcePropertyLookup(names[i]);
061: this .propertyNames[i] = lookup.getPath();
062: }
063: this .size = 10;
064: }
065:
066: public CompassTermFreqsBuilder setSize(int size) {
067: this .size = size;
068: return this ;
069: }
070:
071: public CompassTermFreqsBuilder setAliases(String... aliases) {
072: this .aliases = aliases;
073: return this ;
074: }
075:
076: public CompassTermFreqsBuilder setTypes(Class... types) {
077: if (types == null) {
078: this .aliases = null;
079: return this ;
080: }
081: String[] aliases = new String[types.length];
082: for (int i = 0; i < types.length; i++) {
083: ResourceMapping resourceMapping = session.getMapping()
084: .getRootMappingByClass(types[i]);
085: aliases[i] = resourceMapping.getAlias();
086: }
087: setAliases(aliases);
088: return this ;
089: }
090:
091: public CompassTermFreqsBuilder setSubIndexes(String... subIndexes) {
092: this .subIndexes = subIndexes;
093: return this ;
094: }
095:
096: public CompassTermFreqsBuilder normalize(int min, int max) {
097: this .minNorm = min;
098: this .maxNorm = max;
099: return this ;
100: }
101:
102: public CompassTermFreqsBuilder setSort(Sort sort) {
103: this .sort = sort;
104: return this ;
105: }
106:
107: public CompassTermFreq[] toTermFreqs() {
108: SearchEngineInternalSearch internalSearch = session
109: .getSearchEngine().internalSearch(subIndexes, aliases);
110: SearchEngineTermFrequencies seTermFreqs = session
111: .getSearchEngine().termFreq(propertyNames, size,
112: internalSearch);
113: CompassTermFreq[] termFreqs = seTermFreqs.getTerms();
114: if (sort == Sort.TERM) {
115: List list = Arrays.asList(termFreqs);
116: Collections.sort(list, new Comparator() {
117: public int compare(Object o1, Object o2) {
118: return ((CompassTermFreq) o1).getTerm().compareTo(
119: ((CompassTermFreq) o2).getTerm());
120: }
121: });
122: termFreqs = (CompassTermFreq[]) list
123: .toArray(new CompassTermFreq[list.size()]);
124: }
125: if (minNorm != -1 && maxNorm != -1) {
126: int min = Integer.MAX_VALUE;
127: int max = Integer.MIN_VALUE;
128: for (int i = 0; i < termFreqs.length; i++) {
129: if (termFreqs[i].getFreq() < min) {
130: min = (int) termFreqs[i].getFreq();
131: }
132: if (termFreqs[i].getFreq() > max) {
133: max = (int) termFreqs[i].getFreq();
134: }
135: }
136: for (int i = 0; i < termFreqs.length; i++) {
137: float freq;
138: if ((int) termFreqs[i].getFreq() == min) {
139: freq = minNorm;
140: } else if ((int) termFreqs[i].getFreq() == max) {
141: freq = maxNorm;
142: } else {
143: freq = minNorm + (termFreqs[i].getFreq() - min)
144: / (max - min) * (maxNorm - minNorm);
145: }
146: ((DefaultCompassTermFreq) termFreqs[i]).setFreq(freq);
147: }
148: }
149: return termFreqs;
150: }
151: }
|