# HG changeset patch # User Chris Cannam # Date 1268760988 0 # Node ID 8bed0545570611c16bf8c7a710a92a169f9fc7e1 # Parent 9729919e589cf48c73c99a34091db45332f2cc02 * Make classical code build diff -r 9729919e589c -r 8bed05455706 common/Matcher.cpp --- a/common/Matcher.cpp Mon Mar 01 16:55:27 2010 +0000 +++ b/common/Matcher.cpp Tue Mar 16 17:36:28 2010 +0000 @@ -9,46 +9,58 @@ namespace ClassicalData { -UriList +ComposerTypingQuickMatcher::ComposerTypingQuickMatcher(QList cl) +{ + foreach (Composer *c, cl) { + m_composers[c->property("uri").value()] = c; + } +} + +GuessList ComposerTypingQuickMatcher::match(QString text, int maxResults) const { - UriList results; + GuessList results; - QMultiMap matches; + QMap matches; foreach (Composer *c, m_composers) { float value = c->matchTypingQuick(text); - matches.insert(value, c); + if (value <= 0) continue; + matches.insert(Guess(value, c), 1); } int n = 0; - for (QMultiMap::const_iterator i = matches.end(); - i != matches.begin(); ) { - --i; - if (i.key() <= 0) continue; - results.push_back(i.value()->property("uri").value()); + for (QMap::const_iterator i = matches.begin(); + i != matches.end(); ++i) { + results.push_back(i.key()); if (++n > maxResults) break; } return results; } -UriList +ComposerTypingThoroughMatcher::ComposerTypingThoroughMatcher(QList cl) +{ + foreach (Composer *c, cl) { + m_composers[c->property("uri").value()] = c; + } +} + +GuessList ComposerTypingThoroughMatcher::match(QString text, int maxResults) const { - UriList results; + GuessList results; - QMultiMap matches; + QMap matches; foreach (Composer *c, m_composers) { float value = c->matchTyping(text); - matches.insert(value, c); + if (value <= 0) continue; + matches.insert(Guess(value, c), 1); } int n = 0; - for (QMultiMap::const_iterator i = matches.end(); - i != matches.begin(); ) { - --i; - if (i.key() <= 0) continue; - results.push_back(i.value()->property("uri").value()); + for (QMap::const_iterator i = matches.begin(); + i != matches.end(); ++i) { + results.push_back(i.key()); if (++n > maxResults) break; } diff -r 9729919e589c -r 8bed05455706 common/Matcher.h --- a/common/Matcher.h Mon Mar 01 16:55:27 2010 +0000 +++ b/common/Matcher.h Tue Mar 16 17:36:28 2010 +0000 @@ -5,43 +5,66 @@ #include +#include + namespace ClassicalData { class Composer; +class NamedEntity; + +class Guess +{ +public: + Guess(float c, NamedEntity *e) : m_confidence(c), m_entity(e) { } + + float confidence() const { return m_confidence; } + void setConfidence(float c) { m_confidence = c; } + + NamedEntity *entity() { return m_entity; } + void setEntity(NamedEntity *e) { m_entity = e; } + + bool operator<(const Guess &g) const { + return (confidence() > g.confidence()); + } + +private: + float m_confidence; + NamedEntity *m_entity; +}; + +typedef QList GuessList; class Matcher { public: - virtual Dataquay::UriList match(QString text, int maxResults) const = 0; + // Results are guaranteed to be returned in order from most to + // least confident + virtual GuessList match(QString text, int maxResults) const = 0; }; class ComposerTypingQuickMatcher : public Matcher { public: - ComposerTypingQuickMatcher(QList cl) : - m_composers(cl) { } - - virtual Dataquay::UriList match(QString text, int maxResults) const; + ComposerTypingQuickMatcher(QList cl); + virtual GuessList match(QString text, int maxResults) const; private: - QList m_composers; + QHash m_composers; }; class ComposerTypingThoroughMatcher : public Matcher { public: - ComposerTypingThoroughMatcher(QList cl) : - m_composers(cl) { } - - virtual Dataquay::UriList match(QString text, int maxResults) const; + ComposerTypingThoroughMatcher(QList cl); + virtual GuessList match(QString text, int maxResults) const; private: - QList m_composers; + QHash m_composers; }; - - } +Q_DECLARE_METATYPE(ClassicalData::Guess*); + #endif diff -r 9729919e589c -r 8bed05455706 common/Objects.h --- a/common/Objects.h Mon Mar 01 16:55:27 2010 +0000 +++ b/common/Objects.h Tue Mar 16 17:36:28 2010 +0000 @@ -520,5 +520,6 @@ Q_DECLARE_METATYPE(ClassicalData::Composer*); Q_DECLARE_METATYPE(ClassicalData::Form*); Q_DECLARE_METATYPE(QSet); +Q_DECLARE_METATYPE(ClassicalData::NamedEntity*); #endif diff -r 9729919e589c -r 8bed05455706 common/common.pro --- a/common/common.pro Mon Mar 01 16:55:27 2010 +0000 +++ b/common/common.pro Tue Mar 16 17:36:28 2010 +0000 @@ -1,7 +1,7 @@ TEMPLATE = lib TARGET = -load(../platform.prf) +load(../../all.prf) CONFIG += staticlib diff -r 9729919e589c -r 8bed05455706 testapp/testapp.pro --- a/testapp/testapp.pro Mon Mar 01 16:55:27 2010 +0000 +++ b/testapp/testapp.pro Tue Mar 16 17:36:28 2010 +0000 @@ -1,13 +1,15 @@ TEMPLATE = app TARGET = testapp -load(../platform.prf) +load(../../all.prf) SOURCES += Loader.cpp RESOURCES += testapp.qrc +INCLUDEPATH += ../common + PRE_TARGETDEPS += ../common/libcommon.a -LIBS += ../common/libcommon.a ../../turbot/dataquay/libdataquay.a ../../turbot/ext/libext.a +LIBS += ../common/libcommon.a ../../../turbot/dataquay/libdataquay.a ../../../turbot/ext/libext.a diff -r 9729919e589c -r 8bed05455706 utilities/composer/composer.cpp --- a/utilities/composer/composer.cpp Mon Mar 01 16:55:27 2010 +0000 +++ b/utilities/composer/composer.cpp Tue Mar 16 17:36:28 2010 +0000 @@ -404,11 +404,14 @@ storer->setTypeMapping(tm); storer->setPropertyStorePolicy(ObjectStorer::StoreIfChanged); - storer->setObjectStorePolicy(ObjectStorer::StoreAllObjects); +// storer->setObjectStorePolicy(ObjectStorer::StoreAllObjects); storer->setBlankNodePolicy(ObjectStorer::NoBlankNodes); cerr << "Mapping results back to store..."; - storer->storeAllObjects(root->children()); +// storer->storeAllObjects(root->children()); +// storer->setFollowPolicy(ObjectStorer::FollowAll); + storer->setFollowPolicy(ObjectStorer::FollowObjectProperties); + storer->store(root->children()); cerr << " done" << endl; cerr << "Saving to file out.ttl..."; diff -r 9729919e589c -r 8bed05455706 utilities/composer/composer.pro --- a/utilities/composer/composer.pro Mon Mar 01 16:55:27 2010 +0000 +++ b/utilities/composer/composer.pro Tue Mar 16 17:36:28 2010 +0000 @@ -2,11 +2,13 @@ TARGET = composer QT -= gui network xml -load(../../platform.prf) +load(../../../all.prf) SOURCES += composer.cpp PRE_TARGETDEPS += ../../common/libcommon.a -LIBS += ../../common/libcommon.a ../../../turbot/dataquay/libdataquay.a ../../../turbot/ext/libext.a +INCLUDEPATH += ../../common +LIBS += ../../common/libcommon.a ../../../../turbot/dataquay/libdataquay.a ../../../../turbot/ext/libext.a + diff -r 9729919e589c -r 8bed05455706 utilities/widgettest/widgettest.cpp --- a/utilities/widgettest/widgettest.cpp Mon Mar 01 16:55:27 2010 +0000 +++ b/utilities/widgettest/widgettest.cpp Tue Mar 16 17:36:28 2010 +0000 @@ -1,17 +1,104 @@ /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ #include "TypingSelectWidget.h" +#include "TypeRegistrar.h" +#include "Objects.h" +#include "Matcher.h" + +#include +#include +#include +#include +#include +#include #include +#include + using namespace ClassicalData; +using namespace Dataquay; +using namespace std; + +ostream &operator<<(ostream &target, const QString &str) +{ + return target << str.toLocal8Bit().data(); +} + +ostream &operator<<(ostream &target, const QUrl &u) +{ + return target << "<" << u.toString() << ">"; +} + +void +usage(const char *name) +{ + int s = 0; + for (int i = 0; name[i]; ++i) if (name[i] == '/') s = i + 1; + name = name + s; + cerr << "Usage:" << endl; + cerr << " " << name << " " << endl; + exit(2); +} + +bool +load(BasicStore *store, QString fileName) +{ + QUrl url = QUrl::fromLocalFile(fileName); + + cerr << "Importing from URL " << url << " ..."; + try { + store->import(url, BasicStore::ImportPermitDuplicates); + } catch (RDFException e) { + cerr << " retrying with explicit ntriples type..."; + try { + store->import(url, BasicStore::ImportPermitDuplicates, "ntriples"); + } catch (RDFException e) { + cerr << "failed" << endl; + cerr << "Import failed: " << e.what() << endl; + return false; + } + } + + cerr << " done" << endl; + return true; +} int main(int argc, char **argv) { QApplication app(argc, argv); + if (argc != 2) usage(argv[0]); + QString inFileName = argv[1]; + + BasicStore *store = new BasicStore(); + store->setBaseUri(Uri("http://dbtune.org/classical/resource/")); + ObjectLoader *loader = new ObjectLoader(store); + + TypeMapping tm; + + TypeRegistrar::registerTypes(); + TypeRegistrar::addMappings(store, &tm); + + loader->setTypeMapping(tm); + + if (!load(store, inFileName)) { + cerr << "Failed to load data source" << endl; + return 1; + } + + cerr << "Imported RDF data, mapping to objects..."; + QObject *root = loader->loadAllObjects(0); + cerr << " done" << endl; + + delete loader; + + QList composers = root->findChildren(); + ComposerTypingThoroughMatcher matcher(composers); + TypingSelectWidget *w = new TypingSelectWidget(); + w->addMatcher(&matcher); w->show(); diff -r 9729919e589c -r 8bed05455706 utilities/widgettest/widgettest.pro --- a/utilities/widgettest/widgettest.pro Mon Mar 01 16:55:27 2010 +0000 +++ b/utilities/widgettest/widgettest.pro Tue Mar 16 17:36:28 2010 +0000 @@ -1,11 +1,13 @@ TEMPLATE = app TARGET = widgettest -load(../../platform.prf) +load(../../../all.prf) PRE_TARGETDEPS += ../../common/libcommon.a ../../widgets/libwidgets.a -LIBS += ../../widgets/libwidgets.a ../../common/libcommon.a ../../../turbot/dataquay/libdataquay.a ../../../turbot/ext/libext.a +INCLUDEPATH += ../../widgets ../../common + +LIBS += ../../widgets/libwidgets.a ../../common/libcommon.a ../../../../turbot/dataquay/libdataquay.a ../../../../turbot/ext/libext.a HEADERS += widgettest.h SOURCES += widgettest.cpp diff -r 9729919e589c -r 8bed05455706 widgets/TypingSelectWidget.cpp --- a/widgets/TypingSelectWidget.cpp Mon Mar 01 16:55:27 2010 +0000 +++ b/widgets/TypingSelectWidget.cpp Tue Mar 16 17:36:28 2010 +0000 @@ -2,8 +2,17 @@ #include "TypingSelectWidget.h" +#include + #include #include +#include + +#include "Matcher.h" + +#include "Objects.h" + +using namespace Dataquay; namespace ClassicalData { @@ -13,11 +22,47 @@ QGridLayout *layout = new QGridLayout; setLayout(layout); - QLineEdit *editor = new QLineEdit; - layout->addWidget(editor); + m_editor = new QLineEdit; + layout->addWidget(m_editor); + + connect(m_editor, SIGNAL(textEdited(const QString &)), + this, SLOT(textEdited(const QString &))); + + m_list = new QListWidget(); + m_list->setWindowFlags(Qt::Window | Qt::Tool | Qt::FramelessWindowHint); + m_list->hide(); +} + +void +TypingSelectWidget::textEdited(const QString &s) +{ + if (m_matchers.empty()) return; + + GuessList results = m_matchers[0]->match(s, 10); + m_list->clear(); + + if (results.empty()) { + m_list->hide(); + return; + } + + foreach (Guess g, results) { + Composer *c = qobject_cast(g.entity()); + if (!c) continue; + m_list->addItem(c->getSortName(true)); + } + + m_list->show(); + m_list->move(m_editor->mapToGlobal(QPoint(0, m_editor->height()))); + m_list->resize(m_list->sizeHint()); +// m_list->setMinimumWidth((width()*3)/4); +// m_list->setMinimumHeight(height() * 5); + + + std::cerr << std::endl; +} + + } - -} - diff -r 9729919e589c -r 8bed05455706 widgets/TypingSelectWidget.h --- a/widgets/TypingSelectWidget.h Mon Mar 01 16:55:27 2010 +0000 +++ b/widgets/TypingSelectWidget.h Tue Mar 16 17:36:28 2010 +0000 @@ -6,6 +6,9 @@ #include #include +class QLineEdit; +class QListWidget; + namespace ClassicalData { @@ -13,13 +16,20 @@ class TypingSelectWidget : public QWidget { + Q_OBJECT + public: TypingSelectWidget(QWidget *parent = 0); - void setMatchers(QList m) { m_matchers = m; } + void addMatcher(Matcher *m) { m_matchers.push_back(m); } + +private slots: + void textEdited(const QString &); private: QList m_matchers; + QLineEdit *m_editor; + QListWidget *m_list; }; } diff -r 9729919e589c -r 8bed05455706 widgets/widgets.pro --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/widgets/widgets.pro Tue Mar 16 17:36:28 2010 +0000 @@ -0,0 +1,12 @@ +TEMPLATE = lib +TARGET = + +load(../../all.prf) + +CONFIG += staticlib + +INCLUDEPATH += ../common + +HEADERS += TypingSelectWidget.h +SOURCES += TypingSelectWidget.cpp +