-- add singleton clusters to clustering files
-- read files building map over words
-- add the difference

-- usage: add_single reference clustering1 [clustering2]..

import System.Environment (getArgs)
import System.IO
import Control.Monad
import Data.Set

main :: IO ()
main = do
     (ref:fs) <- getArgs
     r <- readFile ref
     mapM_ (append_s (mkindex r)) fs

append_s :: Set String -> FilePath -> IO ()
append_s ref f = do
     fc <- readFile f
     let fi = mkindex fc
     when (not . Data.Set.null . difference fi $ ref)
          (putStrLn ("Warning: '"++f++"' contains unknown words!"))
     let ss = difference ref fi
     when (Data.Set.null ss) (return ())
     h <- openFile (f++"_s") WriteMode
     mapM_ (hPutStrLn h) (toList ss)
     hClose h

mkindex :: String -> Set String
mkindex = foldr insert empty . words
