Skip to content

Commit

Permalink
Sped up string decompression
Browse files Browse the repository at this point in the history
  • Loading branch information
wpRobertson committed Oct 6, 2017
1 parent bd64b1a commit fe76075
Show file tree
Hide file tree
Showing 3 changed files with 749,142 additions and 12 deletions.
16 changes: 6 additions & 10 deletions DecompressString.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@ import System.FilePath (replaceFileName)

decompressString :: String -> String
decompressString s = go "" s where
go prev (x:xs) | Just word <- lookup (prev++[x]) dictionary = word ++ go "" xs
go prev (x:xs) | Just word <- Map.lookup (prev++[x]) dictionary = word ++ go "" xs
| otherwise = go (prev++[x]) xs
go _ [] = []


--dictionary :: Map.Map String String
--We need to read it at runtime, otherwise compilation is too slow and memory-hungry
--dictionary = Map.fromDistinctAscList $ map read $ lines listdict where
dictionary :: [(String,String)]
dictionary = map parse $lines listdict where
parse s | (first,tab:second) <- span (/='\t') s = (second,first)
listdict = unsafePerformIO $ getDict
dictionary :: Map.Map String String
dictionary = Map.fromDistinctAscList $ map splitTabs $ lines dict where
splitTabs s | (first,tab:second) <- span (/='\t') s = (first,second)
dict = unsafePerformIO $ getDict
getDict = do
path <- getExecutablePath
readFile $ replaceFileName path "dictionary.tsv"
readFile $ "rdictionary.tsv"
4 changes: 2 additions & 2 deletions compressString.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ main = do args <- getArgs
putStr $ unlines $ map quote results
(_,_,errors) -> hPutStrLn stderr $ unlines errors ++ usageInfo "Usage: compressString [OPT] string" consoleOpts
where
buildDict text = Map.fromList $ map splitTab $ lines text
buildDict text = Map.fromDistinctAscList $ map splitTab $ lines text
splitTab s | (first,tab:second) <- span (/='\t') s = (first,second)
quote s = '¨':s++"¨"

Expand Down Expand Up @@ -55,4 +55,4 @@ compressString opt s dictionary = go [(0, [""])] (map replaceNewlines s) where
chooseBest (a@(_,[]):as) (b:bs) = b:chooseBest as bs
chooseBest (a@(l1,w1):as) (b@(l2,w2):bs) | l1 < l2 = a:chooseBest as bs
| l1 ==l2 && not opt = (l1,w1++w2):chooseBest as bs
| otherwise = b:chooseBest as bs
| otherwise = b:chooseBest as bs
Loading

0 comments on commit fe76075

Please sign in to comment.