Add burst 08/1008/2
authorTasuku Suzuki <stasuku@gmail.com>
Sat, 4 Feb 2017 15:01:05 +0000 (00:01 +0900)
committerstasuku <stasuku@gmail.com>
Sat, 4 Feb 2017 15:02:09 +0000 (00:02 +0900)
Change-Id: I4ef2ad1db79baac84f7d8762ffa31d0f87278ed7
Reviewed-on: http://codereview.qt-users.jp/1008
Reviewed-by: <stasuku@gmail.com>
Tested-by: <stasuku@gmail.com>
examples/jubatus/cpp/burst/burst.pro [new file with mode: 0644]
examples/jubatus/cpp/burst/trivial_burst/main.cpp [new file with mode: 0644]
examples/jubatus/cpp/burst/trivial_burst/trivial_burst.json [new file with mode: 0644]
examples/jubatus/cpp/burst/trivial_burst/trivial_burst.pro [new file with mode: 0644]
examples/jubatus/cpp/cpp.pro
src/jubatus/burst/burst.pri [new file with mode: 0644]
src/jubatus/burst/qjubatusburst.cpp [new file with mode: 0644]
src/jubatus/burst/qjubatusburst.h [new file with mode: 0644]
src/jubatus/client/qjubatusclient.cpp
src/jubatus/jubatus.pro

diff --git a/examples/jubatus/cpp/burst/burst.pro b/examples/jubatus/cpp/burst/burst.pro
new file mode 100644 (file)
index 0000000..2faf8ed
--- /dev/null
@@ -0,0 +1,2 @@
+TEMPLATE = subdirs
+SUBDIRS += trivial_burst
diff --git a/examples/jubatus/cpp/burst/trivial_burst/main.cpp b/examples/jubatus/cpp/burst/trivial_burst/main.cpp
new file mode 100644 (file)
index 0000000..cc78147
--- /dev/null
@@ -0,0 +1,48 @@
+#include <QtCore>
+#include <QtJubatus>
+
+void add(QJubatusBurst *burst, int pos, int burstCount, int nonburstCount)
+{
+    QList<QJubatusBurst::Document> documents;
+    documents.reserve(burstCount + nonburstCount);
+    for (int i = 0; i < burstCount; i++) {
+        QJubatusBurst::Document document;
+        document.pos = pos;
+        document.text = QString::fromUtf8("バルス!!");
+        documents.append(document);
+    }
+    for (int i = 0; i < nonburstCount; i++) {
+        QJubatusBurst::Document document;
+        document.pos = pos;
+        document.text = QString::fromUtf8("ユバタス");
+        documents.append(document);
+    }
+    burst->addDocuments(documents);
+}
+
+int main(int argc, char **argv)
+{
+    QCoreApplication app(argc, argv);
+
+    QJubatusBurst burst;
+
+    QJubatusBurst::KeywordWithParams kWP;
+    kWP.keyword = QString::fromUtf8("バルス");
+    kWP.scalingParam = 1.001;
+    kWP.gamma = 0.1;
+    burst.addKeyword(kWP);
+
+    add(&burst,   1,      5,        30);
+    add(&burst,  11,     15,        50);
+    add(&burst,  21,    500,        10);
+    add(&burst,  31,   2000,        10);
+    add(&burst,  41,  22222,        40); // バルスの高まり
+    add(&burst,  51,     10,        10);
+    add(&burst,  61,      5,        25);
+
+    for (const QJubatusBurst::Batch &batch : burst.getResult(QString::fromUtf8("バルス")).batches) {
+        qDebug() << batch.allDataCount << batch.relevantDataCount << batch.burstWeight;
+    }
+
+    return 0;
+}
diff --git a/examples/jubatus/cpp/burst/trivial_burst/trivial_burst.json b/examples/jubatus/cpp/burst/trivial_burst/trivial_burst.json
new file mode 100644 (file)
index 0000000..dbb6af4
--- /dev/null
@@ -0,0 +1,11 @@
+{
+  "parameter" : {
+    "window_batch_size": 5,
+    "batch_interval": 10,
+    "max_reuse_batch_num": 5,
+    "costcut_threshold": -1,
+    "result_window_rotate_size": 5
+  },
+
+  "method" : "burst"
+}
diff --git a/examples/jubatus/cpp/burst/trivial_burst/trivial_burst.pro b/examples/jubatus/cpp/burst/trivial_burst/trivial_burst.pro
new file mode 100644 (file)
index 0000000..14bb879
--- /dev/null
@@ -0,0 +1,11 @@
+TEMPLATE = app
+TARGET = jubatus-trivial-burst
+QT = core jubatus
+CONFIG += c++11
+SOURCES = main.cpp
+
+OTHER_FILES += trivial_burst.json
+
+target.path = $$[QT_INSTALL_EXAMPLES]/jubatus/cpp/burst/trivial_burst
+INSTALLS += target
+
index f941325..0e570c2 100644 (file)
@@ -1,2 +1,2 @@
 TEMPLATE = subdirs
