<commit_before>module Backend.Recipe
    (
      Ingredient(..)
    , RecipeInfo
    , decomposeRecipe
    , handleLines
    ) where
---------------------------------------
--Imports

import Control.Applicative ((<$>), (<*>))
import Data.List (isPrefixOf)
import Data.Char (toLower)

---------------------------------------
--Data & Types

data Ingredient = Ingredient { amount :: Float
                             , unit :: String
                             , item :: String
                             } deriving (Eq)
instance Show Ingredient where
  show (Ingredient a u i) = show a ++ " " ++ u ++ " " ++ i

type ConversionFactor = (Float, Float)
type RecipeName = String
type RecipeInfo = (RecipeName, ConversionFactor, [Ingredient])
---------------------------------------
--Functions

decomposeRecipe :: String -> [String]
decomposeRecipe = filter ((&&) <$> (not . null) <*> (not . isPrefixOf "--")) . lines

handleLines :: [String] -> RecipeInfo
handleLines (n:c:i) = (n, conversion c, ingredients i)
  where ingredients = map (buildIngredient' (snd . conversion $ c) . words) 
        conversion  = calcConversion 

calcConversion :: String -> ConversionFactor
calcConversion x = (wanted x, wanted x / given x    )
    where given  = read . head . words
          wanted = read . last . words

buildIngredient :: [String] -> Ingredient
buildIngredient (a:u:i) = Ingredient (read a) u (unwords i)

buildIngredient' :: Float -> [String] -> Ingredient
buildIngredient' c (v:u:i) = convertUnits $ Ingredient (read v * c) u (unwords i)

convertUnits :: Ingredient -> Ingredient
convertUnits ingredient@(Ingredient a u i) 
    | a < 1     = case map toLower u of
                  "kg" -> Ingredient (a * 1000) "g" i
                  "l"  -> Ingredient (a * 1000) "mL" i
    | a > 1000  = case map toLower u of
                  "g"  -> Ingredient (a / 1000) "kg" i
                  "ml" -> Ingredient (a / 1000) "L" i<commit_msg>Fixed non-exhaustive case in unit conversions.<commit_after>module Backend.Recipe
    (
      Ingredient(..)
    , RecipeInfo
    , decomposeRecipe
    , handleLines
    ) where
---------------------------------------
--Imports

import Control.Applicative ((<$>), (<*>))
import Data.List (isPrefixOf)
import Data.Char (toLower)

---------------------------------------
--Data & Types

data Ingredient = Ingredient { amount :: Float
                             , unit :: String
                             , item :: String
                             } deriving (Eq)
instance Show Ingredient where
  show (Ingredient a u i) = show a ++ " " ++ u ++ " " ++ i

type ConversionFactor = (Float, Float)
type RecipeName = String
type RecipeInfo = (RecipeName, ConversionFactor, [Ingredient])
---------------------------------------
--Functions

decomposeRecipe :: String -> [String]
decomposeRecipe = filter ((&&) <$> (not . null) <*> (not . isPrefixOf "--")) . lines

handleLines :: [String] -> RecipeInfo
handleLines (n:c:i) = (n, conversion c, ingredients i)
  where ingredients = map (buildIngredient' (snd . conversion $ c) . words) 
        conversion  = calcConversion 

calcConversion :: String -> ConversionFactor
calcConversion x = (wanted x, wanted x / given x    )
    where given  = read . head . words
          wanted = read . last . words

buildIngredient :: [String] -> Ingredient
buildIngredient (a:u:i) = Ingredient (read a) u (unwords i)

buildIngredient' :: Float -> [String] -> Ingredient
buildIngredient' c (v:u:i) = convertUnits $ Ingredient (read v * c) u (unwords i)

convertUnits :: Ingredient -> Ingredient
convertUnits ingredient@(Ingredient a u i) 
    | a < 1     = case map toLower u of
                  "kg" -> Ingredient (a * 1000) "g" i
                  "l"  -> Ingredient (a * 1000) "mL" i
                  otherwise -> ingredient
    | a > 1000  = case map toLower u of
                  "g"  -> Ingredient (a / 1000) "kg" i
                  "ml" -> Ingredient (a / 1000) "L" i
                  otherwise -> ingredient 
{'fraction_non_alphanumeric': 0.17776664997496244, 'fraction_numerical': 0.010515773660490736, 'mean_word_length': 1.9139314369073668, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3465410', 'n_tokens_mistral': 1268, 'n_tokens_neox': 1200, 'n_words': 362}