changeset 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
files testapp/Loader.cpp
diffstat 1 files changed, 95 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/testapp/Loader.cpp	Thu Feb 11 17:50:08 2010 +0000
+++ b/testapp/Loader.cpp	Fri Feb 12 16:56:29 2010 +0000
@@ -33,6 +33,78 @@
     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();
@@ -52,16 +124,37 @@
 
     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->storeObjects(root);
+//    outmapper->storeObjectTree(root);
+    outmapper->storeAllObjects(composers);
     delete outmapper;
 
     std::cerr << "stored, saving..." << std::endl;
@@ -69,5 +162,6 @@
     outstore->save("test-output.ttl");
 
     delete outstore;
+*/
 }