Check if a token is a name

This commit is contained in:
2023-03-20 18:48:55 -06:00
parent 91eb24e6f0
commit c17eed4c06
7 changed files with 85 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
(var :Map<String,Bool> loadedNameFiles (new Map))
(var :Map<String,Bool> loadedNames (new Map))
(var libPath (Prelude.libPath "bad-nlp"))
(var namesPath (joinPath libPath "name-database"))
(function loadFilesForToken [:String token]
(let [firstLetter (token.charAt 0)
firstTwoLetters (token.substr 0 2)
familyNameFile (joinPath namesPath "family_name" firstLetter "${firstTwoLetters}.yml")
givenNameFile (joinPath namesPath "given_name" firstLetter "${firstTwoLetters}.yml")]
(loadNameFile familyNameFile)
(loadNameFile givenNameFile)))
(function loadNameFile [:String file]
(unless (or !(FileSystem.exists file) (loadedNameFiles.exists file))
(let [:Array<Dynamic> data (Yaml.read file)]
(doFor name (data.iterator)
(typeCase [name]
([:String name] (dictSet loadedNames name true))
([:AnyObjectMap map]
(dictSet loadedNames (.next (map.keys)) true))))))
(dictSet loadedNameFiles file true))
(function isName [:String token]
(let [token (token.toLowerCase)]
(loadFilesForToken token)
(loadedNames.exists token)))