text-utils (empty) → 0.1.0.0
raw patch · 11 files changed
+227/−0 lines, 11 filesdep +HTFdep +basedep +textsetup-changed
Dependencies added: HTF, base, text
Files
- LICENSE +7/−0
- README.md +5/−0
- Setup.hs +2/−0
- package.yaml +34/−0
- src/Data/Text/KGrams.hs +20/−0
- src/Data/Text/ToFromText.hs +29/−0
- stack.yaml +5/−0
- test/Data/Text/KGramsTest.hs +34/−0
- test/Data/Text/ToFromTextTest.hs +28/−0
- test/Test.hs +10/−0
- text-utils.cabal +53/−0
+ LICENSE view
@@ -0,0 +1,7 @@+Copyright (c) 2016 Alexander Thiemann++Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ README.md view
@@ -0,0 +1,5 @@+# text-utils++[](https://circleci.com/gh/agrafix/text-utils)++Simple text utils
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ package.yaml view
@@ -0,0 +1,34 @@+name: text-utils+version: 0.1.0.0+synopsis: Various text utilities+description: Please see README.md+homepage: https://github.com/agrafix/text-utils#readme+license: MIT+author: Alexander Thiemann+maintainer: mail@athiemann.net+copyright: 2016 Alexander Thiemann <mail@athiemann.net>+category: Web+extra-source-files:+ - README.md+ - package.yaml+ - stack.yaml++dependencies:+ - base >= 4.7 && < 5+ - text++library:+ source-dirs: src+ exposed-modules:+ - Data.Text.KGrams+ - Data.Text.ToFromText++tests:+ spec:+ cpp-options: -DTest+ main: Test.hs+ source-dirs:+ - test+ - src+ dependencies:+ - HTF
+ src/Data/Text/KGrams.hs view
@@ -0,0 +1,20 @@+module Data.Text.KGrams where++import qualified Data.Text as T++textRemovePunc :: T.Text -> T.Text+textRemovePunc = T.map (\ch -> if ch `elem` ".?!-;\'\"" then ' ' else ch)++textNormalizedWords :: T.Text -> [T.Text]+textNormalizedWords = filter (not . T.null) . T.words . T.toLower . textRemovePunc++textKGrams :: Int -> T.Text -> [T.Text]+textKGrams size = concatMap (kgrams size) . textNormalizedWords++kgrams :: Int -> T.Text -> [T.Text]+kgrams size inp =+ let loop txt =+ if T.length txt < size+ then []+ else T.take size txt : loop (T.drop 1 txt)+ in loop inp
+ src/Data/Text/ToFromText.hs view
@@ -0,0 +1,29 @@+{-# LANGUAGE DefaultSignatures #-}+{-# LANGUAGE TypeSynonymInstances #-}+{-# LANGUAGE FlexibleInstances #-}+module Data.Text.ToFromText where++import qualified Data.Text as T++class ToFromText a where+ toText :: a -> T.Text+ fromText :: Monad m => T.Text -> m a++ default fromText :: (Enum a, Bounded a, Monad m) => T.Text -> m a+ fromText = fromTextHelper toText++instance ToFromText T.Text where+ toText = id+ fromText = pure++instance ToFromText String where+ toText = T.pack+ fromText = pure . T.unpack++fromTextHelper :: (Enum a, Bounded a, Monad m) => (a -> T.Text) -> T.Text -> m a+fromTextHelper mkT q =+ loop [minBound .. maxBound]+ where+ loop [] = fail ("Could not read " ++ show q)+ loop (x : xs) =+ if mkT x == q then pure x else loop xs
+ stack.yaml view
@@ -0,0 +1,5 @@+resolver: lts-7.9+packages:+ - .+extra-deps:+ - aeson-1.0.2.1
+ test/Data/Text/KGramsTest.hs view
@@ -0,0 +1,34 @@+{-# LANGUAGE OverloadedStrings #-}+{-# OPTIONS_GHC -F -pgmF htfpp #-}+module Data.Text.KGramsTest where++import Data.Text.KGrams++import Test.Framework++test_removePunc :: IO ()+test_removePunc =+ do assertEqual "hallo" (textRemovePunc "hallo")+ assertEqual "hallo welt" (textRemovePunc "hallo-welt")+ assertEqual "hallo welt " (textRemovePunc "hallo.welt!")++test_normalizedWords :: IO ()+test_normalizedWords =+ do assertEqual ["hallo"] (textNormalizedWords "hallo")+ assertEqual ["hallo", "welt"] (textNormalizedWords "!hallo WeLt ")+ assertEqual ["foo", "bar"] (textNormalizedWords "foo-bar")+ assertEqual ["foo", "bar"] (textNormalizedWords "Foo- bar")+ assertEqual ["foo", "bar"] (textNormalizedWords "Foo.- bar")++test_kgrams :: IO ()+test_kgrams =+ do assertEqual ["foo"] (kgrams 3 "foo")+ assertEqual ["fo", "oo"] (kgrams 2 "foo")+ assertEqual ["f", "o", "o"] (kgrams 1 "foo")+ assertEqual [] (kgrams 3 "fo")++test_textKGrams :: IO ()+test_textKGrams =+ do assertEqual ["foo"] (textKGrams 3 "foo")+ assertEqual ["hal", "all", "llo", "wel", "elt"] (textKGrams 3 "Hallo-Welt")+ assertEqual ["thi", "hie", "iem", "ema", "man", "ann"] (textKGrams 3 "Thiemann")
+ test/Data/Text/ToFromTextTest.hs view
@@ -0,0 +1,28 @@+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE ScopedTypeVariables #-}+{-# OPTIONS_GHC -F -pgmF htfpp #-}+module Data.Text.ToFromTextTest where++import Data.Text.ToFromText++import Test.Framework++data SomeEnum+ = SomeEnumA+ | SomeEnumB+ | SomeEnumC+ deriving (Show, Eq, Enum, Bounded)++instance ToFromText SomeEnum where+ toText e =+ case e of+ SomeEnumA -> "A"+ SomeEnumB -> "B"+ SomeEnumC -> "C"++test_autoFromText :: IO ()+test_autoFromText =+ do assertEqual (Just SomeEnumA) (fromText "A")+ assertEqual (Just SomeEnumB) (fromText "B")+ assertEqual (Just SomeEnumC) (fromText "C")+ assertEqual (Nothing :: Maybe SomeEnum) (fromText "asdasdasd")
+ test/Test.hs view
@@ -0,0 +1,10 @@+{-# OPTIONS_GHC -F -pgmF htfpp #-}+module Main where++import Test.Framework+import {-@ HTF_TESTS @-} Data.Text.KGramsTest+import {-@ HTF_TESTS @-} Data.Text.ToFromTextTest++main :: IO ()+main =+ htfMain htf_importedTests
+ text-utils.cabal view
@@ -0,0 +1,53 @@+-- This file has been generated from package.yaml by hpack version 0.14.0.+--+-- see: https://github.com/sol/hpack++name: text-utils+version: 0.1.0.0+synopsis: Various text utilities+description: Please see README.md+category: Web+homepage: https://github.com/agrafix/text-utils#readme+author: Alexander Thiemann+maintainer: mail@athiemann.net+copyright: 2016 Alexander Thiemann <mail@athiemann.net>+license: MIT+license-file: LICENSE+build-type: Simple+cabal-version: >= 1.10++extra-source-files:+ package.yaml+ README.md+ stack.yaml++library+ hs-source-dirs:+ src+ build-depends:+ base >= 4.7 && < 5+ , text+ exposed-modules:+ Data.Text.KGrams+ Data.Text.ToFromText+ other-modules:+ Paths_text_utils+ default-language: Haskell2010++test-suite spec+ type: exitcode-stdio-1.0+ main-is: Test.hs+ hs-source-dirs:+ test+ , src+ cpp-options: -DTest+ build-depends:+ base >= 4.7 && < 5+ , text+ , HTF+ other-modules:+ Data.Text.KGramsTest+ Data.Text.ToFromTextTest+ Data.Text.KGrams+ Data.Text.ToFromText+ default-language: Haskell2010