-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAnalyzedText.hs
40 lines (35 loc) · 1.4 KB
/
AnalyzedText.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
--- AnalyzedText.hs
module AnalyzedText (AnalyzedText, analyzeText, lCaseWords, wordList, wordSet, text, sentences, paragraphs) where
import Data.List.Split (splitWhen)
import Data.Char (isAlphaNum, toLower)
import Data.Set (Set)
import qualified Data.Set as Set
data AnalyzedText = Analyzed {
lCaseWords :: [String],
wordList :: [String],
wordSet :: Set String,
text :: String,
sentences :: [String],
paragraphs :: [String]
}
analyzeText :: String -> AnalyzedText
analyzeText text = Analyzed {
lCaseWords = lCaseWords,
wordList = wordList,
wordSet = wordSet,
text = text,
sentences = sentences,
paragraphs = paragraphs
}
where
lCaseWords = (lines $
filter (isValidChar) $
map toLower text) >>= words
wordList = (lines $
filter (isValidChar) $ text) >>= words
wordSet = Set.fromList wordList
isSep x = x `elem` ".?!"
paragraphs = lines text
sentences = paragraphs >>= splitWhen (isSep)
isValidChar :: Char -> Bool
isValidChar c = isAlphaNum c || c `elem` "\n "