<commit_before>module Score where

import Data.Char
import Data.List

-- TODO: test this properly
-- TODO: matchLength searches for first char anywhere in string,
-- it should return 0 if first char isn't correct, otherwise find endMatch
matchLength :: String -> String -> Int
matchLength [] _  =  1
matchLength _ []  =  0
matchLength (firstChar:rest) str = 
    case elemIndex firstChar str of
        Just startPos -> let endPos = endMatch rest (drop (startPos + 1) str) 
                                                    (startPos + 1)
                         in if endPos == 0 
                            then 0
                            else endPos - startPos
        Nothing -> 0
  where 
    endMatch [] _ lastIndex = lastIndex 
    endMatch (q:qs) s lastIndex = case elemIndex q s of
        Just i -> endMatch qs (drop (i + 1) s) (i + 1 + lastIndex)
        Nothing -> 0


minMatchLength :: String -> String -> Int
minMatchLength [] _ = 1
minMatchLength _ [] = 0
minMatchLength query@(q:_) choice = 
  let lengths = filter (>0) 
                $ map (matchLength query)
                $ filter (isPrefixOf [q])
                $ tails choice<commit_msg>Drop ~10ms by not using isPrefixOf<commit_after>module Score where

import Data.Char
import Data.List

-- IDEA: look for position of last character first to limit
-- the potential search area of end match or quickly eliminate the str
-- -- Do this with all chars?

-- TODO: test this properly
-- TODO: matchLength searches for first char anywhere in string,
-- it should return 0 if first char isn't correct, otherwise find endMatch
matchLength :: String -> String -> Int
matchLength [] _  =  1
matchLength _ []  =  0
matchLength (firstChar:rest) str = 
    case elemIndex firstChar str of
        Just startPos -> let endPos = endMatch rest (drop (startPos + 1) str) 
                                                    (startPos + 1)
                         in if endPos == 0 
                            then 0
                            else endPos - startPos
        Nothing -> 0
  where 
    endMatch [] _ lastIndex = lastIndex 
    endMatch (q:qs) s lastIndex = case elemIndex q s of
        Just i -> endMatch qs (drop (i + 1) s) (i + 1 + lastIndex)
        Nothing -> 0

{-hasMatch :: String -> String -> Bool-}
{-hasMatch query choice = True-}


minMatchLength :: String -> String -> Int
minMatchLength [] _ = 1
minMatchLength _ [] = 0
minMatchLength query@(q:_) choice = 
  let lengths = filter (>0) 
                $ map (matchLength query)
                {-$ filter (hasMatch query)-}
                $ filter ((== q) . head) 
                $ filter (not . null)
                $ tails choice
{'fraction_non_alphanumeric': 0.08799403430275914, 'fraction_numerical': 0.011185682326621925, 'mean_word_length': 1.7977059436913452, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2167422', 'n_tokens_mistral': 756, 'n_tokens_neox': 694, 'n_words': 285}