diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/modules/language/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/modules/language/langdetector.cpp | 46 | ||||
| -rw-r--r-- | src/modules/language/langdetector.h | 37 | ||||
| -rw-r--r-- | src/modules/language/libkvilanguage.cpp | 77 |
4 files changed, 51 insertions, 111 deletions
diff --git a/src/modules/language/CMakeLists.txt b/src/modules/language/CMakeLists.txt index 501d16c78..96a2607b5 100644 --- a/src/modules/language/CMakeLists.txt +++ b/src/modules/language/CMakeLists.txt @@ -2,13 +2,11 @@ SET(kvilanguage_MOC_HDRS detector.h - langdetector.h ) SET(kvilanguage_SRCS libkvilanguage.cpp detector.cpp - langdetector.cpp ) # After this call, files will be moc'ed to moc_kvi_*.cpp diff --git a/src/modules/language/langdetector.cpp b/src/modules/language/langdetector.cpp deleted file mode 100644 index 979e840f1..000000000 --- a/src/modules/language/langdetector.cpp +++ /dev/null @@ -1,46 +0,0 @@ -// -// File : helpwindow.cpp -// Creation date : Tue Aug 11 2000 18:08:22 by Szymon Stefanek -// -// This file is part of the KVirc irc client distribution -// Copyright (C) 1999-2000 Szymon Stefanek (pragma at kvirc dot net) -// -// This program is FREE software. You can redistribute it and/or -// modify it under the m_terms of the GNU General Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your opinion) any later version. -// -// This program is distributed in the HOPE that it will be USEFUL, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -// See the GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, write to the Free Software Foundation, -// Inc. ,59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -// - -#include <QFile> - -#include "langdetector.h" - -KviLangDetetector::KviLangDetetector(QString doc) -{ - qint64 iMaxSize = 4096; - char buffer[4096]; - QFile *file = new QFile(doc); - - if (!file->open(QIODevice::ReadOnly | QIODevice::Text)) - return; - - if(file->read(buffer, iMaxSize)<1) - return; - - LanguageAndEncodingResult r; - detect_language_and_encoding(buffer,&r,0); - for(int i=0;i<DLE_NUM_BEST_MATCHES;i++) - printf("LANGUAGE %s, ENCODING %s, SCORE: %f\n",r.match[i].szLanguage,r.match[i].szEncoding,r.match[i].dScore); - printf("Accuracy: %f\n",r.dAccuracy); - return; -} - diff --git a/src/modules/language/langdetector.h b/src/modules/language/langdetector.h deleted file mode 100644 index e9beeae28..000000000 --- a/src/modules/language/langdetector.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef _LANGDETECTOR_H_ -#define _LANGDETECTOR_H_ -// -// File : langdetector.h -// Creation date : Sun May 11 2008 00:07:20 CET by Szymon Stefanek -// -// This file is part of the KVirc irc client distribution -// Copyright (C) 1999-2008 Szymon Stefanek (pragma at kvirc dot net) -// -// This program is FREE software. You can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your opinion) any later version. -// -// This program is distributed in the HOPE that it will be USEFUL, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -// See the GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, write to the Free Software Foundation, -// Inc. ,59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -// - -#include "kvi_string.h" -#include "kvi_kvs_array.h" - -#include "detector.h" - -class KviLangDetetector -{ -public: - KviLangDetetector(QString doc); - ~KviLangDetetector(); -}; - -#endif //_LANGDETECTOR_H_ diff --git a/src/modules/language/libkvilanguage.cpp b/src/modules/language/libkvilanguage.cpp index c2fe14a64..be668504e 100644 --- a/src/modules/language/libkvilanguage.cpp +++ b/src/modules/language/libkvilanguage.cpp @@ -28,60 +28,85 @@ #include <QFileInfo> -#include "langdetector.h" +#include "detector.h" /* @doc: language.detect @type: - command + function @title: - language.detect + $language.detect @short: - (Tries to) detect the language and encoding of a text file + (Tries to) detect the language and encoding of a string @syntax: - language.detect [document: string] + <hash> language.detect(<text: string>) @description: - Tries to detect the language and encoding of the file specified - as [document]. The file has to be preferably a simple text file, - or the detector can be deceived by the file format (eg. html files - contains english words inside html tags). - [document] can be an absolute path. + Tries to detect the language and encoding of the string specified + as [text]. The accuracy of the detection mainly depends on the length + of the supplied text. Good results can be achieved with some thousands + characters.[br] This command is exported by the "language" module. + @examples: + [example] + %dati = $language.detect("I'm a lord and i speak perfect english."); + if(%dati{"error"} != "")echo "Language detection failed: " %dati{"error"}; + %count = %dati{"matchcount"}; + for(%i=0;%i<%count;%i++) + { + echo "LANGUAGE " %i " : " %dati{"matches"}[%i]{"language"}; + echo "ENCODING " %i " : " %dati{"matches"}[%i]{"encoding"}; + echo "SCORE " %i " : "%dati{"matches"}[%i]{"score"}; + } + echo "ACCURACY: " %dati{"accuracy"}; + [/example] */ -static bool language_kvs_cmd_detect(KviKvsModuleCommandCall * c) +static bool language_kvs_cmd_detect(KviKvsModuleFunctionCall * c) { - QString doc; + QString text, error; + KviKvsArray *matches; + KviKvsHash *match, *ret; + LanguageAndEncodingResult r; + int matchcount=DLE_NUM_BEST_MATCHES; KVSM_PARAMETERS_BEGIN(c) - KVSM_PARAMETER("document",KVS_PT_STRING,KVS_PF_OPTIONAL,doc) + KVSM_PARAMETER("text",KVS_PT_STRING,KVS_PF_OPTIONAL,text) KVSM_PARAMETERS_END(c) - if(doc.isEmpty()){ - c->warning(__tr2qs("No file given, no detection occured.")); - return true; - } + //on error we return an empty array + matches = new KviKvsArray(); - QFileInfo * f= new QFileInfo(doc); - qDebug("Path %s",doc.toUtf8().data()); - if(f) - { - if(!f->exists()) + if(text.isEmpty()){ + error = "err_notext"; + matchcount = 0; + } else { + detect_language_and_encoding(text.utf8().data(),&r,0); + + for(int i=0;i<matchcount;i++) { - c->warning(__tr2qs("The file you specified doesn't exists, can't detect its language.")); - return true; + match = new KviKvsHash(); + match->set("language",new KviKvsVariant(QString(r.match[i].szLanguage))); + match->set("encoding",new KviKvsVariant(r.match[i].szEncoding)); + match->set("score",new KviKvsVariant((kvs_real_t)r.match[i].dScore)); + matches->set(i, new KviKvsVariant(match)); } } - KviLangDetetector *w = new KviLangDetetector(doc); + ret = new KviKvsHash(); + ret->set("matches",new KviKvsVariant(matches)); + ret->set("matchcount",new KviKvsVariant((kvs_int_t)matchcount)); + ret->set("error",new KviKvsVariant(error)); + ret->set("accuracy",new KviKvsVariant((kvs_real_t)r.dAccuracy)); + + c->returnValue()->setHash(ret); return true; } static bool language_module_init(KviModule * m) { - KVSM_REGISTER_SIMPLE_COMMAND(m,"detect",language_kvs_cmd_detect); + KVSM_REGISTER_FUNCTION(m,"detect",language_kvs_cmd_detect); return true; } |
