【发布时间】:2019-05-14 16:33:37
【问题描述】:
我尝试使用 Haskell 的 Conduit 库实现一个简单的字数统计:
wordcountCv2 :: IO ()
wordcountCv2 = do
hashMap <- runConduitRes $ sourceFile "input.txt"
.| decodeUtf8C
.| omapCE Data.Char.toLower
.| peekForeverE (do
word <- takeWhileCE isAlphaNum
dropCE 1
return word)
.| foldMC insertInHashMap empty
print (toList hashMap)
insertInHashMap x v = do
return (insertWith (+) v 1 x)
问题是这个函数可以很好地处理中小型输入文件,但是随着文件大小的增长,它往往会破坏一些单词。例如,如果我使用一个包含 100 次单词“hello”的小文件,结果是:[("hello",100)],而不是如果 hellos 是例如 100000,结果是:[("hello", 99988),("他",6),("地狱",6),("o",6),("llo",6)]。文件增长得越多,断词就越多。我的实现有问题吗?
【问题讨论】:
-
请一次一个问题。
-
好吧对不起,我删第一个
-
次要观点:
word <- takeWhileCE isAlphaNum使word成为()。取出的单词被推送到下游,而不是返回。无论如何,这应该不是问题。