To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Revision:

root / testapp / Loader.cpp @ 51:c83cc68483ea

History | View | Annotate | Download (5.11 KB)

1
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2

    
3
#include "Objects.h"
4
#include "EditDistance.h"
5
#include "TypeRegistrar.h"
6

    
7
#include <dataquay/BasicStore.h>
8
#include <dataquay/objectmapper/ObjectLoader.h>
9
#include <dataquay/objectmapper/TypeMapping.h>
10
#include <dataquay/Debug.h>
11

    
12
#include <QTemporaryFile>
13
#include <QMultiMap>
14

    
15
#include <iostream>
16

    
17
#ifdef TURBOT_PROFILER
18
#include <base/Profiler.h>
19
#endif
20

    
21
using namespace Dataquay;
22
using namespace ClassicalData;
23

    
24
bool
25
load(BasicStore *store, QString resourceName)
26
{
27
    QTemporaryFile tf;
28
    if (!tf.open()) return false;
29
    tf.setAutoRemove(true);
30
    QFile f(resourceName);
31
    if (!f.open(QFile::ReadOnly)) return false;
32
    QByteArray buffer;
33
    int bufsiz = 10240;
34
    while (!f.atEnd()) {
35
        buffer = f.read(bufsiz);
36
        tf.write(buffer);
37
    }
38
    tf.close();
39
    std::cerr << "unpacked, importing..." << std::endl;
40
    store->import("file://" + tf.fileName(),
41
                  BasicStore::ImportPermitDuplicates, // fastest mode
42
                  "ntriples");
43
    return true;
44
}
45

    
46
int main(int argc, char **argv)
47
{
48
    BasicStore *store = new BasicStore();
49
    store->setBaseUri(Uri("http://dbtune.org/classical/resource/"));
50
    ObjectLoader *loader = new ObjectLoader(store);
51
    TypeMapping tm;
52

    
53
    TypeRegistrar::registerTypes();
54
    TypeRegistrar::addMappings(store, &tm);
55

    
56
    loader->setTypeMapping(tm);
57

    
58
    if (!load(store, ":data.ntriples")) {
59
        std::cerr << "Failed to unpack and load resource" << std::endl;
60
        return 1;
61
    }
62

    
63
    std::cerr << "imported, mapping..." << std::endl;
64

    
65
//    QObject *root = loader->loadAllObjects(0);
66

    
67
    QObjectList objects;
68
    try {
69
        objects = loader->loadAll();
70
//        std::cerr << "Loaded " << objects.size() << " objects" << std::endl;
71
    } catch (const std::exception &e) {
72
        std::cerr << "mapping failed: " << e.what() << std::endl;
73
        return 1;
74
    }
75

    
76
    delete loader;
77
    delete store;
78

    
79
    QObjectList composers;
80
//    std::cerr << "Known composers:" << std::endl;
81
    foreach (QObject *o, objects) {
82
        Composer *c = qobject_cast<Composer *>(o);
83
        if (c) {
84
            QString sn = c->getSortName(true);
85
            if (sn == "") {
86
                std::cerr << "WARNING: Composer " << c->name().toStdString() << " (URI " << c->property("uri").toString().toStdString() << ") has no sort-name" << std::endl;
87
            } else {
88
//                std::cerr << sn.toStdString() << std::endl;
89
            }
90
            composers.push_back(c);
91
        }
92
    }
93
/*
94
    for (int i = 1; i < argc; ++i) {
95
        QString name = argv[i];
96
        std::cerr << "Name: " << name.toStdString() << std::endl;
97
        QMultiMap<int, QString> matches;
98
        foreach (QObject *o, composers) {
99
            Composer *c = qobject_cast<Composer *>(o);
100
            if (!c) continue;
101
            int value = c->matchFuzzyName(name);
102
            matches.insert(value, c->getSortName(false));
103
        }
104
        for (QMultiMap<int, QString>::const_iterator i = matches.begin();
105
             i != matches.end(); ++i) {
106
            if (i.key() < 0) continue;
107
            std::cerr << "Score: " << i.key() << " for name: " << i.value().toStdString() << std::endl;
108
        }
109
    }
110
*/
111
    while (!std::cin.eof()) {
112
        std::cerr << std::endl << "Enter composer name: ";
113
        std::string s;
114
        getline(std::cin, s);
115
        std::cerr << "[" << s << "]" << std::endl;
116
        QMultiMap<float, QString> matches;
117
        QRegExp sre("[\\., -]+");
118
        QStringList elements = QString::fromStdString(s)
119
            .toLower().split(sre, QString::SkipEmptyParts);
120
        foreach (QObject *o, composers) {
121
            Composer *c = qobject_cast<Composer *>(o);
122
            if (!c) continue;
123
//            float value = c->matchFuzzyName(elements);
124
            float value = c->matchTyping(QString::fromStdString(s));
125
            matches.insert(value, c->getSortName(false));
126
        }
127
        int n = 0;
128
        for (QMultiMap<float, QString>::const_iterator i = matches.end();
129
             i != matches.begin(); ) {
130
            --i;
131
            if (i.key() <= 0) continue;
132
            if (n == 0) {
133
                std::cerr << "Best match:" << std::endl << " * ";
134
            } else if (n == 1) {
135
                std::cerr << "Other candidate(s):" << std::endl << " - ";
136
            } else {
137
                std::cerr << " - ";
138
            }
139
            std::cerr << i.value().toStdString();
140
            for (int c = i.value().length(); c < 40; ++c) std::cerr << " ";
141
            std::cerr << "[" << i.key() << "]" << std::endl;
142
            if (++n > 5) break;
143
        }
144
        if (n == 0) std::cerr << "No matches" << std::endl;
145
    }        
146

    
147
#ifdef TURBOT_PROFILER
148
    Turbot::Profiler::dump();
149
#endif
150

    
151
/*
152
    std::cerr << "mapped, storing again..." << std::endl;
153

154
    // let's try just writing out the composers
155

156
    BasicStore *outstore = new BasicStore();
157
    outstore->setBaseUri("http://dbtune.org/classical/resource/");
158
    ObjectMapper *outmapper = new ObjectMapper(outstore);
159

160
    TypeRegistrar::addMappings(outstore, outmapper);
161

162
//    outmapper->storeObjectTree(root);
163
    outmapper->storeAllObjects(composers);
164
    delete outmapper;
165

166
    std::cerr << "stored, saving..." << std::endl;
167

168
    outstore->save("test-output.ttl");
169

170
    delete outstore;
171
*/
172
}
173