view 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
line wrap: on
line source

#include "Objects.h"
#include "TypeRegistrar.h"

#include <dataquay/BasicStore.h>
#include <dataquay/objectmapper/ObjectMapper.h>

#include <QTemporaryFile>

#include <iostream>

using namespace Dataquay;
using namespace ClassicalData;

bool
load(BasicStore *store, QString resourceName)
{
    QTemporaryFile tf;
    if (!tf.open()) return false;
    tf.setAutoRemove(true);
    QFile f(resourceName);
    if (!f.open(QFile::ReadOnly)) return false;
    QByteArray buffer;
    int bufsiz = 10240;
    while (!f.atEnd()) {
	buffer = f.read(bufsiz);
	tf.write(buffer);
    }
    std::cerr << "unpacked, importing..." << std::endl;
    store->import("file://" + tf.fileName(),
		  BasicStore::ImportPermitDuplicates, // fastest mode
		  "ntriples");
    return true;
}

//!!! both nasty and duplicated from Import.cpp

QString makeNameKey(QString name)
{
    QString key = name.toLower()
        .replace("'", "")
        .replace("x", "ks")
        .replace("y", "i")
        .replace("k", "c")
        .replace("ch", "c")
        .replace("cc", "c")
        .replace("v", "f")
        .replace("ff", "f")
        .replace("th", "t")
        .replace("tch", "ch")
        .replace("er", "r");
//    DEBUG << "makeNameKey(" << name << "): " << key << endl;
    return key;
}

bool namesFuzzyMatch(QString an, Composer *b)
{
    // ew!

    QString bn = b->name();
    if (bn == an) return true;
    if (b->aliases().contains(an)) return true;
    int aSurnameIndex = 0, bSurnameIndex = 0;
    if (an.contains(",")) {
        an.replace(",", "");
    } else {
        aSurnameIndex = -1;
    }
    if (bn.contains(",")) {
        bn.replace(",", "");
    } else {
        bSurnameIndex = -1;
    }
    QStringList nl = an.split(QRegExp("[ -]"));
    QStringList bnl = makeNameKey(bn).split(QRegExp("[ -]"));
    int matchCount = 0;
    QString surnameMatch = "";
    if (aSurnameIndex == -1) aSurnameIndex = nl.size()-1;
    if (bSurnameIndex == -1) bSurnameIndex = bnl.size()-1;
    if (nl[aSurnameIndex][0].isUpper() &&
        nl[aSurnameIndex] != "Della" &&
        makeNameKey(nl[aSurnameIndex]) == bnl[bSurnameIndex]) {
        surnameMatch = nl[aSurnameIndex];
    }
    int tested = 0;
    foreach (QString elt, nl) {
        if (!elt[0].isUpper() || elt == "Della") continue;
        QString k = makeNameKey(elt);
        if (bnl.contains(k)) {
            ++matchCount;
        }
        if (++tested == 2 && matchCount == 0) {
            return false;
        }
    }
    if (surnameMatch != "") {
//        DEBUG << "namesFuzzyMatch: note: surnameMatch = " << surnameMatch << endl;
        if (matchCount > 1) {
            return true;
        } else {
//            DEBUG << "(but not enough else matched)" << endl;
            return false;
        }
    }
    return false;
}

int main(int argc, char **argv)
{
    BasicStore *store = new BasicStore();
    store->setBaseUri("http://dbtune.org/classical/resource/");
    ObjectMapper *mapper = new ObjectMapper(store);

    TypeRegistrar::addMappings(store, mapper);

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

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

    QObject *root = mapper->loadAllObjects(0);

    delete mapper;
    delete store;
    
    QObjectList composers;
    foreach (QObject *o, root->children()) {
	if (qobject_cast<Composer *>(o)) composers.push_back(o);
    }
    
    if (argc > 1) {
	QString name = argv[1];
	std::cerr << "Name: " << name.toStdString() << std::endl;
	foreach (QObject *o, composers) {
	    Composer *c = qobject_cast<Composer *>(o);
	    if (!c) continue;
	    if (namesFuzzyMatch(name, c)) {
		std::cerr << "Matches: " << c->name().toStdString() << std::endl;
	    }
	}
    }

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

    // let's try just writing out the composers

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

    TypeRegistrar::addMappings(outstore, outmapper);

//    outmapper->storeObjectTree(root);
    outmapper->storeAllObjects(composers);
    delete outmapper;

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

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

    delete outstore;
*/
}