Statistics
| Revision:

root / tmp / org.txm.core / src / groovy / org / txm / functions / mesures / Diversity.groovy @ 187

History | View | Annotate | Download (1.9 kB)

1
package org.txm.functions.mesures;
2

    
3
import java.io.IOException;
4
import java.util.ArrayList;
5
import java.util.Arrays;
6
import java.util.HashSet;
7
import java.util.List;
8

    
9
import org.txm.Toolbox;
10
import org.txm.searchengine.cqp.clientExceptions.CqiClientException;
11
import org.txm.searchengine.cqp.corpus.Corpus;
12
import org.txm.searchengine.cqp.corpus.Property;
13
import org.txm.searchengine.cqp.corpus.QueryResult;
14
import org.txm.searchengine.cqp.corpus.query.Query;
15
import org.txm.searchengine.cqp.serverException.CqiServerError;
16

    
17
public class Diversity extends Mesure {
18

    
19
        private static final long serialVersionUID = -375293299211271669L;
20
        
21
        public static List<Synthese> SUPORTED = Arrays.asList(Synthese.SUM);
22
        private String query;
23
        private String property;
24

    
25
        public Diversity(String domaine, String query, String property) {
26
                this.domaine = domaine;
27
                this.query = query;
28
                this.property = property;
29
        }
30

    
31
        @Override
32
        public boolean compute(ArrayList<Corpus> corpora) {
33
                this.corpora = corpora;
34
                Integer[] rez = new Integer[corpora.size()];
35
                for (int icorpus = 0 ; icorpus < corpora.size() ; icorpus++) {
36
                        try {
37
                                Corpus corpus = corpora.get(icorpus);
38
                                rez[icorpus] = computeForCorpus(corpus);
39
                        } catch (Exception e) {
40
                                // TODO Auto-generated catch block
41
                                e.printStackTrace();
42
                                return false;
43
                        }        
44
                }
45
                this.add(new MesureResult(this, rez, "Diversite: "+query+" "+property));
46
                return true;
47
        }
48
        
49
        private Integer computeForCorpus(Corpus corpus) throws CqiClientException, IOException, CqiServerError {
50
                if (domaine != null) corpus = corpus.createSubcorpus(new Query(domaine), "DOMAINE");
51
                
52
                Property p = corpus.getProperty(property);
53
                QueryResult result = corpus.query(new Query(query), "TMP", false);
54
                int[] starts = result.getStarts();
55
                int[] ids = Toolbox.getCqiClient().cpos2Id(p.getQualifiedName(), starts);
56
                HashSet<Integer> set = new HashSet<Integer>();
57
                for (int i : ids) set.add(i); // no double
58
                return set.size();
59
        }
60
}