turn into a function
This commit is contained in:
parent
c5b13fe4ca
commit
e4c99b168f
1 changed files with 26 additions and 22 deletions
48
haskell.hs
48
haskell.hs
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue