turn into a function

This commit is contained in:
千住柱間 2024-12-22 21:25:07 -04:00
commit e4c99b168f
Signed by: hashirama
GPG key ID: 53E62470A86BC185

View file

@ -38,40 +38,44 @@ getResults ptr =
rest <- getResults (ptr `plusPtr` sizeOf (undefined :: Ptr CChar))
return (current : rest)
-- Main program
main :: IO ()
main = do
-- Check if dictionary file exists
let dictPath = "/home/hashirama/.local/share/hakurei/dict.bin"
lookupPhrase :: FilePath -> String -> IO [String]
lookupPhrase dictPath searchPhrase = do
fileExists <- doesFileExist dictPath
if not fileExists
then putStrLn "Dictionary file not found!"
then pure []
else do
putStrLn "Calling load_trie..."
dictFile <- newCString dictPath
trieHandle <- loadTrie dictFile
if trieHandle == nullPtr
then putStrLn "Failed to load trie"
then pure []
else do
putStrLn "Trie loaded successfully!"
-- Perform search
putStrLn "Calling perform_search..."
searchStr <- newCString "電気通信業務取扱規程"
searchStr <- newCString searchPhrase
resultCount <- malloc :: IO (Ptr CSize)
resultsPtr <- performSearch trieHandle searchStr resultCount
if resultsPtr == nullPtr
then putStrLn "perform_search returned a null pointer."
results <- if resultsPtr == nullPtr
then pure []
else do
count <- peek resultCount
putStrLn $ "Result count: " ++ show count
results <- getResults resultsPtr
putStrLn "Search results as list (UTF-8):"
mapM_ (putStrLn . T.unpack) results
r <- getResults resultsPtr
freeSearchResults resultsPtr
pure r
free resultCount
freeTrie trieHandle
putStrLn "Program completed successfully."
pure $ map T.unpack results
-- Main program
main :: IO ()
main = do
-- define dictionary once
let lookup = lookupPhrase "/home/hashirama/.local/share/hakurei/dict.bin"
-- some other code here , GUI stuff
-- when you need to lookup something
results <- lookup "よくない"
mapM_ putStrLn results