packages feed

cjk 0.1.0.0 → 0.1.0.1

raw patch · 9 files changed

+67/−20 lines, 9 filesPVP: minor bump suggested

API additions: PVP suggests at least a minor version bump

API changes (from Hackage documentation)

+ CJK.Data.CEDICT: showReadingAccented :: Reading -> String
+ CJK.Data.Pinyin: combiningMarkTone :: Char -> Maybe Tone
+ CJK.Data.Pinyin: toAccented :: Phone -> Text
+ CJK.Data.Pinyin: toneCombiningMark :: Tone -> Maybe Char

Files

CJK/Data/CEDICT.hs view
@@ -1,6 +1,6 @@ {-# LANGUAGE OverloadedStrings, PatternGuards #-} module CJK.Data.CEDICT (-    Reading, showReading,+    Reading, showReading, showReadingAccented,     Word(..), showHeadWord,     DefinitionToken(..), WordDefinition(..), Definition(..),     entries@@ -33,22 +33,27 @@ type Reading = [Either Text.Text Phone]  showReading :: Reading -> String-showReading yins = "[" ++ intercalate " " (map (either Text.unpack show) yins) ++ "]"+showReading yins = intercalate " " (map (either Text.unpack show) yins) +showReadingAccented :: Reading -> String+showReadingAccented yins = intercalate " " (map (either Text.unpack (Text.unpack . toAccented)) yins) + data Word = Word {     traditional :: [Char],     simplified  :: [Char],     reading     :: Reading   } +bracketed s = "[" ++ s ++ "]"+ instance Show Word where-    show word | simplified word == traditional word = traditional word                           ++ showReading (reading word)-              | otherwise                           = traditional word ++ "|" ++ simplified word ++ showReading (reading word)+    show word | simplified word == traditional word = traditional word                           ++ bracketed (showReading (reading word))+              | otherwise                           = traditional word ++ "|" ++ simplified word ++ bracketed (showReading (reading word))  -- | Show a word as in the head of a dictionary entry showHeadWord :: Word -> String-showHeadWord word = traditional word ++ " " ++ simplified word ++ " " ++ showReading (reading word)+showHeadWord word = traditional word ++ " " ++ simplified word ++ " " ++ bracketed (showReading (reading word))  mkWord :: [Char] -> [Char] -> Reading -> Word -- Fix problems in dictionary:@@ -66,7 +71,7 @@       -- Check for 市 suffix which is missing in yins in examples like 鹿泉市       | ntrad == nyins + 1, '市'                        <- last trad -> Word trad simp (yins ++ [Right (Phone "shi" Falling)])       -- Last-ditch check for an unhandled error-      | ntrad /= nyins -> error $ "mkWord: differing numbers of characters and readings (" ++ show trad ++ " vs. " ++ showReading yins ++ ")"+      | ntrad /= nyins -> error $ "mkWord: differing numbers of characters and readings (" ++ show trad ++ " vs. " ++ bracketed (showReading yins) ++ ")"       | otherwise      -> Word trad simp yins   where ntrad = length trad         nsimp = length simp@@ -100,7 +105,7 @@  {-# NOINLINE contents #-} contents :: TextL.Text-contents = unsafePerformIO (readUTF8File "data/cedict_1_0_ts_utf-8_mdbg.txt")+contents = unsafePerformIO (readUTF8DataFile "data/cedict_1_0_ts_utf-8_mdbg.txt")  entries :: [Definition] entries = parseLazy fileP contents
CJK/Data/Pinyin.hs view
@@ -23,7 +23,23 @@ toneNumber Falling       = 4 toneNumber Neutral       = 5 +-- | Returns the Unicode combining character used to produce the accent for this tone. Returns Nothing if no accent is required.+toneCombiningMark :: Tone -> Maybe Char+toneCombiningMark Flat          = Just '\x304'+toneCombiningMark Rising        = Just '\x301'+toneCombiningMark FallingRising = Just '\x30C'+toneCombiningMark Falling       = Just '\x300'+toneCombiningMark Neutral       = Nothing +-- | Returns the tone associated with this Unicode combining character, if any.+combiningMarkTone :: Char -> Maybe Tone+combiningMarkTone '\x304' = Just Flat+combiningMarkTone '\x301' = Just Rising+combiningMarkTone '\x30C' = Just FallingRising+combiningMarkTone '\x300' = Just Falling+combiningMarkTone _       = Nothing++ data Phone = Phone {     sound :: Text.Text,     tone  :: Tone@@ -36,11 +52,27 @@ fromAccented s = go [] Nothing $ Text.unpack $ Text.normalize Text.NFD s   where go !tser !mb_tone cs = case cs of             []           -> Phone { sound = Text.pack (reverse tser), tone = fromMaybe Neutral mb_tone }-            ('\x304':cs) -> go tser     (jst Flat)          cs-            ('\x301':cs) -> go tser     (jst Rising)        cs-            ('\x30C':cs) -> go tser     (jst FallingRising) cs-            ('\x300':cs) -> go tser     (jst Falling)       cs-            (c:cs)       -> go (c:tser) mb_tone             cs+            (c:cs)       -> case combiningMarkTone c of+                              Just tone -> go tser     (jst tone) cs+                              Nothing   -> go (c:tser) mb_tone    cs           where jst tone' = case mb_tone of-                              Nothing   -> Just tone'-                              Just tone -> error $ "Conflicting tones " ++ show tone ++ " and " ++ show tone' ++ " in " ++ Text.unpack s+                              Just tone | tone /= tone' -> error $ "Conflicting tones " ++ show tone ++ " and " ++ show tone' ++ " in " ++ Text.unpack s+                              _                         -> Just tone' -- Allow multiple tones of the same time, even if it is technically incorrect++toAccented :: Phone -> Text.Text+toAccented yin = Text.pack $ go $ Text.unpack $ sound yin+  where go []                 = show (tone yin) -- All pinyin contain a vowel, so this can only happen when the pinyin is in fact invalid+        go (c:cs) | isVowel c = maybeToList (toneCombiningMark (tone yin)) ++ c:cs+                  | otherwise = c : go cs++        isVowel 'a' = True+        isVowel 'e' = True+        isVowel 'i' = True+        isVowel 'o' = True+        isVowel 'u' = True+        isVowel 'A' = True+        isVowel 'E' = True+        isVowel 'I' = True+        isVowel 'O' = True+        isVowel 'U' = True+        isVowel _   = False
CJK/Data/Unihan/DictionaryLikeData.hs view
@@ -123,7 +123,7 @@  {-# NOINLINE contents #-} contents :: TextL.Text-contents = unsafePerformIO (readUTF8File "data/Unihan/Unihan_DictionaryLikeData.txt")+contents = unsafePerformIO (readUTF8DataFile "data/Unihan/Unihan_DictionaryLikeData.txt")  dictionaryLikes :: DictionaryLikesMap dictionaryLikes = parseLazy fileP contents
CJK/Data/Unihan/NumericValues.hs view
@@ -39,7 +39,7 @@  {-# NOINLINE contents #-} contents :: TextL.Text-contents = unsafePerformIO (readUTF8File "data/Unihan/Unihan_NumericValues.txt")+contents = unsafePerformIO (readUTF8DataFile "data/Unihan/Unihan_NumericValues.txt")  numericValues :: NumericValuesMap numericValues = parseLazy fileP contents
CJK/Data/Unihan/RadicalStrokeCounts.hs view
@@ -84,7 +84,7 @@  {-# NOINLINE contents #-} contents :: TextL.Text-contents = unsafePerformIO (readUTF8File "data/Unihan/Unihan_RadicalStrokeCounts.txt")+contents = unsafePerformIO (readUTF8DataFile "data/Unihan/Unihan_RadicalStrokeCounts.txt")  strokeCounts :: StrokeCountsMap strokeCounts = parseLazy fileP contents
CJK/Data/Unihan/Readings.hs view
@@ -183,7 +183,7 @@  {-# NOINLINE contents #-} contents :: TextL.Text-contents = unsafePerformIO (readUTF8File "data/Unihan/Unihan_Readings.txt")+contents = unsafePerformIO (readUTF8DataFile "data/Unihan/Unihan_Readings.txt")  readings :: ReadingsMap readings = parseLazy fileP contents
CJK/Data/Unihan/Variants.hs view
@@ -75,7 +75,7 @@  {-# NOINLINE contents #-} contents :: TextL.Text-contents = unsafePerformIO (readUTF8File "data/Unihan/Unihan_Variants.txt")+contents = unsafePerformIO (readUTF8DataFile "data/Unihan/Unihan_Variants.txt")  variants :: VariantsMap variants = parseLazy fileP contents
CJK/Utilities.hs view
@@ -13,6 +13,13 @@ import qualified Data.Attoparsec.Text as DAT import qualified Data.Attoparsec.Text.Lazy as DATL +import Paths_cjk+++readUTF8DataFile :: FilePath -> IO TextL.Text+readUTF8DataFile fp = do+    full_fp <- getDataFileName fp+    readUTF8File full_fp  readUTF8File :: FilePath -> IO TextL.Text readUTF8File = fmap TextL.decodeUtf8 . BS.readFile
cjk.cabal view
@@ -1,5 +1,5 @@ name:                cjk-version:             0.1.0.0+version:             0.1.0.1 synopsis:            Data about Chinese, Japanese and Korean characters and languages description:         A Haskell interface to the most important information from the Unicode Unihan character                      database and CC-CEDICT free Chinese-English dictionary.@@ -31,6 +31,9 @@                      CJK.Data.Types   other-modules:     CJK.Data.Internal                      CJK.Utilities+                     -- I don't think I should have to put this here, but if I don't then any executables+                     -- linking against the cjk library will fail to find symbols exported by this module+                     Paths_cjk   build-depends:       base >=4.5 && < 5                      , containers >=0.4.2                      , bytestring >=0.9