packages feed

mecab 0.2.0 → 0.3.0

raw patch · 2 files changed

+57/−35 lines, 2 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

- Text.MeCab: alpha :: Node -> CFloat
- Text.MeCab: beta :: Node -> CFloat
- Text.MeCab: charType :: Node -> CUChar
- Text.MeCab: cost :: Node -> CLong
- Text.MeCab: feature :: Node -> Text
- Text.MeCab: id :: Node -> CUInt
- Text.MeCab: instance Eq Node
- Text.MeCab: instance Read Node
- Text.MeCab: instance Show Node
- Text.MeCab: isBest :: Node -> Bool
- Text.MeCab: lcAttr :: Node -> CUShort
- Text.MeCab: posid :: Node -> CUShort
- Text.MeCab: prob :: Node -> CFloat
- Text.MeCab: rcAttr :: Node -> CUShort
- Text.MeCab: rlength :: Node -> CUShort
- Text.MeCab: stat :: Node -> Stat
- Text.MeCab: surface :: Node -> Text
- Text.MeCab: wcost :: Node -> CShort
+ Text.MeCab: class MeCabString str
+ Text.MeCab: fromBS :: MeCabString str => ByteString -> str
+ Text.MeCab: instance Eq str => Eq (Node str)
+ Text.MeCab: instance MeCabString ByteString
+ Text.MeCab: instance MeCabString String
+ Text.MeCab: instance MeCabString Text
+ Text.MeCab: instance Read str => Read (Node str)
+ Text.MeCab: instance Show str => Show (Node str)
+ Text.MeCab: nodeAlpha :: Node str -> CFloat
+ Text.MeCab: nodeBeta :: Node str -> CFloat
+ Text.MeCab: nodeCharType :: Node str -> CUChar
+ Text.MeCab: nodeCost :: Node str -> CLong
+ Text.MeCab: nodeFeature :: Node str -> str
+ Text.MeCab: nodeId :: Node str -> CUInt
+ Text.MeCab: nodeIsBest :: Node str -> Bool
+ Text.MeCab: nodeLcAttr :: Node str -> CUShort
+ Text.MeCab: nodePosid :: Node str -> CUShort
+ Text.MeCab: nodeProb :: Node str -> CFloat
+ Text.MeCab: nodeRcAttr :: Node str -> CUShort
+ Text.MeCab: nodeRlength :: Node str -> CUShort
+ Text.MeCab: nodeStat :: Node str -> Stat
+ Text.MeCab: nodeSurface :: Node str -> str
+ Text.MeCab: nodeWcost :: Node str -> CShort
+ Text.MeCab: toBS :: MeCabString str => str -> ByteString
- Text.MeCab: Node :: Text -> Text -> CUShort -> CUInt -> CUShort -> CUShort -> CUShort -> CUChar -> Stat -> Bool -> CFloat -> CFloat -> CFloat -> CShort -> CLong -> Node
+ Text.MeCab: Node :: str -> str -> CUShort -> CUInt -> CUShort -> CUShort -> CUShort -> CUChar -> Stat -> Bool -> CFloat -> CFloat -> CFloat -> CShort -> CLong -> Node str
- Text.MeCab: data Node
+ Text.MeCab: data Node str
- Text.MeCab: next :: MeCab -> IO (Maybe Text)
+ Text.MeCab: next :: MeCabString str => MeCab -> IO (Maybe str)
- Text.MeCab: nextNode :: MeCab -> IO [Node]
+ Text.MeCab: nextNode :: MeCabString str => MeCab -> IO [Node str]
- Text.MeCab: parse :: MeCab -> Text -> IO Text
+ Text.MeCab: parse :: MeCabString str => MeCab -> str -> IO str
- Text.MeCab: parseNBest :: MeCab -> Int -> Text -> IO Text
+ Text.MeCab: parseNBest :: MeCabString str => MeCab -> Int -> str -> IO str
- Text.MeCab: parseNBestInit :: MeCab -> Text -> IO ()
+ Text.MeCab: parseNBestInit :: MeCabString str => MeCab -> str -> IO ()
- Text.MeCab: parseToNode :: MeCab -> Text -> IO [Node]
+ Text.MeCab: parseToNode :: MeCabString str => MeCab -> str -> IO [Node str]

Files

