2010-11-17 06:43:56 +01:00
|
|
|
// Copyright (c) 2010, Thomas Goyne <plorkyeran@aegisub.org>
|
2006-01-16 22:02:54 +01:00
|
|
|
// 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 Aegisub Group 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 THE COPYRIGHT OWNER OR CONTRIBUTORS 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.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
2006-01-16 22:02:54 +01:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @file spellchecker_hunspell.cpp
|
|
|
|
/// @brief Hunspell-based spell checker implementation
|
|
|
|
/// @ingroup spelling
|
|
|
|
///
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2007-12-31 07:46:22 +01:00
|
|
|
#ifdef WITH_HUNSPELL
|
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#ifndef AGI_PRE
|
2010-11-17 06:43:56 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <iterator>
|
|
|
|
#include <list>
|
|
|
|
|
2007-01-01 05:15:54 +01:00
|
|
|
#include <wx/dir.h>
|
2007-01-01 06:15:05 +01:00
|
|
|
#include <wx/filename.h>
|
2009-09-10 15:06:40 +02:00
|
|
|
#endif
|
|
|
|
|
2010-11-17 06:43:56 +01:00
|
|
|
#include <libaegisub/io.h>
|
|
|
|
#include <libaegisub/line_iterator.h>
|
2010-06-01 10:21:30 +02:00
|
|
|
#include <libaegisub/log.h>
|
2009-09-10 15:06:40 +02:00
|
|
|
|
|
|
|
#include "charset_conv.h"
|
2010-05-21 03:13:36 +02:00
|
|
|
#include "compat.h"
|
|
|
|
#include "main.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "spellchecker_hunspell.h"
|
|
|
|
#include "standard_paths.h"
|
2007-04-22 04:03:40 +02:00
|
|
|
|
2006-12-24 22:52:54 +01:00
|
|
|
HunspellSpellChecker::HunspellSpellChecker() {
|
2010-05-21 03:13:36 +02:00
|
|
|
SetLanguage(lagi_wxString(OPT_GET("Tool/Spell Checker/Language")->GetString()));
|
2006-12-24 22:52:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
HunspellSpellChecker::~HunspellSpellChecker() {
|
|
|
|
}
|
|
|
|
|
2006-12-26 02:08:46 +01:00
|
|
|
bool HunspellSpellChecker::CanAddWord(wxString word) {
|
2010-11-17 06:43:56 +01:00
|
|
|
if (!hunspell.get()) return false;
|
2010-06-03 22:32:25 +02:00
|
|
|
try {
|
2010-06-03 22:54:46 +02:00
|
|
|
conv->Convert(STD_STR(word));
|
2010-06-03 22:32:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (agi::charset::ConvError const&) {
|
|
|
|
return false;
|
|
|
|
}
|
2006-12-26 02:08:46 +01:00
|
|
|
}
|
|
|
|
|
2006-12-25 06:43:00 +01:00
|
|
|
void HunspellSpellChecker::AddWord(wxString word) {
|
2010-11-17 06:43:56 +01:00
|
|
|
if (!hunspell.get()) return;
|
|
|
|
|
|
|
|
std::string sword = STD_STR(word);
|
2007-01-02 01:47:47 +01:00
|
|
|
|
2010-11-17 06:43:56 +01:00
|
|
|
// Add it to the in-memory dictionary
|
|
|
|
hunspell->add(conv->Convert(sword).c_str());
|
2007-01-02 01:47:47 +01:00
|
|
|
|
2010-11-17 06:43:56 +01:00
|
|
|
std::list<std::string> words;
|
|
|
|
|
2008-01-14 03:01:50 +01:00
|
|
|
// Ensure that the path exists
|
2010-11-17 06:43:56 +01:00
|
|
|
wxFileName fn(userDicPath);
|
2008-01-14 03:01:50 +01:00
|
|
|
if (!fn.DirExists()) {
|
|
|
|
wxFileName::Mkdir(fn.GetPath());
|
|
|
|
}
|
2010-11-17 06:43:56 +01:00
|
|
|
// Read the old contents of the user's dictionary
|
|
|
|
else {
|
|
|
|
std::auto_ptr<std::istream> stream(agi::io::Open(STD_STR(userDicPath)));
|
|
|
|
std::remove_copy_if(
|
|
|
|
++agi::line_iterator<std::string>(*stream.get()),
|
|
|
|
agi::line_iterator<std::string>(),
|
|
|
|
std::back_inserter(words),
|
|
|
|
std::mem_fun_ref(&std::string::empty));
|
2007-01-02 01:47:47 +01:00
|
|
|
}
|
2008-01-14 03:01:50 +01:00
|
|
|
|
2010-11-17 06:43:56 +01:00
|
|
|
// Add the word
|
|
|
|
words.push_back(sword);
|
|
|
|
words.sort();
|
2009-08-01 06:37:29 +02:00
|
|
|
|
2010-11-17 06:43:56 +01:00
|
|
|
// Write the new dictionary
|
2010-06-03 22:32:25 +02:00
|
|
|
try {
|
2010-11-17 06:43:56 +01:00
|
|
|
agi::io::Save writer(STD_STR(userDicPath));
|
|
|
|
writer.Get() << words.size() << "\n";
|
|
|
|
std::copy(words.begin(), words.end(), std::ostream_iterator<std::string>(writer.Get(), "\n"));
|
2010-06-03 22:32:25 +02:00
|
|
|
}
|
2010-09-08 22:03:48 +02:00
|
|
|
catch (const agi::Exception&) {
|
2010-06-03 22:32:25 +02:00
|
|
|
// Failed to open file
|
|
|
|
}
|
2006-12-25 06:43:00 +01:00
|
|
|
}
|
|
|
|
|
2006-12-24 22:52:54 +01:00
|
|
|
bool HunspellSpellChecker::CheckWord(wxString word) {
|
2010-11-17 06:43:56 +01:00
|
|
|
if (!hunspell.get()) return true;
|
2010-06-03 22:32:25 +02:00
|
|
|
try {
|
2010-06-03 22:54:46 +02:00
|
|
|
return hunspell->spell(conv->Convert(STD_STR(word)).c_str()) == 1;
|
2010-06-03 22:32:25 +02:00
|
|
|
}
|
|
|
|
catch (agi::charset::ConvError const&) {
|
|
|
|
return false;
|
|
|
|
}
|
2006-12-24 22:52:54 +01:00
|
|
|
}
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2006-12-24 22:52:54 +01:00
|
|
|
wxArrayString HunspellSpellChecker::GetSuggestions(wxString word) {
|
|
|
|
wxArrayString suggestions;
|
2010-11-17 06:43:56 +01:00
|
|
|
if (!hunspell.get()) return suggestions;
|
2006-12-25 06:43:00 +01:00
|
|
|
|
2010-11-17 06:43:56 +01:00
|
|
|
// Grab raw from Hunspell
|
|
|
|
char **results;
|
|
|
|
int n = hunspell->suggest(&results,conv->Convert(STD_STR(word)).c_str());
|
2006-12-25 06:43:00 +01:00
|
|
|
|
2010-11-17 06:43:56 +01:00
|
|
|
suggestions.reserve(n);
|
|
|
|
// Convert each
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
try {
|
|
|
|
suggestions.Add(lagi_wxString(rconv->Convert(results[i])));
|
|
|
|
}
|
|
|
|
catch (agi::charset::ConvError const&) {
|
|
|
|
// Shouldn't ever actually happen...
|
|
|
|
}
|
|
|
|
delete results[i];
|
2010-06-03 22:32:25 +02:00
|
|
|
}
|
2006-12-25 06:43:00 +01:00
|
|
|
|
2010-11-17 06:43:56 +01:00
|
|
|
delete results;
|
|
|
|
|
2006-12-24 22:52:54 +01:00
|
|
|
return suggestions;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxArrayString HunspellSpellChecker::GetLanguageList() {
|
2010-11-18 04:00:08 +01:00
|
|
|
if (!languages.empty()) return languages;
|
|
|
|
|
2010-11-17 06:43:56 +01:00
|
|
|
wxArrayString dic, aff;
|
|
|
|
|
|
|
|
// Get list of dictionaries
|
|
|
|
wxString path = StandardPaths::DecodePath("?data/dictionaries/");
|
|
|
|
if (wxFileName::DirExists(path)) {
|
|
|
|
wxDir::GetAllFiles(path, &dic, "*.dic", wxDIR_FILES);
|
|
|
|
wxDir::GetAllFiles(path, &aff, "*.aff", wxDIR_FILES);
|
|
|
|
}
|
|
|
|
path = StandardPaths::DecodePath(lagi_wxString(OPT_GET("Path/Dictionary")->GetString()) + "/");
|
|
|
|
if (wxFileName::DirExists(path)) {
|
|
|
|
wxDir::GetAllFiles(path, &dic, "*.dic", wxDIR_FILES);
|
|
|
|
wxDir::GetAllFiles(path, &aff, "*.aff", wxDIR_FILES);
|
|
|
|
}
|
|
|
|
if (aff.empty()) return wxArrayString();
|
|
|
|
|
|
|
|
dic.Sort();
|
|
|
|
aff.Sort();
|
|
|
|
|
|
|
|
// Drop extensions
|
|
|
|
for (size_t i = 0; i < dic.size(); ++i) dic[i].resize(dic[i].size() - 4);
|
|
|
|
for (size_t i = 0; i < aff.size(); ++i) aff[i].resize(aff[i].size() - 4);
|
|
|
|
|
|
|
|
// Verify that each aff has a dic
|
|
|
|
for (size_t i = 0, j = 0; i < dic.size() && j < aff.size(); ) {
|
|
|
|
int cmp = dic[i].Cmp(aff[j]);
|
|
|
|
if (cmp < 0) ++i;
|
|
|
|
else if (cmp > 0) ++j;
|
|
|
|
else {
|
|
|
|
// Don't insert a language twice if it's in both the user dir and
|
|
|
|
// the app's dir
|
|
|
|
wxString name = wxFileName(aff[j]).GetName();
|
2010-11-18 04:00:08 +01:00
|
|
|
if (languages.empty() || name != languages.back())
|
|
|
|
languages.push_back(name);
|
2010-11-17 06:43:56 +01:00
|
|
|
++i;
|
|
|
|
++j;
|
2007-01-01 05:15:54 +01:00
|
|
|
}
|
|
|
|
}
|
2010-11-18 04:00:08 +01:00
|
|
|
return languages;
|
2006-12-24 22:52:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void HunspellSpellChecker::SetLanguage(wxString language) {
|
2010-11-17 06:43:56 +01:00
|
|
|
if (language.empty()) return;
|
|
|
|
|
|
|
|
wxString userDicRoot = StandardPaths::DecodePath(lagi_wxString(OPT_GET("Path/Dictionary")->GetString()));
|
|
|
|
wxString dataDicRoot = StandardPaths::DecodePath("?data/dictionaries");
|
|
|
|
|
|
|
|
// If the user has a dic/aff pair in their dictionary path for this language
|
|
|
|
// use that; otherwise use the one from Aegisub's install dir, adding words
|
|
|
|
// from the dic in the user's dictionary path if it exists
|
|
|
|
wxString affPath = wxString::Format("%s/%s.aff", userDicRoot, language);
|
|
|
|
wxString dicPath = wxString::Format("%s/%s.dic", userDicRoot, language);
|
|
|
|
userDicPath = wxString::Format("%s/user_%s.dic", userDicRoot, language);
|
|
|
|
if (!wxFileExists(affPath) || !wxFileExists(dicPath)) {
|
|
|
|
affPath = wxString::Format("%s/%s.aff", dataDicRoot, language);
|
|
|
|
dicPath = wxString::Format("%s/%s.dic", dataDicRoot, language);
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_I("dictionary/file") << dicPath;
|
|
|
|
|
|
|
|
if (!wxFileExists(affPath) || !wxFileExists(dicPath)) {
|
|
|
|
LOG_D("dictionary/file") << "Dictionary not found";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
hunspell.reset(new Hunspell(affPath.mb_str(csConvLocal), dicPath.mb_str(csConvLocal)));
|
|
|
|
if (!hunspell.get()) return;
|
|
|
|
|
|
|
|
conv.reset(new agi::charset::IconvWrapper("utf-8", hunspell->get_dic_encoding()));
|
|
|
|
rconv.reset(new agi::charset::IconvWrapper(hunspell->get_dic_encoding(), "utf-8"));
|
|
|
|
|
|
|
|
try {
|
|
|
|
std::auto_ptr<std::istream> stream(agi::io::Open(STD_STR(userDicPath)));
|
|
|
|
agi::line_iterator<std::string> userDic(*stream.get());
|
|
|
|
agi::line_iterator<std::string> end;
|
|
|
|
++userDic; // skip entry count line
|
|
|
|
for (; userDic != end; ++userDic) {
|
2010-11-17 06:44:02 +01:00
|
|
|
if (userDic->empty()) continue;
|
2010-11-17 06:43:56 +01:00
|
|
|
try {
|
|
|
|
hunspell->add(conv->Convert(*userDic).c_str());
|
2008-01-14 03:01:50 +01:00
|
|
|
}
|
2010-11-17 06:43:56 +01:00
|
|
|
catch (agi::charset::ConvError const&) {
|
|
|
|
// Normally this shouldn't happen, but some versions of Aegisub
|
|
|
|
// wrote words in the wrong charset
|
|
|
|
}
|
2008-01-14 03:01:50 +01:00
|
|
|
}
|
2010-11-17 06:43:56 +01:00
|
|
|
}
|
|
|
|
catch (agi::Exception const&) {
|
2010-11-18 04:00:08 +01:00
|
|
|
// File doesn't exist or we don't have permission to read it
|
2008-01-14 03:01:50 +01:00
|
|
|
}
|
2006-12-24 22:52:54 +01:00
|
|
|
}
|
2007-12-31 07:46:22 +01:00
|
|
|
|
|
|
|
#endif // WITH_HUNSPELL
|