module DetectEnglish where import Data.Char import Data.HashSet (HashSet) import qualified Data.HashSet as HashSet import Data.List import System.IO importDictionary :: IO (HashSet String) importDictionary = do wholeFile <- readFile "dictionary.txt" return (HashSet.fromList (words wholeFile)) -- | -- >>> isEnglish (HashSet.fromList ["HI", "THERE"]) 0.5 "hi there English" -- True -- | -- >>> isEnglish (HashSet.fromList ["HI", "THERE"]) 0.2 "hi there English" -- True isEnglish :: HashSet String -> Float -> String -> Bool isEnglish hashDict threshold message = let wordsList = words (map toUpper message) numEnglishWords = genericLength . filter id $ map (`HashSet.member` hashDict) wordsList numTotalWords = genericLength wordsList in threshold < numEnglishWords / numTotalWords