annotate testapp/Loader.cpp @ 9:9e2b203254ab classical-rdf

* test matching of composer names
author Chris Cannam
date Fri, 12 Feb 2010 16:56:29 +0000
parents 71cf328c2a9d
children d35e5d769c87
rev   line source
Chris@7 1
Chris@7 2 #include "Objects.h"
Chris@7 3 #include "TypeRegistrar.h"
Chris@7 4
Chris@7 5 #include <dataquay/BasicStore.h>
Chris@7 6 #include <dataquay/objectmapper/ObjectMapper.h>
Chris@7 7
Chris@7 8 #include <QTemporaryFile>
Chris@7 9
Chris@7 10 #include <iostream>
Chris@7 11
Chris@7 12 using namespace Dataquay;
Chris@7 13 using namespace ClassicalData;
Chris@7 14
Chris@7 15 bool
Chris@7 16 load(BasicStore *store, QString resourceName)
Chris@7 17 {
Chris@7 18 QTemporaryFile tf;
Chris@7 19 if (!tf.open()) return false;
Chris@7 20 tf.setAutoRemove(true);
Chris@7 21 QFile f(resourceName);
Chris@7 22 if (!f.open(QFile::ReadOnly)) return false;
Chris@7 23 QByteArray buffer;
Chris@7 24 int bufsiz = 10240;
Chris@7 25 while (!f.atEnd()) {
Chris@7 26 buffer = f.read(bufsiz);
Chris@7 27 tf.write(buffer);
Chris@7 28 }
Chris@7 29 std::cerr << "unpacked, importing..." << std::endl;
Chris@7 30 store->import("file://" + tf.fileName(),
Chris@7 31 BasicStore::ImportPermitDuplicates, // fastest mode
Chris@7 32 "ntriples");
Chris@7 33 return true;
Chris@7 34 }
Chris@7 35
Chris@9 36 //!!! both nasty and duplicated from Import.cpp
Chris@9 37
Chris@9 38 QString makeNameKey(QString name)
Chris@9 39 {
Chris@9 40 QString key = name.toLower()
Chris@9 41 .replace("'", "")
Chris@9 42 .replace("x", "ks")
Chris@9 43 .replace("y", "i")
Chris@9 44 .replace("k", "c")
Chris@9 45 .replace("ch", "c")
Chris@9 46 .replace("cc", "c")
Chris@9 47 .replace("v", "f")
Chris@9 48 .replace("ff", "f")
Chris@9 49 .replace("th", "t")
Chris@9 50 .replace("tch", "ch")
Chris@9 51 .replace("er", "r");
Chris@9 52 // DEBUG << "makeNameKey(" << name << "): " << key << endl;
Chris@9 53 return key;
Chris@9 54 }
Chris@9 55
Chris@9 56 bool namesFuzzyMatch(QString an, Composer *b)
Chris@9 57 {
Chris@9 58 // ew!
Chris@9 59
Chris@9 60 QString bn = b->name();
Chris@9 61 if (bn == an) return true;
Chris@9 62 if (b->aliases().contains(an)) return true;
Chris@9 63 int aSurnameIndex = 0, bSurnameIndex = 0;
Chris@9 64 if (an.contains(",")) {
Chris@9 65 an.replace(",", "");
Chris@9 66 } else {
Chris@9 67 aSurnameIndex = -1;
Chris@9 68 }
Chris@9 69 if (bn.contains(",")) {
Chris@9 70 bn.replace(",", "");
Chris@9 71 } else {
Chris@9 72 bSurnameIndex = -1;
Chris@9 73 }
Chris@9 74 QStringList nl = an.split(QRegExp("[ -]"));
Chris@9 75 QStringList bnl = makeNameKey(bn).split(QRegExp("[ -]"));
Chris@9 76 int matchCount = 0;
Chris@9 77 QString surnameMatch = "";
Chris@9 78 if (aSurnameIndex == -1) aSurnameIndex = nl.size()-1;
Chris@9 79 if (bSurnameIndex == -1) bSurnameIndex = bnl.size()-1;
Chris@9 80 if (nl[aSurnameIndex][0].isUpper() &&
Chris@9 81 nl[aSurnameIndex] != "Della" &&
Chris@9 82 makeNameKey(nl[aSurnameIndex]) == bnl[bSurnameIndex]) {
Chris@9 83 surnameMatch = nl[aSurnameIndex];
Chris@9 84 }
Chris@9 85 int tested = 0;
Chris@9 86 foreach (QString elt, nl) {
Chris@9 87 if (!elt[0].isUpper() || elt == "Della") continue;
Chris@9 88 QString k = makeNameKey(elt);
Chris@9 89 if (bnl.contains(k)) {
Chris@9 90 ++matchCount;
Chris@9 91 }
Chris@9 92 if (++tested == 2 && matchCount == 0) {
Chris@9 93 return false;
Chris@9 94 }
Chris@9 95 }
Chris@9 96 if (surnameMatch != "") {
Chris@9 97 // DEBUG << "namesFuzzyMatch: note: surnameMatch = " << surnameMatch << endl;
Chris@9 98 if (matchCount > 1) {
Chris@9 99 return true;
Chris@9 100 } else {
Chris@9 101 // DEBUG << "(but not enough else matched)" << endl;
Chris@9 102 return false;
Chris@9 103 }
Chris@9 104 }
Chris@9 105 return false;
Chris@9 106 }
Chris@9 107
Chris@7 108 int main(int argc, char **argv)
Chris@7 109 {
Chris@7 110 BasicStore *store = new BasicStore();
Chris@7 111 store->setBaseUri("http://dbtune.org/classical/resource/");
Chris@7 112 ObjectMapper *mapper = new ObjectMapper(store);
Chris@7 113
Chris@7 114 TypeRegistrar::addMappings(store, mapper);
Chris@7 115
Chris@7 116 if (!load(store, ":data.ntriples")) {
Chris@7 117 std::cerr << "Failed to unpack and load resource" << std::endl;
Chris@7 118 return 1;
Chris@7 119 }
Chris@7 120
Chris@7 121 std::cerr << "imported, mapping..." << std::endl;
Chris@7 122
Chris@8 123 QObject *root = mapper->loadAllObjects(0);
Chris@8 124
Chris@7 125 delete mapper;
Chris@7 126 delete store;
Chris@9 127
Chris@9 128 QObjectList composers;
Chris@9 129 foreach (QObject *o, root->children()) {
Chris@9 130 if (qobject_cast<Composer *>(o)) composers.push_back(o);
Chris@9 131 }
Chris@9 132
Chris@9 133 if (argc > 1) {
Chris@9 134 QString name = argv[1];
Chris@9 135 std::cerr << "Name: " << name.toStdString() << std::endl;
Chris@9 136 foreach (QObject *o, composers) {
Chris@9 137 Composer *c = qobject_cast<Composer *>(o);
Chris@9 138 if (!c) continue;
Chris@9 139 if (namesFuzzyMatch(name, c)) {
Chris@9 140 std::cerr << "Matches: " << c->name().toStdString() << std::endl;
Chris@9 141 }
Chris@9 142 }
Chris@9 143 }
Chris@8 144
Chris@9 145 /*
Chris@8 146 std::cerr << "mapped, storing again..." << std::endl;
Chris@8 147
Chris@9 148 // let's try just writing out the composers
Chris@9 149
Chris@8 150 BasicStore *outstore = new BasicStore();
Chris@8 151 outstore->setBaseUri("http://dbtune.org/classical/resource/");
Chris@8 152 ObjectMapper *outmapper = new ObjectMapper(outstore);
Chris@8 153
Chris@8 154 TypeRegistrar::addMappings(outstore, outmapper);
Chris@8 155
Chris@9 156 // outmapper->storeObjectTree(root);
Chris@9 157 outmapper->storeAllObjects(composers);
Chris@8 158 delete outmapper;
Chris@8 159
Chris@8 160 std::cerr << "stored, saving..." << std::endl;
Chris@8 161
Chris@8 162 outstore->save("test-output.ttl");
Chris@8 163
Chris@8 164 delete outstore;
Chris@9 165 */
Chris@7 166 }
Chris@7 167