From: <wel...@us...> - 2011-07-27 17:59:48
|
Revision: 7561 http://planeshift.svn.sourceforge.net/planeshift/?rev=7561&view=rev Author: weltall2 Date: 2011-07-27 17:59:42 +0000 (Wed, 27 Jul 2011) Log Message: ----------- other missing files Added Paths: ----------- trunk/src/plugins/common/spellchecker/ trunk/src/plugins/common/spellchecker/Jamfile trunk/src/plugins/common/spellchecker/spellchecker.cpp trunk/src/plugins/common/spellchecker/spellchecker.csplugin trunk/src/plugins/common/spellchecker/spellchecker.h Added: trunk/src/plugins/common/spellchecker/Jamfile =================================================================== --- trunk/src/plugins/common/spellchecker/Jamfile (rev 0) +++ trunk/src/plugins/common/spellchecker/Jamfile 2011-07-27 17:59:42 UTC (rev 7561) @@ -0,0 +1,19 @@ +SubDir TOP src plugins common spellchecker ; + +if $(HUNSPELL.AVAILABLE) = "yes" +{ +HUNSPELLDEF = HUNSPELL ; +} + +Description spellchecker : "Hunspell-Spellchecker Plugin" ; + +Plugin spellchecker + : [ Wildcard *.cpp *.h ] +; + +CFlags spellchecker : [ FDefines $(HUNSPELLDEF) ] ; +ExternalLibs spellchecker : CRYSTAL $(HUNSPELLDEF) ; +CompileGroups spellchecker : psclient ; + + + Property changes on: trunk/src/plugins/common/spellchecker/Jamfile ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:eol-style + native Added: trunk/src/plugins/common/spellchecker/spellchecker.cpp =================================================================== --- trunk/src/plugins/common/spellchecker/spellchecker.cpp (rev 0) +++ trunk/src/plugins/common/spellchecker/spellchecker.cpp 2011-07-27 17:59:42 UTC (rev 7561) @@ -0,0 +1,154 @@ +/* +* spellchecker.h, Author: Fabian Stock (Aiw...@go...) +* +* Copyright (C) 2001-2011 Atomic Blue (in...@pl..., http://www.atomicblue.org) +* +* +* 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 (version 2 of the License) +* 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. +* +*/ + +// crystalSpace includes +#include <cssysdef.h> +#include <iutil/plugin.h> +#include <iutil/vfs.h> +#include <iutil/stringarray.h> + +// my own includes +#include "spellchecker.h" + +SCF_IMPLEMENT_FACTORY(SpellChecker) + +SpellChecker::SpellChecker(iBase* parent): scfImplementationType(this, parent) +{ + return; +} + +SpellChecker::~SpellChecker() +{ + for(size_t i = 0; i < hunspellChecker.GetSize(); i++) + { + delete hunspellChecker.Get(i); + } + hunspellChecker.DeleteAll(); + return; +} + +bool SpellChecker::Initialize(iObjectRegistry* objReg) +{ + // lets register ourselves + csRef<iVFS> vfs = csQueryRegistry<iVFS > (objReg); + + csRef<iStringArray> files; + //search for dic files in the dict folder + files = vfs->FindFiles("/planeshift/data/dict/"); + + if(files.IsValid()) + { + for(size_t i = 0; i < files->GetSize(); i++) + { + csRef<iDataBuffer> AffPath; + csRef<iDataBuffer> DicPath; + csString file = files->Get(i); + csString fileAff; + + // less than 4 chars...can't be a dictionary + if(file.Length() <= 4) + continue; + + //check if the file is a dic file + if(file.Slice(file.Length()-4) != ".dic") + continue; + + //if so generate also the aff file path and add the dictionary to the list. + fileAff = file.Slice(0, file.Length()-3) + "aff"; + // Hunspell needs real file pathes...so get them from the VFS + AffPath = vfs->GetRealPath(fileAff); + DicPath = vfs->GetRealPath(file); + // create the spellchecker object + hunspellChecker.Push(new Hunspell(AffPath->GetData(), DicPath->GetData())); + } + } + return true; +} + +void SpellChecker::addWord(csString newWord) +{ + // do nothing if no dictionary was loaded otherwise add the word to the first dictionary in memory + if (hunspellChecker.GetSize() != 0) + { + // remove special chars from the words...not really nice to do this but otherwise a user could add words that by no way could be recognized + // as those chars will never be presented to the specllchecker + removeSpecialChars(newWord); + // now add it to the dictionary...happens only in memory and is not persistant + if (!(hunspellChecker[0]->add(newWord.GetData()))) + { + // also add it to the list of personal dictionary words...needed to save and change those words + personalDict.Push(newWord); + } + } +} + +void SpellChecker::clearPersonalDict() +{ + // do nothing if no dictionary was loaded (means that we also never added any custom words to a dictionary) + if (hunspellChecker.GetSize() != 0) + { + // first we have to remove the words from the dictionary again. + for (size_t i = 0; i < personalDict.GetSize(); i++) + { + hunspellChecker[0]->remove(personalDict[i].GetData()); + } + // now we clear the array as well + personalDict.DeleteAll(); + } +} + +bool SpellChecker::correct(csString wordToCheck) +{ + // first we remove some chars that might confuse the spellchecker + removeSpecialChars(wordToCheck); + // now we check the word with all available dictionaries + for(size_t i = 0; i < hunspellChecker.GetSize(); i++) + { + if(hunspellChecker.Get(i)->spell(wordToCheck.GetDataSafe())) + { + // as soon as the word is in one of the dicts it's considered correct + return true; + } + } + // not in any dict? Must be a type + return false; +} + +void SpellChecker::removeSpecialChars(csString& str) +{ + // remove all chars that interfere with spellchecking of a word + // ("'" can't be removed so that "don't" is recognized correctly.... + // this will lead to things like "/me says 'Hello'" being displayed as error + str.ReplaceAll("?", ""); + str.ReplaceAll("!", ""); + str.ReplaceAll("\"", ""); + str.ReplaceAll("*", ""); + str.ReplaceAll(".", ""); + str.ReplaceAll(",", ""); + str.ReplaceAll(";", ""); + str.ReplaceAll(":", ""); + str.ReplaceAll("-", ""); + str.ReplaceAll("(", ""); + str.ReplaceAll(")", ""); + str.ReplaceAll("{", ""); + str.ReplaceAll("}", ""); + str.ReplaceAll("[", ""); + str.ReplaceAll("]", ""); + str.ReplaceAll("/", ""); +} Property changes on: trunk/src/plugins/common/spellchecker/spellchecker.cpp ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:eol-style + native Added: trunk/src/plugins/common/spellchecker/spellchecker.csplugin =================================================================== --- trunk/src/plugins/common/spellchecker/spellchecker.csplugin (rev 0) +++ trunk/src/plugins/common/spellchecker/spellchecker.csplugin 2011-07-27 17:59:42 UTC (rev 7561) @@ -0,0 +1,12 @@ +<?xml version="1.0"?> +<plugin> + <scf> + <classes> + <class> + <name>crystalspace.planeshift.spellchecker</name> + <implementation>SpellChecker</implementation> + <description>Hunspell-Spellchecker</description> + </class> + </classes> + </scf> +</plugin> Property changes on: trunk/src/plugins/common/spellchecker/spellchecker.csplugin ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:eol-style + native Added: trunk/src/plugins/common/spellchecker/spellchecker.h =================================================================== --- trunk/src/plugins/common/spellchecker/spellchecker.h (rev 0) +++ trunk/src/plugins/common/spellchecker/spellchecker.h 2011-07-27 17:59:42 UTC (rev 7561) @@ -0,0 +1,67 @@ +/* +* spellchecker.h, Author: Fabian Stock (Aiw...@go...) +* +* Copyright (C) 2001-2011 Atomic Blue (in...@pl..., http://www.atomicblue.org) +* +* +* 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 (version 2 of the License) +* 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. +* +*/ + +#ifndef _SPELLCHECKER_H_ +#define _SPELLCHECKER_H_ + +// crystalSpace includes +#include <iutil/comp.h> + +// Hunspell includes +#include <hunspell.hxx> + +//my own includes +#include <ispellchecker.h> + +struct iObjectRegistry; + +/** + * CS Plugin for a hunspell spellchecker + */ +class SpellChecker : public scfImplementation2<SpellChecker, iSpellChecker, iComponent> +{ + public: + SpellChecker(iBase* parent); + virtual ~SpellChecker(); + + //from iComponent + virtual bool Initialize(iObjectRegistry* objReg); + + //from iSpellChecker + virtual void addWord(csString newWord); + virtual const csArray<csString>& getPersonalDict() {return personalDict;}; + virtual void clearPersonalDict(); + virtual bool hasDicts() {return hunspellChecker.GetSize() != 0;}; + virtual bool correct(csString wordToCheck); + protected: + /** Helper method that removes chars from a string which confuse the spellchecker. Called from "bool correct(csString wordToCheck)" + */ + virtual void removeSpecialChars(csString& str); + private: + /** array hunspell spellchecker class (for each found dictionary= + */ + csArray<Hunspell*> hunspellChecker; + /** array containing the words of the personal dictionary + */ + csArray<csString> personalDict; + +}; + + +#endif // _SPELLCHECKER_H_ Property changes on: trunk/src/plugins/common/spellchecker/spellchecker.h ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |