summaryrefslogtreecommitdiff
path: root/tools/dataProvider.h
blob: 348d15cb45c8edfa45e73c606ec7bcbcfc93cfbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#ifndef DATAPROV_H
#define DATAPROV_H

#include<Eigen/Dense>
#include "../tools/easylogging++.h"
#include<vector>

// TODO decide how to construct training data
// One possible way for training data:
//  Matrix composed of an array of feature vectors
//  Labels are composed of linked list, such as
//      6,3,4,0,5,0,0
//      =>  0->6 | 1->3 | 2->4->5
//  How to compensate for non exhaustive labeling?
//      Use -1 to indicate not yet labeled data
//      -1s will be excluded from training

typedef struct DataEntry{
    std::string qid;
    double rank;
    Eigen::VectorXd feature;
} DataEntry;

class DataList{
private:
    int n;
    std::vector<DataEntry*> data;
public:
    unsigned long getSize(){return data.size();}
    void addEntry(DataEntry* d){data.push_back(d);}
    void setfSize(int fsize){n=fsize;}
    int getfSize(){return n;}
    void clear(){
        for (int i=0;i<data.size();++i)
            delete data[i];
        data.clear();
    }
    static DataEntry* copyEntry(DataEntry* d)
    {
        DataEntry* dat = new DataEntry;
        dat->rank = d->rank;
        dat->qid = d->qid;
        dat->feature.resize(d->feature.rows());
        for (int i=0;i<d->feature.rows();++i)
        {
            dat->feature(i)=d->feature(i);
        }
        return dat;
    }
    inline std::vector<DataEntry*>& getData(){
        return data;
    }
    ~DataList(){
        clear();
    }
};

class RidList{
private:
    int n;
    std::vector<DataEntry*> uniq;
    std::vector<DataEntry*> other;
public:
    static bool single;
    void clear(){
        uniq.clear();
        other.clear();
    }
    void setfSize(int fsize){n=fsize;}
    inline int getfSize(){return n;}
    void addEntry(DataEntry* d){
        int ext=false;
        if (d->qid=="-1")
            other.push_back(d);
        for (int i=0;i<uniq.size();++i)
            if (uniq[i]->qid==d->qid)
            {
                ext = true;
                d->rank = i;
            }
        if (ext)
            other.push_back(d);
        else
            uniq.push_back(d);
    }
    inline DataEntry* getU(int x)
    {
        return uniq[x];
    }
    inline DataEntry* getO(int x)
    {
        return other[x];
    }
    inline std::string getQid(int x)
    {
        int a,b,n=getqSize();
        a=x/n;
        return getU(a)->qid;
    }
    inline int getqSize()
    {
        if (single)
            return (int)other.size();
        return (int)(uniq.size()+other.size()-1);
    }
    inline int getuSize()
    {
        return (int)uniq.size();
    }
    inline int getSize()
    {
        return getuSize()*getqSize();
    }
    inline Eigen::VectorXd getVec(int x){
        int a,b,n=getqSize();
        a=x/n;
        b=x%n;
        Eigen::VectorXd vec;
        if (single)
            return (uniq[a]->feature-other[b]->feature).cwiseAbs();
        if (b<a)
            vec=uniq[a]->feature-uniq[b]->feature;
        else
        if (b<uniq.size()-1)
            vec=uniq[a]->feature-uniq[b+1]->feature;
        else
            vec=uniq[a]->feature-other[b-uniq.size()+1]->feature;
        return vec.cwiseAbs();
    };
    inline double getL(int x){
        int a,b,n=getqSize();
        a=x/n;
        b=x%n;
        if (single)
        {
            if (std::fabs(other[b]->rank - a) < 1e-5)
                return 1;
            return -1;
        }
        if (b<uniq.size()-1)
            return -1;
        else
        if (std::fabs(other[b-uniq.size()+1]->rank - a) < 1e-5)
            return 1;
        return -1;
    };
};

class DataProvider  //Virtual base class for data input
{
protected:
    bool eof;
public:
    DataProvider():eof(false){};

    bool EOFile(){return eof;}
    virtual void getAllDataSet(RidList &out) = 0;
    virtual int getDataSet(DataList &out) = 0;
    virtual int open()=0;
    virtual int close()=0;
};

#endif