opencc-0.1.0.0: OpenCC bindings
LicenseMIT
StabilityExperimental
PortabilityPOSIX
Safe HaskellNone
LanguageHaskell2010

Text.OpenCC

Description

This module exposes higher-level binding to OpenCC than Text.OpenCC.Raw. OpenCC resources are managed automatically, and large string objects are passed without being copied.

There are three sets of higher-level bindings.

  1. IO handles. You work in IO monad and work with OpenCC handles directly.
  2. One-shot. convert1 directly converts a String.
  3. Monadic. You work in OpenCCM monad where you can use convert.

Caveat: the one-shot interface is unsafe, but as long as OpenCC is not shared or you don't care about errors, you will be fine.

defaultTradToSimp and defaultSimpToTrad are suggested by OpenCC.

Synopsis

Documentation

convert1 :: String -> Text -> Maybe Text Source #

Do a one-shot conversion. Note that this might affect the outcome of lastError, and thus unsafe (despite the pureness suggested by the signature).

convert1 defaultSimpToTrad "头发发财"

type OpenCC = ForeignPtr () Source #

OpenCC handle plus the finalizer. The OpenCC instance will be finalized when the object is garbage collected.

open :: String -> MaybeT IO OpenCC Source #

Open a new OpenCC session with specified configuration. Nothing is returned if error happens, and the error message can be retrieved from lastError.

lastError :: IO Text Source #

Return the last error message. This function is NOT thread-safe.

convertIO :: OpenCC -> Text -> IO Text Source #

Use an OpenCC handle to do the conversion. The result is a UTF-8 encoded text.

data OpenCCM a Source #

The OpenCC environment. In this environment, any conversion happens within a single OpenCC instance created by withOpenCC.

Instances

Instances details
Monad OpenCCM Source # 
Instance details

Defined in Text.OpenCC

Methods

(>>=) :: OpenCCM a -> (a -> OpenCCM b) -> OpenCCM b #

(>>) :: OpenCCM a -> OpenCCM b -> OpenCCM b #

return :: a -> OpenCCM a #

Functor OpenCCM Source # 
Instance details

Defined in Text.OpenCC

Methods

fmap :: (a -> b) -> OpenCCM a -> OpenCCM b #

(<$) :: a -> OpenCCM b -> OpenCCM a #

Applicative OpenCCM Source # 
Instance details

Defined in Text.OpenCC

Methods

pure :: a -> OpenCCM a #

(<*>) :: OpenCCM (a -> b) -> OpenCCM a -> OpenCCM b #

liftA2 :: (a -> b -> c) -> OpenCCM a -> OpenCCM b -> OpenCCM c #

(*>) :: OpenCCM a -> OpenCCM b -> OpenCCM b #

(<*) :: OpenCCM a -> OpenCCM b -> OpenCCM a #

MonadIO OpenCCM Source # 
Instance details

Defined in Text.OpenCC

Methods

liftIO :: IO a -> OpenCCM a #

withOpenCC :: String -> OpenCCM a -> MaybeT IO a Source #

Open an OpenCC environment (which is MonadIO), where convert is available.

unsafeWithOpenCC :: String -> OpenCCM a -> Maybe a Source #

Same as withOpenCC but the result is not IO. This is unsafe, and the same safety conditions as convert1 apply here.

convert :: Text -> OpenCCM Text Source #

Convert a string in the current environment.

defaultSimpToTrad :: String Source #

Filename of default Simplified to Traditional configuration

defaultTradToSimp :: String Source #

Filename of default Traditional to Simplified configuration