summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorhorchi <vdr@jwendel.de>2017-03-05 16:39:28 +0100
committerhorchi <vdr@jwendel.de>2017-03-05 16:39:28 +0100
commite2a48d8701f91b8e24fbe9e99e91eb72a87bb749 (patch)
tree726f70554b4ca985a09ef6e30a7fdc8df089993c /tools
downloadvdr-epg-daemon-e2a48d8701f91b8e24fbe9e99e91eb72a87bb749.tar.gz
vdr-epg-daemon-e2a48d8701f91b8e24fbe9e99e91eb72a87bb749.tar.bz2
git init1.1.103
Diffstat (limited to 'tools')
-rw-r--r--tools/fuzzy.c70
-rw-r--r--tools/fuzzy.h10
-rw-r--r--tools/stringhelpers.c53
-rw-r--r--tools/stringhelpers.h31
4 files changed, 164 insertions, 0 deletions
diff --git a/tools/fuzzy.c b/tools/fuzzy.c
new file mode 100644
index 0000000..188f017
--- /dev/null
+++ b/tools/fuzzy.c
@@ -0,0 +1,70 @@
+#include <iterator>
+#include <algorithm>
+#include <vector>
+#include <string>
+#include <sstream>
+#include <iostream>
+
+template<typename T, typename C>
+size_t
+seq_distance(const T& seq1, const T& seq2, const C& cost,
+ const typename T::value_type& empty = typename T::value_type()) {
+ const size_t size1 = seq1.size();
+ const size_t size2 = seq2.size();
+
+ std::vector<size_t> curr_col(size2 + 1);
+ std::vector<size_t> prev_col(size2 + 1);
+
+ // Prime the previous column for use in the following loop:
+ prev_col[0] = 0;
+ for (size_t idx2 = 0; idx2 < size2; ++idx2) {
+ prev_col[idx2 + 1] = prev_col[idx2] + cost(empty, seq2[idx2]);
+ }
+
+ for (size_t idx1 = 0; idx1 < size1; ++idx1) {
+ curr_col[0] = curr_col[0] + cost(seq1[idx1], empty);
+
+ for (size_t idx2 = 0; idx2 < size2; ++idx2) {
+ curr_col[idx2 + 1] = std::min(std::min(
+ curr_col[idx2] + cost(empty, seq2[idx2]),
+ prev_col[idx2 + 1] + cost(seq1[idx1], empty)),
+ prev_col[idx2] + cost(seq1[idx1], seq2[idx2]));
+ }
+
+ curr_col.swap(prev_col);
+ curr_col[0] = prev_col[0];
+ }
+
+ return prev_col[size2];
+}
+
+size_t
+letter_distance(char letter1, char letter2) {
+ return letter1 != letter2 ? 1 : 0;
+}
+
+size_t
+word_distance(const std::string& word1, const std::string& word2) {
+ return seq_distance(word1, word2, &letter_distance);
+}
+
+size_t
+sentence_distance(const std::string& sentence1, const std::string& sentence2) {
+ std::vector<std::string> words1;
+ std::vector<std::string> words2;
+ std::istringstream iss1(sentence1);
+ std::istringstream iss2(sentence2);
+ for(std::istream_iterator<std::string> it(iss1), end; ; ) {
+ words1.push_back(*it);
+ if(++it == end)
+ break;
+ words1.push_back(" ");
+ }
+ for(std::istream_iterator<std::string> it(iss2), end; ; ) {
+ words2.push_back(*it);
+ if(++it == end)
+ break;
+ words2.push_back(" ");
+ }
+ return seq_distance(words1, words2, &word_distance);
+} \ No newline at end of file
diff --git a/tools/fuzzy.h b/tools/fuzzy.h
new file mode 100644
index 0000000..7d90466
--- /dev/null
+++ b/tools/fuzzy.h
@@ -0,0 +1,10 @@
+#ifndef __FUZZY_H__
+#define __FUZZY_H__
+
+#include <string>
+
+size_t letter_distance(char letter1, char letter2);
+size_t word_distance(const std::string& word1, const std::string& word2);
+size_t sentence_distance(const std::string& sentence1, const std::string& sentence2);
+
+#endif
diff --git a/tools/stringhelpers.c b/tools/stringhelpers.c
new file mode 100644
index 0000000..d70c493
--- /dev/null
+++ b/tools/stringhelpers.c
@@ -0,0 +1,53 @@
+
+#include "stringhelpers.h"
+
+cJsonLoader::cJsonLoader(const char* name)
+{
+ err = new json_error_t;
+ obj = json_loads(name, 0, err);
+
+}
+
+cJsonLoader::~cJsonLoader()
+{
+ if (obj)
+ json_decref(obj);
+
+ if (err)
+ delete(err);
+}
+
+json_t* cJsonLoader::objectByName(const char* name)
+{
+ return json_object_get(obj, name);
+}
+
+int cJsonLoader::isObject()
+{
+ return obj && json_is_object(obj);
+}
+
+//replace all "search" with "replace" in "subject"
+string str_replace(const string& search, const string& replace, const string& subject) {
+ string str = subject;
+ size_t pos = 0;
+ while((pos = str.find(search, pos)) != string::npos) {
+ str.replace(pos, search.length(), replace);
+ pos += replace.length();
+ }
+ return str;
+}
+//cut string after first "search"
+string str_cut(const string& search, const string& subject) {
+ string str = subject;
+ string strCutted = "";
+ size_t found = str.find_first_of(search);
+ if (found != string::npos) {
+ strCutted = str.substr(0, found);
+ size_t foundSpace = strCutted.find_last_of(" ");
+ if ((foundSpace != string::npos) && (foundSpace == (strCutted.size()-1))) {
+ strCutted = strCutted.substr(0, strCutted.size()-1);
+ }
+ }
+ return strCutted;
+}
diff --git a/tools/stringhelpers.h b/tools/stringhelpers.h
new file mode 100644
index 0000000..b4006f3
--- /dev/null
+++ b/tools/stringhelpers.h
@@ -0,0 +1,31 @@
+
+#ifndef _STRINGHELPERS_H_
+#define _STRINGHELPERS_H_
+
+#include <string>
+using namespace std;
+
+string str_replace(const string& search, const string& replace, const string& subject);
+string str_cut(const string& search, const string& subject);
+
+#include <jansson.h>
+
+class cJsonLoader
+{
+ public:
+
+ cJsonLoader(const char* name);
+ ~cJsonLoader();
+
+ json_t* object() { return obj; }
+ json_error_t* error() { return err; }
+ json_t* objectByName(const char* name);
+ int isObject();
+
+ protected:
+
+ json_error_t* err;
+ json_t* obj;
+};
+
+#endif // _STRINGHELPERS_H_