/* * Copyright 2005,2006 Fabrice Colin * * 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 option) 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 Library 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 #include #include #include #include "config.h" extern "C" { #ifdef HAVE_LIBTEXTCAT_TEXTCAT_H #include #else #include #endif } #include "StringManip.h" #include "Timer.h" #include "LanguageDetector.h" #include "config.h" using std::cout; using std::cerr; using std::endl; using std::string; using std::vector; using std::min; unsigned int LanguageDetector::m_maxTextSize = 1000; LanguageDetector::LanguageDetector() { } LanguageDetector::~LanguageDetector() { } /** * Attempts to guess the language. * Returns a list of candidates, or "unknown" if detection failed. */ void LanguageDetector::guessLanguage(const char *pData, unsigned int dataLength, std::vector &candidates) { string confFile(SYSCONFDIR); char *textCatVersion = textcat_Version(); #ifdef HAVE_TEXTCAT_CAT const char *catResults[10]; #endif candidates.clear(); // What configuration file should we use ? confFile += "/pinot/"; #ifdef DEBUG cout << "LanguageDetector::guessLanguage: detected " << textCatVersion << endl; #endif if (strncasecmp(textCatVersion, "TextCat 3", 9) == 0) { // Version 3 confFile += "textcat3_conf.txt"; } else { confFile += "textcat_conf.txt"; } // Initialize void *td = textcat_Init(confFile.c_str()); if (td == NULL) { candidates.push_back("unknown"); return; } // Classify #ifdef DEBUG Timer timer; timer.start(); cout << "LanguageDetector::guessLanguage: starting" << endl; #endif #ifdef HAVE_TEXTCAT_CAT unsigned int resultNum = textcat_Cat(td, pData, min(dataLength, m_maxTextSize), catResults, 10); if (resultNum == 0 ) { candidates.push_back("unknown"); } else { for (unsigned int i=0; i