{-# OPTIONS_GHC -threaded #-}
module DobutokO.Sound.Keyboard (
qwerty2dvorak
, dvorak2qwerty
, input2BL
, input2BLN
, input2BLMN
, readFile2BL
, readFile2BLN
, readFile2BLMN
, readFile2BLGen
, readFile2BLGenN
, readFile2BLGenMN
, readFileDoubles
, readFileDoublesN
, readFileDoublesMN
, readFileDoublesGen
, readFileDoublesGenN
, readFileDoublesGenMN
, takeDoubles
, hashStr2
, convH
) where
import CaseBi (getBFst')
import Data.Char (isAsciiLower)
import qualified Data.Vector as V
import GHC.Int (Int64)
import qualified Data.ByteString.Lazy.Char8 as BL (ByteString,map,zipWith,tail,filter,getContents,readFile,take,drop)
takeDoubles :: BL.ByteString -> V.Vector Int
takeDoubles xs = V.fromList . BL.zipWith hashStr2 xs $ BL.tail xs
qwerty2dvorak :: BL.ByteString -> BL.ByteString
qwerty2dvorak = BL.map (getBFst' (' ',V.fromList . zip ("/;<>" ++ ['a'..'z']) $ "wvszaxje.uidchtnmbrl'poygk,qf;"))
dvorak2qwerty :: BL.ByteString -> BL.ByteString
dvorak2qwerty = BL.map (getBFst' (' ',V.fromList . zip ("',.;" ++ ['a'..'z']) $ "qwezanihdyujgcvpmlsrxo;kf.,bt/"))
hashStr2 :: Char -> Char -> Int
hashStr2 x y = getBFst' (57, V.fromList . zip ['a'..'z'] . map (\t -> getBFst' (26 * t + 18, V.fromList . zip ['a'..'z'] $ [(26 * t + 4)..]) y) .
concat $ [[0],[6..8],[1],[9..11],[4],[12..16],[2],[17..21],[3],[22..24],[5,25]]) x
input2BL :: IO (BL.ByteString)
input2BL = fmap (convH [] (BL.filter isAsciiLower)) BL.getContents
input2BLN :: Int64 -> IO (BL.ByteString)
input2BLN n = fmap (convH [] (BL.take n . BL.filter isAsciiLower)) BL.getContents
input2BLMN :: Int64 -> Int64 -> IO (BL.ByteString)
input2BLMN m n = fmap (convH [] (BL.take n . BL.drop m . BL.filter isAsciiLower)) BL.getContents
readFile2BLGen :: String -> FilePath -> IO (BL.ByteString)
readFile2BLGen ys = fmap (convH ys (BL.filter isAsciiLower)) . BL.readFile
readFile2BLGenN :: Int64 -> String -> FilePath -> IO (BL.ByteString)
readFile2BLGenN n ys = fmap (convH ys (BL.take n . BL.filter isAsciiLower)) . BL.readFile
readFile2BLGenMN :: Int64 -> Int64 -> String -> FilePath -> IO (BL.ByteString)
readFile2BLGenMN m n ys = fmap (convH ys (BL.take n . BL.drop m . BL.filter isAsciiLower)) . BL.readFile
convH :: String -> (BL.ByteString -> BL.ByteString) -> (BL.ByteString -> BL.ByteString)
convH ys f
| null ys = f
| ys == "q" = qwerty2dvorak . f
| otherwise = dvorak2qwerty . f
readFile2BL :: FilePath -> IO (BL.ByteString)
readFile2BL = readFile2BLGen []
readFile2BLN :: Int64 -> FilePath -> IO (BL.ByteString)
readFile2BLN n = readFile2BLGenN n []
readFile2BLMN :: Int64 -> Int64 -> FilePath -> IO (BL.ByteString)
readFile2BLMN m n = readFile2BLGenMN m n []
readFileDoublesGen :: String -> FilePath -> IO (V.Vector Int)
readFileDoublesGen ys = fmap (takeDoubles . convH ys (BL.filter isAsciiLower)) . BL.readFile
readFileDoublesGenN :: Int64 -> String -> FilePath -> IO (V.Vector Int)
readFileDoublesGenN n ys = fmap (V.unsafeSlice 0 (fromIntegral n) . takeDoubles . convH ys (BL.filter isAsciiLower)) . BL.readFile
readFileDoublesGenMN :: Int64 -> Int64 -> String -> FilePath -> IO (V.Vector Int)
readFileDoublesGenMN m n ys = fmap (V.unsafeSlice (fromIntegral m) (fromIntegral n) . takeDoubles . convH ys (BL.filter isAsciiLower)) . BL.readFile
readFileDoubles :: FilePath -> IO (V.Vector Int)
readFileDoubles = readFileDoublesGen []
readFileDoublesN :: Int64 -> FilePath -> IO (V.Vector Int)
readFileDoublesN n = readFileDoublesGenN n []
readFileDoublesMN :: Int64 -> Int64 -> FilePath -> IO (V.Vector Int)
readFileDoublesMN m n = readFileDoublesGenMN m n []