-SUBDIRS += anomaly classifier recommender regression
+SUBDIRS += anomaly burst classifier recommender regression
diff --git a/src/jubatus/burst/burst.pri b/src/jubatus/burst/burst.pri
new file mode 100644 (file)
index 0000000..e59865e
--- /dev/null
@@ -0,0 +1,5 @@
+INCLUDEPATH += $$PWD
+DEPENDPATH += $$PWD
+
+HEADERS += $$PWD/qjubatusburst.h
+SOURCES += $$PWD/qjubatusburst.cpp
diff --git a/src/jubatus/burst/qjubatusburst.cpp b/src/jubatus/burst/qjubatusburst.cpp
new file mode 100644 (file)
index 0000000..c7e07ff
--- /dev/null
@@ -0,0 +1,197 @@
+/* Copyright (c) 2012 Silk Project.
+ * All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in the
+ *       documentation and/or other materials provided with the distribution.
+ *     * Neither the name of the Silk nor the
+ *       names of its contributors may be used to endorse or promote products
+ *       derived from this software without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL SILK BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "qjubatusburst.h"
+
+#include <QtCore/QDebug>
+
+#include <vector>
+
+#include <jubatus/client/burst_client.hpp>
+
+QJubatusBurst::QJubatusBurst(QObject *parent)
+    : QJubatusClient(parent)
+{
+}
+
+int QJubatusBurst::addDocuments(const QList<QJubatusBurst::Document> &data)
+{
+    bool ret = 0;
+    EXEC_JUBATUS_COMMAND( ret = client()->add_documents(convert(data)) );
+    return ret;
+}
+
+QJubatusBurst::Window QJubatusBurst::getResult(const QString &keyword)
+{
+    Window ret;
+    EXEC_JUBATUS_COMMAND( ret = convert(client()->get_result(convert(keyword))) );
+    return ret;
+}
+
+QJubatusBurst::Window QJubatusBurst::getResultAt(const QString &keyword, double pos)
+{
+    Window ret;
+    EXEC_JUBATUS_COMMAND( ret = convert(client()->get_result_at(convert(keyword), pos)) );
+    return ret;
+}
+
+QHash<QString, QJubatusBurst::Window> QJubatusBurst::getAllBurstedResults()
+{
+    QHash<QString, QJubatusBurst::Window> ret;
+    EXEC_JUBATUS_COMMAND( ret = convert(client()->get_all_bursted_results()) );
+    return ret;
+}
+
+QHash<QString, QJubatusBurst::Window> QJubatusBurst::getAllBurstedResultsAt(double pos)
+{
+    QHash<QString, QJubatusBurst::Window> ret;
+    EXEC_JUBATUS_COMMAND( ret = convert(client()->get_all_bursted_results_at(pos)) );
+    return ret;
+}
+
+QList<QJubatusBurst::KeywordWithParams> QJubatusBurst::getAllKeywords()
+{
+    QList<QJubatusBurst::KeywordWithParams> ret;
+    EXEC_JUBATUS_COMMAND( ret = convert(client()->get_all_keywords()) );
+    return ret;
+}
+
+bool QJubatusBurst::addKeyword(const QJubatusBurst::KeywordWithParams &keyword)
+{
+    bool ret = false;
+    EXEC_JUBATUS_COMMAND( ret = client()->add_keyword(convert(keyword)) );
+    return ret;
+}
+
+bool QJubatusBurst::removeKeyword(const QString &keyword)
+{
+    bool ret = false;
+    EXEC_JUBATUS_COMMAND( ret = client()->remove_keyword(convert(keyword)) );
+    return ret;
+}
+
+bool QJubatusBurst::removeAllKeywords()
+{
+    bool ret = false;
+    EXEC_JUBATUS_COMMAND( ret = client()->remove_all_keywords() );
+    return ret;
+}
+
+bool QJubatusBurst::clear()
+{
+    bool ret = false;
+    EXEC_JUBATUS_COMMAND( ret = client()->clear() );
+    return ret;
+}
+
+std::vector<jubatus::burst::document> QJubatusBurst::convert(const QList<QJubatusBurst::Document> &data) const
+{
+    std::vector<jubatus::burst::document> ret;
+    foreach (const Document &datum, data) {
+        ret.push_back(convert(datum));
+    }
+    return ret;
+}
+
+jubatus::burst::document QJubatusBurst::convert(const Document &data) const
+{
+    return jubatus::burst::document(data.pos, convert(data.text));
+}
+
+QHash<QString, QJubatusBurst::Window> QJubatusBurst::convert(const std::map<std::string, jubatus::burst::window> &data) const
+{
+    QHash<QString, Window> ret;
+    for(const auto &itr : data) {
+        ret.insert(convert(itr.first), convert(itr.second));
+    }
+    return ret;
+}
+
+QJubatusBurst::Window QJubatusBurst::convert(const jubatus::burst::window &data) const
+{
+    QJubatusBurst::Window ret;
+    ret.startPos = data.start_pos;
+    ret.batches = convert(data.batches);
+    return ret;
+}
+
+QList<QJubatusBurst::Batch> QJubatusBurst::convert(const std::vector<jubatus::burst::batch> &data) const
+{
+    QList<QJubatusBurst::Batch> ret;
+    for(const jubatus::burst::batch &datum : data) {
+        ret.append(convert(datum));
+    }
+    return ret;
+}
+
+QJubatusBurst::Batch QJubatusBurst::convert(const jubatus::burst::batch &data) const
+{
+    QJubatusBurst::Batch ret;
+    ret.allDataCount = data.all_data_count;
+    ret.relevantDataCount = data.relevant_data_count;
+    ret.burstWeight = data.burst_weight;
+    return ret;
+}
+
+QJubatusBurst::KeywordWithParams QJubatusBurst::convert(const jubatus::burst::keyword_with_params &data) const
+{
+    QJubatusBurst::KeywordWithParams ret;
+    ret.keyword = convert(data.keyword);
+    ret.scalingParam = data.scaling_param;
+    ret.gamma = data.gamma;
+    return ret;
+}
+QList<QJubatusBurst::KeywordWithParams> QJubatusBurst::convert(const std::vector<jubatus::burst::keyword_with_params> &data) const
+{
+    QList<QJubatusBurst::KeywordWithParams> ret;
+    for (const jubatus::burst::keyword_with_params &datum : data) {
+        ret.append(convert(datum));
+    }
+    return ret;
+}
+
+jubatus::burst::keyword_with_params QJubatusBurst::convert(const QJubatusBurst::KeywordWithParams &data) const
+{
+    jubatus::burst::keyword_with_params ret;
+    ret.keyword = convert(data.keyword);
+    ret.scaling_param = data.scalingParam;
+    ret.gamma = data.gamma;
+    return ret;
+}
+
+std::vector<jubatus::burst::keyword_with_params> QJubatusBurst::convert(const QList<QJubatusBurst::KeywordWithParams> &data) const
+{
+    std::vector<jubatus::burst::keyword_with_params> ret;
+    foreach (const KeywordWithParams &datum, data) {
+        ret.push_back(convert(datum));
+    }
+    return ret;
+}
+
+jubatus::burst::client::burst *QJubatusBurst::client()
+{
+    return QJubatusClient::client<jubatus::burst::client::burst>();
+}
diff --git a/src/jubatus/burst/qjubatusburst.h b/src/jubatus/burst/qjubatusburst.h
new file mode 100644 (file)
index 0000000..f04e116
--- /dev/null
@@ -0,0 +1,107 @@
+/* Copyright (c) 2012 Silk Project.
+ * All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in the
+ *       documentation and/or other materials provided with the distribution.
+ *     * Neither the name of the Silk nor the
+ *       names of its contributors may be used to endorse or promote products
+ *       derived from this software without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL SILK BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef QJUBATUSBURST_H
+#define QJUBATUSBURST_H
+
+#include "jubatus_global.h"
+#include "qjubatusclient.h"
+
+#include <QtCore/QVariant>
+
+namespace jubatus {
+    namespace burst {
+        struct keyword_with_params;
+        struct batch;
+        struct window;
+        struct document;
+        namespace client {
+            class burst;
+        }
+    }
+}
+
+class JUBATUS_EXPORT QJubatusBurst : public QJubatusClient
+{
+    Q_OBJECT
+public:
+    explicit QJubatusBurst(QObject *parent = nullptr);
+
+    struct KeywordWithParams {
+        KeywordWithParams() : scalingParam(0.0), gamma(0.0) {}
+        QString keyword;
+        double scalingParam;
+        double gamma;
+    };
+
+    struct Batch {
+        Batch() : allDataCount(0), relevantDataCount(0), burstWeight(0.0) {}
+        qint32 allDataCount;
+        qint32 relevantDataCount;
+        double burstWeight;
+    };
+
+    struct Window {
+        Window() : startPos(0.0) {}
+        double startPos;
+        QList<Batch> batches;
+    };
+
+    struct Document {
+        Document() : pos(0.0) {}
+        double pos;
+        QString text;
+    };
+
+    int addDocuments(const QList<Document> &data);
+    Window getResult(const QString &keyword);
+    Window getResultAt(const QString &keyword, double pos);
+    QHash<QString, Window> getAllBurstedResults();
+    QHash<QString, Window> getAllBurstedResultsAt(double pos);
+    QList<KeywordWithParams> getAllKeywords();
+    bool addKeyword(const KeywordWithParams &keyword);
+    bool removeKeyword(const QString &keyword);
+    bool removeAllKeywords();
+    bool clear();
+
+protected:
+    using QJubatusClient::convert;
+    std::vector<jubatus::burst::document> convert(const QList<Document> &data) const;
+    jubatus::burst::document convert(const Document &data) const;
+    QHash<QString, Window> convert(const std::map<std::string, jubatus::burst::window> &data) const;
+    Window convert(const jubatus::burst::window &data) const;
+    QList<Batch> convert(const std::vector<jubatus::burst::batch> &data) const;
+    Batch convert(const jubatus::burst::batch &data) const;
+    KeywordWithParams convert(const jubatus::burst::keyword_with_params &data) const;
+    QList<KeywordWithParams> convert(const std::vector<jubatus::burst::keyword_with_params> &data) const;
+    jubatus::burst::keyword_with_params convert(const KeywordWithParams &data) const;
+    std::vector<jubatus::burst::keyword_with_params> convert(const QList<KeywordWithParams> &data) const;
+
+private:
+    jubatus::burst::client::burst *client();
+};
+
+#endif // QJUBATUSBURST_H
index 24cc860..15041e3 100644 (file)
@@ -235,7 +235,7 @@ std::map<std::string, std::string> QJubatusClient::convert(const QHash<QString,
 QHash<QString, QString> QJubatusClient::convert(const std::map<std::string, std::string> &data) const
 {
     QHash<QString, QString> ret;
-    for(const auto itr : data) {
+    for(const auto &itr : data) {
         ret.insert(convert(itr.first), convert(itr.second));
     }
     return ret;
index bc2ab2b..31e5d14 100644 (file)
@@ -16,6 +16,7 @@ HEADERS += \
 
 include(./client/client.pri)
 include(./anomaly/anomaly.pri)
+include(./burst/burst.pri)
 include(./classifier/classifier.pri)
 include(./recommender/recommender.pri)
 include(./regression/regression.pri)