packages feed

translate (empty) → 2009.12.17

raw patch · 6 files changed

+167/−0 lines, 6 filesdep +basedep +curldep +jsonsetup-changed

Dependencies added: base, curl, json, network

Files

+ LICENSE view
@@ -0,0 +1,31 @@+Copyright (c) 2009, Jinjing Wang++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are+met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Jinjing Wang nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.lhs view
@@ -0,0 +1,4 @@+#! /usr/bin/env runhaskell++> import Distribution.Simple+> main = defaultMain
+ changelog.md view
+ readme.md view
@@ -0,0 +1,15 @@+Translate+==========++Haskell binding to Google translate++    ghci+    +    > :m Text.Translate+    > translate "en" "fr" "hello"+    +    Just "bonjour"+    +++    
+ src/Text/Translate.hs view
@@ -0,0 +1,93 @@+{-# LANGUAGE DeriveDataTypeable #-}++module Text.Translate (translate) where++import Text.JSON.Generic+import Network.Curl+import qualified Data.List as L+import Prelude hiding ((.), (-))+import Network.URI++-- 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
+ translate.cabal view
@@ -0,0 +1,24 @@+Name:                 translate+Version:              2009.12.17+Build-type:           Simple+Synopsis:             Haskell binding to Google translate+Description:          translate "en" "fr" "hello"++                      ++License:              BSD3+License-file:         LICENSE+Author:               Wang, Jinjing+Maintainer:           Wang, Jinjing <nfjinjing@gmail.com>+Build-Depends:        base+Cabal-version:        >= 1.2+category:             Codec+homepage:             http://github.com/nfjinjing/translate+data-files:           readme.md, changelog.md++library+  ghc-options: -Wall+  build-depends: base >= 4 && < 5, curl, json, network+  hs-source-dirs: src/+  exposed-modules:  +                    Text.Translate