Permalink
Browse files

init

  • Loading branch information...
0 parents commit d83168868e7520029f1ee51bbe05cff845183751 @nfjinjing committed Dec 17, 2009
Showing with 240 additions and 0 deletions.
  1. +23 −0 .gitignore
  2. +31 −0 LICENSE
  3. +24 −0 Nemesis
  4. +4 −0 Setup.lhs
  5. 0 changelog.md
  6. 0 known-issues.md
  7. +8 −0 readme.md
  8. +34 −0 src/Codec/SnowWhite.hs
  9. 0 src/Main.hs
  10. +92 −0 src/Text/Translate.hs
  11. +24 −0 translate.cabal
@@ -0,0 +1,23 @@
+*.swp
+*.swo
+*.o
+*.hi
+.searchpath.default.map
+*.pid
+*.fcgi
+
+*.exe
+
+*.log
+
+/public
+/db
+
+/_darcs
+/dist
+
+manifest
+hello
+Main
+
+.nemesis
31 LICENSE
@@ -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.
24 Nemesis
@@ -0,0 +1,24 @@
+nemesis = do
+
+ clean
+ [ "**/*.hi"
+ , "**/*.o"
+ , "manifest"
+ , "main"
+ , "nemesis-tmp.*"
+ ]
+
+
+ desc "prepare cabal dist"
+ task "dist" $ do
+ sh "cabal clean"
+ sh "cabal configure"
+ sh "cabal sdist"
+
+ desc "start console"
+ task "i" (sh "ghci -isrc src/Text/Translate.hs")
+
+ desc "show sloc"
+ task "stat" $ do
+ sh "cloc -match-f=hs$ --quiet src --no3"
+
@@ -0,0 +1,4 @@
+#! /usr/bin/env runhaskell
+
+> import Distribution.Simple
+> main = defaultMain
No changes.
No changes.
@@ -0,0 +1,8 @@
+Translate
+==========
+
+Haskell binding to google translate
+
+ ghci
+
+ > :m
@@ -0,0 +1,34 @@
+module Codec.SnowWhite (pack, unpack) where
+
+import Data.Binary
+import Numeric
+import MPS.Env hiding (encode, decode)
+import Prelude ()
+import qualified Data.ByteString.Lazy.Char8 as B
+import Data.Char
+
+c2w, w2c :: Char -> Char
+c2w '0' = ' '
+c2w '1' = '\t'
+c2w _ = error "not 0 or 1"
+
+w2c ' ' = '0'
+w2c '\t' = '1'
+w2c _ = error "not 0 or 1"
+
+
+b2s, pack :: (Binary a) => a -> String
+b2s = encode > B.unpack > map (ord > (base 2) > rjust 8 '0' > map c2w) > concat
+
+base :: Int -> Int -> String
+base p n = showIntAtBase p intToDigit n ""
+
+from_base :: Int -> String -> Int
+from_base p = readInt p (const True) digitToInt > first > fst
+
+s2b, unpack :: (Binary a) => String -> a
+s2b = select (belongs_to " \t") > in_group_of 8 > map (map w2c > from_base 2 > chr) > B.pack > decode
+
+
+pack = b2s
+unpack = s2b
No changes.
@@ -0,0 +1,92 @@
+{-# LANGUAGE DeriveDataTypeable #-}
+
+module Text.Translate (translate) where
+
+import Curl
+import Hack.Contrib.Utils
+import Text.JSON.Generic
+import qualified TranslateStatus as T
+import Network.Curl
+import qualified Data.List as L
+import Prelude hiding ((.), (-))
+
+-- 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.is 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.is 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
@@ -0,0 +1,24 @@
+Name: translate
+Version: 2009.12.1
+Build-type: Simple
+Synopsis: askell binding to google translate
+Description:
+
+
+
+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/snow-white
+data-files: readme.md, changelog.md
+
+library
+ ghc-options: -Wall
+ build-depends: base >= 4 && < 5, curl, json
+ hs-source-dirs: src/
+ exposed-modules:
+ Text.Translate

0 comments on commit d831688

Please sign in to comment.