diff options
author | Tobias Markmann <tm@ayena.de> | 2013-03-25 22:12:52 (GMT) |
---|---|---|
committer | Tobias Markmann <tm@ayena.de> | 2013-03-26 22:31:39 (GMT) |
commit | 20ead0a84fdd8c9e870e98ee6a2712bfa263d7fb (patch) | |
tree | 537b08f35ba08de612856d8376e171766212b98a /SwifTools | |
parent | 99063cc6e9168bf88d6c245d1f8bac02bd898c3d (diff) | |
download | swift-20ead0a84fdd8c9e870e98ee6a2712bfa263d7fb.zip swift-20ead0a84fdd8c9e870e98ee6a2712bfa263d7fb.tar.bz2 |
Adding basic support for native spell checking on Mac OS X.
Change-Id: Id29313a06f052ecbaef54be0c185cd7f1df375a2
License: This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details.
Diffstat (limited to 'SwifTools')
-rw-r--r-- | SwifTools/MacOSXChecker.h | 22 | ||||
-rw-r--r-- | SwifTools/MacOSXChecker.mm | 54 | ||||
-rw-r--r-- | SwifTools/SConscript | 6 | ||||
-rw-r--r-- | SwifTools/SpellChecker.h | 2 | ||||
-rw-r--r-- | SwifTools/SpellCheckerFactory.cpp | 5 | ||||
-rw-r--r-- | SwifTools/SpellCheckerFactory.h | 4 |
6 files changed, 92 insertions, 1 deletions
diff --git a/SwifTools/MacOSXChecker.h b/SwifTools/MacOSXChecker.h new file mode 100644 index 0000000..f2f8ebc --- /dev/null +++ b/SwifTools/MacOSXChecker.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2013 Tobias Markmann + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include <vector> +#include <boost/tuple/tuple.hpp> +#include <SwifTools/SpellChecker.h> + +namespace Swift { + class MacOSXChecker : public SpellChecker { + public: + MacOSXChecker(); + virtual ~MacOSXChecker(); + virtual bool isCorrect(const std::string& word); + virtual void getSuggestions(const std::string& word, std::vector<std::string>& list); + virtual void checkFragment(const std::string& fragment, PositionPairList& misspelledPositions); + }; +} diff --git a/SwifTools/MacOSXChecker.mm b/SwifTools/MacOSXChecker.mm new file mode 100644 index 0000000..3e687d1 --- /dev/null +++ b/SwifTools/MacOSXChecker.mm @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2013 Tobias Markmann + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include <SwifTools/MacOSXChecker.h> + +#include <algorithm> +#include <boost/algorithm/string.hpp> + +#include <AppKit/AppKit.h> + +namespace Swift { + +MacOSXChecker::MacOSXChecker() { +} + +MacOSXChecker::~MacOSXChecker() { +} + +bool MacOSXChecker::isCorrect(const std::string& /*word*/) { + // No content since it doesn't seem to be used anywhere. + return false; +} + +void MacOSXChecker::getSuggestions(const std::string& word, std::vector<std::string>& list) { + NSSpellChecker* spellChecker = [NSSpellChecker sharedSpellChecker]; + NSString* wordString = [[NSString alloc] initWithUTF8String: word.c_str()]; + NSArray* suggestions = [spellChecker guessesForWord: wordString]; + for(unsigned int i = 0; i < [suggestions count]; ++i) { + list.push_back(std::string([[suggestions objectAtIndex:i] UTF8String])); + } + [wordString release]; +} + +void MacOSXChecker::checkFragment(const std::string& fragment, PositionPairList& misspelledPositions) { + NSSpellChecker* spellChecker = [NSSpellChecker sharedSpellChecker]; + size_t nextLocation = 0; + NSRange range; + NSString *fragmentString = [[NSString alloc] initWithUTF8String: fragment.c_str()]; + do { + range = [spellChecker checkSpellingOfString:fragmentString startingAt:static_cast<long>(nextLocation)]; + if (range.location != NSNotFound) { + if (range.location < nextLocation) + break; + misspelledPositions.push_back(PositionPair(static_cast<int>(range.location), static_cast<int>(range.location + range.length))); + nextLocation = range.location + range.length + 1; + } + } while (range.location != NSNotFound); + [fragmentString release]; +} + +} diff --git a/SwifTools/SConscript b/SwifTools/SConscript index eaf5787..b30a79c 100644 --- a/SwifTools/SConscript +++ b/SwifTools/SConscript @@ -41,6 +41,12 @@ if env["SCONS_STAGE"] == "build" : "HunspellChecker.cpp", "SpellParser.cpp", ] + elif swiftools_env["PLATFORM"] == "darwin" : + sources += [ + "SpellCheckerFactory.cpp", + "MacOSXChecker.mm", + "SpellParser.cpp", + ] if swiftools_env.get("HAVE_SPARKLE", 0) : diff --git a/SwifTools/SpellChecker.h b/SwifTools/SpellChecker.h index 746fcaf..fd38418 100644 --- a/SwifTools/SpellChecker.h +++ b/SwifTools/SpellChecker.h @@ -20,7 +20,7 @@ namespace Swift { } virtual ~SpellChecker() { delete parser_; - }; + } virtual bool isCorrect(const std::string& word) = 0; virtual void getSuggestions(const std::string& word, std::vector<std::string>& list) = 0; virtual void checkFragment(const std::string& fragment, PositionPairList& misspelledPositions) = 0; diff --git a/SwifTools/SpellCheckerFactory.cpp b/SwifTools/SpellCheckerFactory.cpp index 6061d78..d068d90 100644 --- a/SwifTools/SpellCheckerFactory.cpp +++ b/SwifTools/SpellCheckerFactory.cpp @@ -9,9 +9,12 @@ #include <SwifTools/SpellChecker.h> #include <SwifTools/HunspellChecker.h> #include <SwifTools/SpellCheckerFactory.h> +#include <Swiften/Base/Platform.h> #ifdef HAVE_HUNSPELL #include <hunspell/hunspell.hxx> +#elif defined(SWIFTEN_PLATFORM_MACOSX) +#include <SwifTools/MacOSXChecker.h> #endif namespace Swift { @@ -27,6 +30,8 @@ SpellChecker* SpellCheckerFactory::createSpellChecker(const std::string& dictFil return new HunspellChecker(affixFile.c_str(), dictFile.c_str()); } // If dictionaries don't exist disable the checker +#elif defined(SWIFTEN_PLATFORM_MACOSX) + return new MacOSXChecker(); #endif return NULL; } diff --git a/SwifTools/SpellCheckerFactory.h b/SwifTools/SpellCheckerFactory.h index 086ea66..91118f9 100644 --- a/SwifTools/SpellCheckerFactory.h +++ b/SwifTools/SpellCheckerFactory.h @@ -6,8 +6,12 @@ #pragma once +#include <Swiften/Base/Platform.h> + #ifdef HAVE_HUNSPELL #define HAVE_SPELLCHECKER +#elif defined(SWIFTEN_PLATFORM_MACOSX) +#define HAVE_SPELLCHECKER #endif namespace Swift { |