sentence-jp (empty) → 0.1.0.0
raw patch · 4 files changed
+142/−0 lines, 4 filesdep +basedep +mecabdep +random-shufflesetup-changed
Dependencies added: base, mecab, random-shuffle, text, transformers
Files
- Control/SentenceJP.hs +83/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- sentence-jp.cabal +27/−0
+ Control/SentenceJP.hs view
@@ -0,0 +1,83 @@+{-# LANGUAGE OverloadedStrings #-}+module Control.SentenceJP+ ( generateSentence+ ) where++import Control.Monad.IO.Class ( MonadIO (), liftIO )+import Data.List ( concat, foldl1 )+import Data.Text ( Text () )+import Prelude hiding ( foldl1 )+import System.Random.Shuffle ( shuffleM )+import Text.MeCab ( new, parseToNodes, Node (..) )+import qualified Data.Text as T+++--- Define data types+--+data PositionText = Begin Text | Middle Text | End Text deriving (Show)++extractPositionText :: PositionText -> Text+extractPositionText (Begin x) = x+extractPositionText (Middle x) = x+extractPositionText (End x) = x++isEnd :: PositionText -> Bool+isEnd (End _) = True+isEnd _ = False++type PositionTextCons = (Text -> PositionText)+getPosition :: PositionText -> PositionTextCons+getPosition (Begin _) = Begin+getPosition (Middle _) = Middle+getPosition (End _) = End+++data ChainableWords = ChainableWords PositionText PositionText deriving (Show)++areChainable :: ChainableWords -> ChainableWords -> Bool+areChainable (ChainableWords _ y1) (ChainableWords x2 _) =+ let jointL = extractPositionText y1+ jointR = extractPositionText x2+ in jointL == jointR++toText :: ChainableWords -> Text+toText (ChainableWords x y) = extractPositionText x `T.append` extractPositionText y++hasBegin :: ChainableWords -> Bool+hasBegin (ChainableWords (Begin _) _) = True+hasBegin _ = False+++--- Define functions+--++-- | Generating Sentence as Text from [Text]+generateSentence :: MonadIO m => [Text] -> m Text+generateSentence sources = do+ mecab <- liftIO . new $ ["mecab"]+ nodes <- liftIO . mapM (parseToNodes mecab) $ sources+ let sentences = map (toChainable . filter (/= "") . map nodeSurface) $ nodes+ sentences' <- liftIO . shuffleM . concat $ sentences+ return . chainWords $ sentences'++toChainable :: [Text] -> [ChainableWords]+toChainable [x] = [ChainableWords (Begin x) (End "")]+toChainable xs = zipWith ChainableWords xs' (tail xs')+ where+ beginWord = Begin . head $ xs+ endWord = End . last $ xs+ middleWords = map Middle . init . tail $ xs+ xs' = [beginWord] ++ middleWords ++ [endWord]++chainWords :: [ChainableWords] -> Text+chainWords [] = error "aho-baka-hoge"+chainWords [x] = toText x+chainWords xs = toText . foldl1 chain . dropWhile (not . hasBegin) $ xs+ where+ chain :: ChainableWords -> ChainableWords -> ChainableWords+ chain a@(ChainableWords _ y1) b@(ChainableWords _ y2)+ | areChainable a b = if isEnd y1+ then a+ else let l = Begin . toText $ a+ in ChainableWords l y2+ | otherwise = a
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright aiya000 (c) 2016++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 aiya000 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.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ sentence-jp.cabal view
@@ -0,0 +1,27 @@+name: sentence-jp+version: 0.1.0.0+synopsis: Easily generating message of japanese natural language+description: Please see README.md+homepage: https://github.com/aiya000/hs-sentence-jp#readme+license: BSD3+license-file: LICENSE+author: aiya000+maintainer: aiya000.develop@gmail.com+copyright: 2016 aiya000+category: Japanese Natural Language Processing+build-type: Simple+-- extra-source-files:+cabal-version: >=1.10++library+ exposed-modules: Control.SentenceJP+ build-depends: base >= 4.7 && < 5+ , mecab+ , random-shuffle+ , text+ , transformers+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/aiya000/hs-sentence-jp