Permalink
Browse files

apply Joao's patch

  • Loading branch information...
1 parent e8779a2 commit fd7a0e6770a3de55378c1de34e0de205d8372f50 @nfjinjing committed Jan 24, 2010
Showing with 22 additions and 96 deletions.
  1. +8 −0 changelog.md
  2. +3 −2 readme.md
  3. 0 src/Main.hs
  4. +3 −88 src/Text/Translate.hs
  5. +8 −6 translate.cabal
View
8 changelog.md
@@ -0,0 +1,8 @@
+# 2010.01.24 Joao F. Ferreira <joao@joaoff.com>
+
+ - Added support for language detection: `Text.Language.Detect`
+ - `Text.Translate` is now `Text.Language.Translate`, but I've maintained
+ `Text.Translate` for backward compatibility
+ - Fixed bug with URI escaping. Google's API is expecting an ASCII string
+ that is first escaped with UTF-8.
+ - Refactored parts of the module to avoid repeated code
View
5 readme.md
@@ -10,6 +10,7 @@ Haskell binding to Google translate
Just "bonjour"
+ > :m Text.Language.Detect
+ > detect "Program testing can be used to show the presence of bugs, but never to show their absence!"
-
-
+ Just ("en",True,0.87355334)
View
0 src/Main.hs
No changes.
View
91 src/Text/Translate.hs
@@ -2,92 +2,7 @@
module Text.Translate (translate) where
-import Text.JSON.Generic
-import Network.Curl
-import qualified Data.List as L
-import Prelude hiding ((.), (-))
-import Network.URI
+-- This module is just a wrapper to Text.Language.Translate
+-- It exists for backward compatibility.
--- http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&langpair=en|de&q=Hello+World
-
-{-
-{"responseData"=>{"translatedText"=>"Hallo Welt"},
- "responseStatus"=>200,
- "responseDetails"=>nil}
--}
-
-curl :: String -> IO (Maybe String)
-curl x = do
- (r, s) <- curlGetString x []
- if r == CurlOK
- then return - Just s
- else return Nothing
-
-base_url :: String
-base_url = "http://ajax.googleapis.com/ajax/services/language/translate"
-
-trans_api :: String -> String -> String -> String
-trans_api from to what =
- let make_pair (x, y) = x ++ "=" ++ escape_uri y
- in
- base_url ++ "?" ++ [("v", "1.0"), ("langpair", from ++ "|" ++ to), ("q", what)] .map make_pair .join "&"
-
-data RStatus = RStatus
- {
- responseStatus :: Integer
- }
- deriving (Eq, Show, Data, Typeable)
-
-data RText = RText
- {
- translatedText :: String
- }
- deriving (Eq, Show, Data, Typeable)
-
-data RGood = RGood
- {
- responseData :: RText
- }
- deriving (Eq, Show, Data, Typeable)
-
-data RBad = RBad
- {
- responseDetails :: String
- }
- deriving (Eq, Show, Data, Typeable)
-
-translate :: String -> String -> String -> IO (Maybe String)
-translate from to what = do
- r <- curl - trans_api from to what
- case r of
- Nothing -> return - Nothing
- Just x ->
- let status = x.decodeJSON
- in
- if status.responseStatus == 200
- then do
- let rgood = x.decodeJSON
- return - Just - rgood.responseData.translatedText
- else do
- return Nothing
-
-
--- bolerplate
-
-
--- base DSL
-{-# INLINE (.) #-}
-(.) :: a -> (a -> b) -> b
-a . f = f a
-infixl 9 .
-
-{-# INLINE (-) #-}
-(-) :: (a -> b) -> a -> b
-f - x = f x
-infixr 0 -
-
-join :: [a] -> [[a]] -> [a]
-join = L.intercalate
-
-escape_uri :: String -> String
-escape_uri = escapeURIString isAllowedInURI
+import Text.Language.Translate (translate)
View
14 translate.cabal
@@ -1,9 +1,8 @@
Name: translate
-Version: 2009.12.17
+Version: 2010.1.24
Build-type: Simple
-Synopsis: Haskell binding to Google translate
-Description: translate "en" "fr" "hello"
-
+Synopsis: Haskell binding to Google's AJAX Language API for Translation and Detection
+Description: Haskell binding to Google's AJAX Language API for Translation and Detection.
License: BSD3
@@ -12,13 +11,16 @@ Author: Wang, Jinjing
Maintainer: Wang, Jinjing <nfjinjing@gmail.com>
Build-Depends: base
Cabal-version: >= 1.2
-category: Codec
+category: Text
homepage: http://github.com/nfjinjing/translate
data-files: readme.md, changelog.md
library
ghc-options: -Wall
- build-depends: base >= 4 && < 5, curl, json, network
+ build-depends: base >= 4 && < 5, curl, json, network, utf8-string
hs-source-dirs: src/
exposed-modules:
Text.Translate
+ Text.Language.Translate
+ Text.Language.Detect
+ Text.Language.Internals

0 comments on commit fd7a0e6

Please sign in to comment.