Manioc

I/O

Oct 25th, 2018
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.List
  2. import Data.Typeable
  3. import Data.Char
  4. import System.Directory
  5. import Types
  6.  
  7. type Image = [Float]
  8. type Sample = (Int, Image)
  9.  
  10. strToArr::String->[Float]
  11. strToArr line = [(read x::Float)| x<-(words line)]
  12.  
  13. strToMatrix::String->[[Float]]
  14. strToMatrix mt = [(strToArr x)| x<-(lines mt)]
  15.  
  16. arrToStr::[Float]->String
  17. arrToStr [] = ""
  18. arrToStr (h:t) = (show h::String) ++ " " ++ (arrToStr t)
  19.  
  20. matrixToStr::[[Float]]->String
  21. matrixToStr [] = ""
  22. matrixToStr (h:t) = (arrToStr h) ++ "\n" ++ (matrixToStr t)
  23.  
  24. listRamdom::Int->IO [Float]
  25. listRamdom 0 = return []
  26. listRamdom sz = do
  27.    
  28.  
  29. readIn::IO Data
  30. readIn = do
  31.     wH <- readFile "Weight_hidden.txt"
  32.     bH <- readFile "Biases_hidden.txt"
  33.  
  34.     wO <- readFile "Weight_Output.txt"
  35.     bO <- readFile "Biases_Output.txt"
  36.  
  37.     return $ Data (strToMatrix wH) (strToArr bH) (strToMatrix wO) (strToArr bO)
  38.  
  39. writeIn::Data->IO()
  40. writeIn elem = do
  41.     writeFile "Weight_hidden.txt" (matrixToStr (wHidden elem))
  42.     writeFile "Biases_hidden.txt" (arrToStr (bHidden elem))
  43.  
  44.     writeFile "Weight_Output.txt" (matrixToStr (wOutput elem))
  45.     writeFile "Biases_Output.txt" (arrToStr (bOutput elem))
  46.  
  47.  
  48. isEmptyFile::String->IO Bool
  49. isEmptyFile path = do
  50.     elem <- readFile path
  51.  
  52.     if (length elem) == 0 then return True
  53.     else return False
  54.  
  55. pickNumber::FilePath->Int
  56. pickNumber "" = -1
  57. pickNumber (h:t)
  58.                 | h == '-' = digitToInt $ t!!0
  59.                 | otherwise = (pickNumber t)
  60.  
  61. makeImage::FilePath->IO Image
  62. makeImage path = do
  63.     elem <- readFile path
  64.     let ret = (strToArr elem)
  65.     return ret
  66.  
  67. makeSample::FilePath->String->IO Sample
  68. makeSample archive path = do
  69.     let number = (pickNumber archive)
  70.     mt <- (makeImage $ path++archive)
  71.     return (number, mt)
  72.  
  73. listSample::[FilePath]->String->IO [Sample]
  74. listSample [] path = return []
  75. listSample (h:t) path = do
  76.                 head <- makeSample h path
  77.                 tail <- listSample t path
  78.                 return $ head:tail
  79.  
  80. getTest::IO [Sample]
  81. getTest = do
  82.     let caminho = "C:/Users/Amandio/Documents/Programacao/Hecome_Bumans/Tests" -- change
  83.     elems <- (getDirectoryContents caminho) -- Colocar diretorio
  84.     ret <- listSample (filter (/= ".") $ filter (/= "..") elems) caminho
  85.     return ret
  86.  
  87. getTrain::IO [Sample]
  88. getTrain = do
  89.     let caminho = "C:/Users/Amandio/Documents/Programacao/Hecome_Bumans/Trains" -- change
  90.     elems <- (getDirectoryContents caminho) -- Colocar diretorio
  91.     ret <- listSample (filter (/= ".") $ filter (/= "..") elems) caminho
  92.     return ret
  93.  
  94. main::IO()
  95. main = do
  96.     d <- readIn
  97.     print $ typeOf d
  98.     print d
Advertisement
Add Comment
Please, Sign In to add comment