bindings-libstemmer (empty) → 0.1.0.0
raw patch · 7 files changed
+219/−0 lines, 7 filesdep +basedep +bindings-DSLdep +resourcetsetup-changed
Dependencies added: base, bindings-DSL, resourcet, transformers
Files
- Bindings/Stemmer.hs +100/−0
- Bindings/Stemmer/Raw.hsc +16/−0
- Bindings/Stemmer/Simple.hs +23/−0
- LICENSE +20/−0
- README.md +21/−0
- Setup.hs +2/−0
- bindings-libstemmer.cabal +37/−0
+ Bindings/Stemmer.hs view
@@ -0,0 +1,100 @@+module Bindings.Stemmer+ ( Encoding(..)+ , Language(..)+ , StemConfig(..)+ , Stemmer(..)+ , init_stemmer+ , new_stemmer+ , stemword+ , delete_stemmer+ , unsafeStemword ) where++import Bindings.Stemmer.Raw+import Foreign.C.String+import System.IO.Unsafe (unsafePerformIO)+import Data.Char (toLower)+import Foreign.Ptr++-- | 'Encoding' Type+--+-- === NOTE:+--+-- * 'ISO_8859_2' for 'Romanian' | 'Hungarian' only.+-- * 'KOI8_R' for 'Russian' only.+data Encoding = UTF_8+ | ISO_8859_1+ | ISO_8859_2+ | KOI8_R+ deriving Show++-- | 'Language' Type+data Language = Danish+ | Dutch+ | English+ | Finnish+ | French+ | German+ | Hungarian+ | Italian+ | Norwegian+ | Porter+ | Portuguese+ | Romanian+ | Russian+ | Spanish+ | Swedish+ | Turkish+ deriving Show++-- | 'StemConfig' Type+data StemConfig = StemConfig { language :: Language+ , encoding :: Encoding }+ deriving Show++-- | 'Stemmer' Type+--+-- Wrapper type for Ptr C'sb_stemmer.+type Stemmer = Ptr C'sb_stemmer++-- | create 'StemConfig' type+--+-- * algorithm: 'Language'+--+-- * encoding: 'Encoding'+init_stemmer :: Language -> Encoding -> IO StemConfig+init_stemmer lang enc = do+ return StemConfig { language = lang+ , encoding = enc }++-- | create stemmer instance+new_stemmer :: StemConfig -> IO Stemmer+new_stemmer StemConfig{..} = do+ cword_enc <- encodingCString encoding+ algorithm <- languageCString language+ stemmer <- c'sb_stemmer_new algorithm cword_enc+ return stemmer++-- | stem word with 'Stemmer'+stemword :: Stemmer -> String -> IO String+stemword stemmer word = do+ cword <- newCString word+ strPtr <- c'sb_stemmer_stem stemmer cword (fromIntegral $ length word)+ str_length <- c'sb_stemmer_length stemmer+ peekCStringLen (strPtr, fromIntegral str_length)++-- | delete stemmer instance+delete_stemmer :: Stemmer -> IO ()+delete_stemmer = c'sb_stemmer_delete++-- | stem words with unsafePerformIO+unsafeStemword :: Stemmer -> String -> String+unsafeStemword stemmer word = unsafePerformIO $ stemword stemmer word++-- | 'Encoding' Type Util function+encodingCString :: Encoding -> IO CString+encodingCString = newCString . show++-- | 'Language' Type Util function+languageCString :: Language -> IO CString+languageCString = newCString . go . show+ where go (x:xs) = (toLower x) : xs
+ Bindings/Stemmer/Raw.hsc view
@@ -0,0 +1,16 @@+{-# OPTIONS_GHC -fno-warn-unused-imports #-}+#include <bindings.dsl.h>+#include <libstemmer.h>+module Bindings.Stemmer.Raw where+import Foreign.Ptr+#strict_import++{- struct sb_stemmer; -}+#opaque_t struct sb_stemmer+{- typedef unsigned char sb_symbol; -}+#synonym_t sb_symbol , CUChar+#ccall sb_stemmer_list , IO (Ptr CString)+#ccall sb_stemmer_new , CString -> CString -> IO (Ptr <struct sb_stemmer>)+#ccall sb_stemmer_delete , Ptr <struct sb_stemmer> -> IO ()+#ccall sb_stemmer_stem , Ptr <struct sb_stemmer> -> CString -> CInt -> IO CString+#ccall sb_stemmer_length , Ptr <struct sb_stemmer> -> IO CInt
+ Bindings/Stemmer/Simple.hs view
@@ -0,0 +1,23 @@+module Bindings.Stemmer.Simple+ ( Stemmer(..)+ , Language(..)+ , Encoding(..)+ , init_stemmer+ , simple_stem ) where++import Bindings.Stemmer (StemConfig(..), Stemmer(..),+ Language(..), Encoding(..),+ init_stemmer, new_stemmer,+ delete_stemmer, stemword)+import Control.Monad.Trans.Resource (allocate, runResourceT, release)+import Control.Monad.Trans.Class (lift)++-- | stem word with 'ResourceT'.+--+-- new & delete 'Stemmer' a.k.a (Ptr C'sb_stemmer) automatically.+simple_stem :: StemConfig -> String -> IO String+simple_stem StemConfig{..} word = runResourceT $ do+ (key, stemmer) <- allocate (new_stemmer StemConfig{..}) delete_stemmer+ str <- lift $ stemword stemmer word+ lift $ release key+ return str
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2014 cosmo0920++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ README.md view
@@ -0,0 +1,21 @@+Bindings-Libstemmer+===++low level binding for libstemmer.++## Requirement++* libstemmer_c++### for Ubuntu++```bash+$ sudo apt-get install libstemmer-dev+```++### for OS X++```bash+$ brew tap cosmo0920/tokenizers+$ brew install cosmo0920/tokenizers/libstemmer+```
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ bindings-libstemmer.cabal view
@@ -0,0 +1,37 @@+-- Initial bindings-libstemmer.cabal generated by cabal init. For further+-- documentation, see http://haskell.org/cabal/users-guide/++name: bindings-libstemmer+version: 0.1.0.0+synopsis: Binding for libstemmer with low level binding.+description: A binding for libstemmer.+license: MIT+license-file: LICENSE+author: cosmo0920+maintainer: cosmo0920.wp@gmail.com+-- copyright:+category: FFI+build-type: Simple+extra-source-files: README.md+cabal-version: >=1.10++library+ exposed-modules: Bindings.Stemmer.Raw+ Bindings.Stemmer+ Bindings.Stemmer.Simple+ -- other-modules:+ -- other-extensions:+ extra-libraries: stemmer+ build-depends: base >= 4.5 && < 5+ , bindings-DSL >= 1.0 && < 1.2+ , transformers >= 0.3 && < 0.5+ , resourcet >= 1.1 && < 1.2+ hs-source-dirs: .+ default-language: Haskell2010+ build-tools: hsc2hs+ default-extensions: ForeignFunctionInterface+ CPP+ RecordWildCards+source-repository head+ type: git+ location: git://github.com/cosmo0920/bindings-libstemmer.git