-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseInput.hs
executable file
·62 lines (49 loc) · 2.09 KB
/
ParseInput.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
module ParseInput where
import Data.Char
import Datasets
import Input
import Matrices (Matrix (..), shape)
-- Convert input from text file into a list of strings
inputToStrings :: String -> [String]
inputToStrings = lines
-- Remove the table headers (first row of text file)
removeHeaders :: [String] -> [String]
removeHeaders (x : xs) = xs
-- Takes a list of strings, and converts them to a list of Floats
convertToNumerals :: [String] -> [[Float]]
convertToNumerals = map wordsToFloat
--Takes a string of words (seperated by space), converts each word to float
wordsToFloat :: String -> [Float]
wordsToFloat s = map stringToFloat (words s)
--Takes a single word and converts it to a float
stringToFloat :: String -> Float
stringToFloat s = read s :: Float
--Takes 2D list of Floats (rows by columns), and builds the X and y matrices.
-- y is a Matrix comprised of the floats in the last column of the input
-- X is a Matrix comprised of all floats in the input, except for the last column
--Returns both X in y as Matrices in a tuple
splitXandY :: [[Float]] -> (Matrix, Matrix)
splitXandY input = (Matrix reducedX (length reducedX, length $ last reducedX), Matrix builtY (length builtY, length $ last builtY))
where
reducedX = reduceX input
builtY = buildY input
--Takes 2D list of Floats, removes the last column in the 2D list
reduceX :: [[Float]] -> [[Float]]
reduceX = map removeLast
-- Take a list and removes the last element
removeLast :: [Float] -> [Float]
removeLast = init
-- Takes a 2D list of Floats, returns the last column in the 2D list
buildY :: [[Float]] -> [[Float]]
buildY input = [[last ys] | ys <- input]
-- Create the data from our 2-tuple of matrices
createData :: (Matrix, Matrix) -> Dataset
createData (m1, m2) = uncurry (Dataset m1 m2) (shape m1)
createInput :: Dataset -> Float -> Int -> Input
createInput = Input
-- Bringing it all together...
parseInput :: String -> String -> String -> Input
parseInput input lr i = createInput (createData $ splitXandY $ convertToNumerals $ removeHeaders $ inputToStrings input) lrFloat iInt
where
lrFloat = read lr :: Float
iInt = read i :: Int