Text/MeCab.hsc view
@@ -1,5 +1,7 @@ {-# LANGUAGE ForeignFunctionInterface #-} {-# LANGUAGE DeriveDataTypeable #-}+{-# LANGUAGE TypeSynonymInstances #-}+{-# LANGUAGE FlexibleInstances #-}  module Text.MeCab(   Text.MeCab.new,@@ -7,6 +9,8 @@      version,   +  MeCabString(..),+     parse,   parseNBest,   @@ -37,7 +41,7 @@ import Foreign import Foreign.C -import Prelude hiding (id)+import Prelude  #include <mecab.h> @@ -89,35 +93,53 @@  -- -parse :: MeCab -> T.Text -> IO T.Text+class MeCabString str where+  toBS :: str -> B.ByteString+  fromBS :: B.ByteString -> str++instance MeCabString String where+  toBS = toBS . T.pack+  fromBS = T.unpack . fromBS++instance MeCabString B.ByteString where+  toBS = id+  fromBS = id++instance MeCabString T.Text where+  toBS = T.encodeUtf8+  fromBS = T.decodeUtf8++--++parse :: MeCabString str => MeCab -> str -> IO str parse m txt = withForeignPtr (unMeCab m) $ \pm ->-  B.useAsCStringLen (T.encodeUtf8 txt) $ \(pstr, len) -> do+  B.useAsCStringLen (toBS txt) $ \(pstr, len) -> do     p <- mecab_sparse_tostr2 pm pstr (fromIntegral len)     when (p == nullPtr) $ throwIO =<< (MeCabError <$> strerror pm)     packCString p -parseNBest :: MeCab -> Int -> T.Text -> IO T.Text+parseNBest :: MeCabString str => MeCab -> Int -> str -> IO str parseNBest m n txt = withForeignPtr (unMeCab m) $ \pm ->-  B.useAsCStringLen (T.encodeUtf8 txt) $ \(pstr, len) -> do+  B.useAsCStringLen (toBS txt) $ \(pstr, len) -> do     p <- mecab_nbest_sparse_tostr2 pm (fromIntegral n) pstr (fromIntegral len)     when (p == nullPtr) $ throwIO =<< (MeCabError <$> strerror pm)     packCString p -parseNBestInit :: MeCab -> T.Text -> IO ()+parseNBestInit :: MeCabString str => MeCab -> str -> IO () parseNBestInit m txt = withForeignPtr (unMeCab m) $ \pm ->-  B.useAsCStringLen (T.encodeUtf8 txt) $ \(pstr, len) -> do+  B.useAsCStringLen (toBS txt) $ \(pstr, len) -> do     ret <- mecab_nbest_init2 pm pstr (fromIntegral len)     when (ret /= 1) $ throwIO =<< (MeCabError <$> strerror pm) -next :: MeCab -> IO (Maybe T.Text)+next :: MeCabString str => MeCab -> IO (Maybe str) next m = withForeignPtr (unMeCab m) $ \pm -> do   r <- mecab_nbest_next_tostr pm   if r == nullPtr     then return Nothing     else Just <$> packCString r -packCString :: CString -> IO T.Text-packCString p = T.decodeUtf8 <$> B.packCString p+packCString :: MeCabString str => CString -> IO str+packCString p = fromBS <$> B.packCString p  -- @@ -125,26 +147,26 @@   NOR | UNK | BOS | EOS   deriving (Eq, Read, Show) -data Node =+data Node str =   Node-  { surface :: T.Text-  , feature :: T.Text-  , rlength :: CUShort-  , id :: CUInt-  , rcAttr :: CUShort-  , lcAttr :: CUShort-  , posid :: CUShort-  , charType :: CUChar-  , stat :: Stat-  , isBest :: Bool-  , alpha :: CFloat-  , beta :: CFloat-  , prob :: CFloat-  , wcost :: CShort-  , cost :: CLong+  { nodeSurface :: str+  , nodeFeature :: str+  , nodeRlength :: CUShort+  , nodeId :: CUInt+  , nodeRcAttr :: CUShort+  , nodeLcAttr :: CUShort+  , nodePosid :: CUShort+  , nodeCharType :: CUChar+  , nodeStat :: Stat+  , nodeIsBest :: Bool+  , nodeAlpha :: CFloat+  , nodeBeta :: CFloat+  , nodeProb :: CFloat+  , nodeWcost :: CShort+  , nodeCost :: CLong   } deriving (Eq, Read, Show) -peekNodes :: Ptr Node -> IO [Node]+peekNodes :: MeCabString str => Ptr (Node str) -> IO [Node str] peekNodes ptr   | ptr == nullPtr =     return []@@ -152,12 +174,12 @@       (:) <$> peekNode ptr           <*> (peekNodes =<< (#peek mecab_node_t, next) ptr) -peekNode :: Ptr Node -> IO Node+peekNode :: MeCabString str => Ptr (Node str) -> IO (Node str) peekNode ptr = do   sfc <- do     p <- (#peek mecab_node_t, surface) ptr     len <- (#peek mecab_node_t, length) ptr-    T.decodeUtf8 <$> B.packCStringLen (p, fromIntegral (len :: CUShort))+    fromBS <$> B.packCStringLen (p, fromIntegral (len :: CUShort))   Node     <$> return sfc     <*> (packCString =<< (#peek mecab_node_t, feature) ptr)@@ -182,14 +204,14 @@     toStat (#const MECAB_EOS_NODE) = EOS     toStat _ = UNK -parseToNode :: MeCab -> T.Text -> IO [Node]+parseToNode :: MeCabString str => MeCab -> str -> IO [Node str] parseToNode m txt = withForeignPtr (unMeCab m) $ \pm ->-  B.useAsCStringLen (T.encodeUtf8 txt) $ \(pstr, len) -> do+  B.useAsCStringLen (toBS txt) $ \(pstr, len) -> do     p <- mecab_sparse_tonode2 pm pstr (fromIntegral len)     when (p == nullPtr) $ throwIO =<< (MeCabError <$> strerror pm)     peekNodes p  -nextNode :: MeCab -> IO  [Node]+nextNode :: MeCabString str => MeCab -> IO  [Node str] nextNode m = withForeignPtr (unMeCab m) $ \pm -> do   p <- mecab_nbest_next_tonode pm   when (p == nullPtr) $ throwIO =<< (MeCabError <$> strerror pm)@@ -259,10 +281,10 @@   :: Ptr MeCab -> IO CString  foreign import ccall mecab_sparse_tonode2-  :: Ptr MeCab -> CString -> CSize -> IO (Ptr Node)+  :: Ptr MeCab -> CString -> CSize -> IO (Ptr (Node a))  foreign import ccall mecab_nbest_next_tonode-  :: Ptr MeCab -> IO (Ptr Node)+  :: Ptr MeCab -> IO (Ptr (Node a))  foreign import ccall mecab_get_partial   :: Ptr MeCab -> IO CInt
mecab.cabal view
@@ -1,5 +1,5 @@ Name:                mecab-Version:             0.2.0+Version:             0.3.0 License:             BSD3 License-File:        LICENSE Author:              Hideyuki Tanaka