Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
99 additions
and
55 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,59 @@ | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE RecordWildCards #-} | ||
module Main where | ||
|
||
import Control.Concurrent.Async (mapConcurrently) | ||
import Control.Monad (foldM) | ||
import Data.Foldable (foldl') | ||
import Data.Text (Text) | ||
import qualified Data.Text as Text (lines, null, pack, toLower, words) | ||
import qualified Data.Text.IO as Text (putStr) | ||
import qualified Data.Text as Text | ||
import qualified Data.Text.IO as Text | ||
import System.FilePath.Glob (glob) | ||
import System.Environment (getArgs) | ||
import WordFreq | ||
import Options.Applicative | ||
|
||
main :: IO () | ||
main = do | ||
args <- getArgs | ||
case args of | ||
[glob] -> run glob | ||
_ -> run "wiki_*" | ||
data Options = Options { | ||
knownWordListFiles :: [FilePath] | ||
, knownOutput :: Maybe FilePath | ||
, unknownOutput :: Maybe FilePath | ||
, subsequenceOutput :: Maybe FilePath | ||
, wikiExtractorFiles :: [FilePath] | ||
} deriving (Eq) | ||
|
||
programOptions :: Parser Options | ||
programOptions = | ||
Options <$> many (strOption $ long "known-words" <> short 'w' <> metavar "FILE") | ||
<*> optional (strOption $ long "known" <> short 'k' <> metavar "KNOWN-OUTPUT-FILE") | ||
<*> optional (strOption $ long "unknown" <> short 'u' <> metavar "UNKNOWN-OUTPUT-FILE") | ||
<*> optional (strOption $ long "subseq" <> short 's' <> metavar "OUTPUT-FILE") | ||
<*> some (argument str (metavar "FILES...")) | ||
|
||
run match = do | ||
words <- loadWordList "/usr/share/dict/ngerman" | ||
map <- normalize . foldl' mappend mempty <$> (mapConcurrently loadWikiDump =<< glob match) | ||
let known = filterWordFreq (knownWord words) (const True) map | ||
let unknown = filterWordFreq (not . knownWord words) (> 1) map | ||
Text.putStr $ foldWordFreq showWord (commonSequences known) | ||
wikiwc :: Options -> IO () | ||
wikiwc Options{..} = do | ||
words <- foldl' mappend mempty <$> mapConcurrently loadWordList knownWordListFiles | ||
wc <- normalize <$> foldM loadWikiDump mempty wikiExtractorFiles | ||
let known = filterWordFreq (knownWord words) (const True) wc | ||
let unknown = filterWordFreq (not . knownWord words) (const True) wc | ||
case knownOutput of | ||
Just fp -> Text.writeFile fp $ toText . byFrequency $ known | ||
Nothing -> pure () | ||
case unknownOutput of | ||
Just fp -> Text.writeFile fp $ toText . byFrequency $ unknown | ||
Nothing -> pure () | ||
case subsequenceOutput of | ||
Just fp -> Text.writeFile fp $ toText . byFrequency $ commonSequences known | ||
Nothing -> pure () | ||
pure () | ||
|
||
main :: IO () | ||
main = wikiwc =<< execParser opts where | ||
opts = info (programOptions <**> helper) $ | ||
fullDesc | ||
<> progDesc "Count words and substrings from Wikipedia dumps." | ||
<> header "wikiwc - Wikipedia word counter" | ||
|
||
showWord :: Text -> Int -> Text | ||
showWord s i = Text.pack (show i) <> " " <> s <> "\n" | ||
toText :: [(Int, Text)] -> Text | ||
toText = foldMap f where | ||
f (a, w) = Text.pack (show a) <> " " <> w <> "\n" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,8 @@ dependencies: | |
- containers | ||
- extra | ||
- Glob | ||
- optparse-applicative | ||
- parallel | ||
- text | ||
|
||
library: | ||
|
This file contains 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