Created
April 16, 2015 23:02
-
-
Save shoooe/2fb6a538e1c9893ecd7a to your computer and use it in GitHub Desktop.
N-grams!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
groupEvery :: Int -> [a] -> [[a]] | |
groupEvery n lst | |
| length lst < n = [] | |
| otherwise = take n lst : groupEvery n (tail lst) | |
ngrams :: Int -> String -> [String] | |
ngrams n = concatMap (groupEvery n . fillWord '_') . words | |
where fillWord f w = f : (w ++ replicate (n - 1) f) | |
main :: IO () | |
main = print $ ngrams 3 "This is simple and a example" | |
-- ["_Th","Thi","his","is_","s__","_is","is_","s__","_si","sim","imp" | |
-- ,"mpl","ple","le_","e__","_an","and","nd_","d__","_a_","a__","_ex" | |
-- ,"exa","xam","amp","mpl","ple","le_","e__"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment