{"id":17549181,"url":"https://github.com/yhirose/cpp-fstlib","last_synced_at":"2025-04-24T02:08:44.234Z","repository":{"id":45703907,"uuid":"44276834","full_name":"yhirose/cpp-fstlib","owner":"yhirose","description":"A single file C++17 header-only Minimal Acyclic Subsequential Transducers, or Finite State Transducers","archived":false,"fork":false,"pushed_at":"2022-10-26T12:33:02.000Z","size":7656,"stargazers_count":55,"open_issues_count":0,"forks_count":9,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-24T02:08:36.748Z","etag":null,"topics":["cpp","cpp17","finite-state-transducers","fst","header-only"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yhirose.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-14T21:14:23.000Z","updated_at":"2025-03-10T09:58:30.000Z","dependencies_parsed_at":"2023-01-20T16:31:57.482Z","dependency_job_id":null,"html_url":"https://github.com/yhirose/cpp-fstlib","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yhirose%2Fcpp-fstlib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yhirose%2Fcpp-fstlib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yhirose%2Fcpp-fstlib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yhirose%2Fcpp-fstlib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yhirose","download_url":"https://codeload.github.com/yhirose/cpp-fstlib/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250546081,"owners_count":21448260,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","cpp17","finite-state-transducers","fst","header-only"],"created_at":"2024-10-21T02:50:07.369Z","updated_at":"2025-04-24T02:08:44.204Z","avatar_url":"https://github.com/yhirose.png","language":"C++","readme":"# cpp-fstlib\n\n[![](https://github.com/yhirose/cpp-fstlib/workflows/CMake/badge.svg)](https://github.com/yhirose/cpp-fstlib/actions)\n\nC++17 header-only FST (finite state transducer) library.\nWe can use it as [Trie data structure](https://en.wikipedia.org/wiki/Trie).\nThis library uses the algorithm \"[Minimal Acyclic Subsequential Transducers](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.24.3698\u0026rep=rep1\u0026type=pdf)\".\n\n## Play cpp-fstlib with cli\n\n```bash\n\u003e git clone http://github/yhirose/cpp-fstlib\n\u003e cd cpp-fstlib\n\u003e make build \u0026\u0026 cd build\n\u003e cmake .. \u0026\u0026 make\n\u003e ./cmd/fst compile /usr/share/dict/words words.fst\n\n\u003e ./cmd/fst search words.fst hello\n83713\n\n\u003e ./cmd/fst prefix words.fst helloworld\nh: 81421\nhe: 82951\nhell: 83657\nhello: 83713\n\n\u003e ./cmd/fst longest words.fst helloworld\nhello: 83713\n\n\u003e ./cmd/fst predictive words.fst predictiv\npredictive: 153474\npredictively: 153475\npredictiveness: 153476\n\n\u003e ./cmd/fst fuzzy words.fst fuzzy -ed 2 // Edit distance 2\nSuzy: 195759\nbuzz: 28064\nbuzzy: 28076\n...\n\n\u003e ./cmd/fst spellcheck words.fst thier\ntheir: 0.946667\nthir: 0.762667\ntier: 0.752\nthief: 0.736\ntrier: 0.704\n```\n\n## API reference\n\n```cpp\nnamespace fst {\n\nenum class Result { Success, EmptyKey, UnsortedKey, DuplicateKey };\n\nstd::pair\u003cResult, size_t /* error input index */\u003e compile\u003cuint32_t\u003e(\n  const std::vector\u003cstd::pair\u003cstd::string, uint32_t\u003e\u003e \u0026input,\n  std::ostream \u0026os,\n  bool sorted\n);\n\nstd::pair\u003cResult, size_t /* error input index */\u003e compile\u003cstd::string\u003e(\n  const std::vector\u003cstd::pair\u003cstd::string, std::string\u003e\u003e \u0026input,\n  std::ostream \u0026os\n);\n\nstd::pair\u003cResult, size_t /* error input index */\u003e compile(\n  const std::vector\u003cstd::string\u003e \u0026key_only_input,\n  std::ostream \u0026os,\n  bool need_output, // true: map, false: set\n  bool sorted\n);\n\ntemplate \u003ctypename output_t\u003e class map {\npublic:\n  map(const char *byte_code, size_t byte_code_size);\n\n  operator bool() const;\n\n  bool contains(std::string_view sv) const;\n\n  output_t operator[](std::string_view sv) const;\n\n  output_t at(std::string_view sv) const;\n\n  bool exact_match_search(std::string_view sv, output_t \u0026output) const;\n\n  std::vector\u003cstd::pair\u003csize_t length, output_t output\u003e\u003e\n  common_prefix_search(std::string_view sv) const;\n\n  size_t longest_common_prefix_search(std::string_view sv, output_t \u0026output) const;\n\n  std::vector\u003cstd::pair\u003cstd::string, output_t\u003e\u003e\n  predictive_search(std::string_view sv) const;\n\n  std::vector\u003cstd::pair\u003cstd::string, output_t\u003e\u003e\n  edit_distance_search(std::string_view sv, size_t max_edits) const;\n\n  std::vector\u003cstd::tuple\u003cdouble, std::string, output_t\u003e\u003e\n  suggest(std::string_view word) const;\n}\n\nclass set {\npublic:\n  set(const char *byte_code, size_t byte_code_size);\n\n  operator bool() const;\n\n  bool contains(std::string_view sv) const;\n\n  std::vector\u003csize_t\u003e common_prefix_search(std::string_view sv) const;\n\n  size_t longest_common_prefix_search(std::string_view sv) const;\n\n  std::vector\u003cstd::string\u003e predictive_search(std::string_view sv) const;\n\n  std::vector\u003cstd::string\u003e\n  edit_distance_search(std::string_view sv, size_t max_edits) const;\n\n  std::vector\u003cstd::pair\u003cdouble, std::string\u003e\u003e\n  suggest(std::string_view word) const;\n}\n\n} // namespace fst\n```\n\n## API usage\n\n```cpp\nconst std::vector\u003cstd::pair\u003cstd::string, std::string\u003e\u003e items = {\n  {\"hello\", \"こんにちは!\"},\n  {\"world\", \"世界!\"},\n  {\"hello world\", \"こんにちは世界!\"}, // incorrect sort order entry...\n};\n\nstd::stringstream out;\nauto sorted = false; // ask fst::compile to sort entries\nauto [result, error_line] = fst::compile\u003cstd::string\u003e(items, out, sorted);\n\nif (result == fst::Result::Success) {\n  const auto\u0026 byte_code = out.str();\n  fst::map\u003cstd::string\u003e matcher(byte_code.data(), byte_code.size());\n\n  if (matcher) {\n    assert(matcher.contains(\"hello world\"));\n    assert(!matcher.contains(\"Hello World\"));\n    assert(matcher[\"hello\"] == \"こんにちは!\");\n\n    auto prefixes = matcher.common_prefix_search(\"hello world!\");\n    assert(prefixes.size() == 2);\n    assert(prefixes[0].first == 5);\n    assert(prefixes[0].second == \"こんにちは!\");\n    assert(prefixes[1].first == 11);\n    assert(prefixes[1].second == \"こんにちは世界!\");\n\n    std::string output;\n    auto length = matcher.longest_common_prefix_search(\"hello world!\", output);\n    assert(length == 11);\n    assert(output == \"こんにちは世界!\");\n\n    auto predictives = matcher.predictive_search(\"he\");\n    assert(predictives.size() == 2);\n    assert(predictives[0].first == \"hello\");\n    assert(predictives[0].second == \"こんにちは!\");\n    assert(predictives[1].first == \"hello world\");\n    assert(predictives[1].second == \"こんにちは世界!\");\n\n    std::cout \u003c\u003c \"[Edit distance 1]\" \u003c\u003c std::endl;\n    for (auto [k, o]: matcher.edit_distance_search(\"hellow\", 1)) {\n      std::cout \u003c\u003c \"key: \" \u003c\u003c k \u003c\u003c \" output: \" \u003c\u003c o \u003c\u003c std::endl;\n    }\n\n    std::cout \u003c\u003c \"[Suggestions]\" \u003c\u003c std::endl;\n    for (auto [r, k, o]: matcher.suggest(\"hellow\")) {\n      std::cout \u003c\u003c \"ratio: \" \u003c\u003c r \u003c\u003c \" key: \" \u003c\u003c k \u003c\u003c \" output: \" \u003c\u003c o \u003c\u003c std::endl;\n    }\n  }\n}\n```\n\n```\n[Edit distance 1]\nkey: hello output: こんにちは\n[Suggestions]\nratio: 0.810185 key: hello output: こんにちは\nratio: 0.504132 key: hello world output: こんにちは世界!\nratio: 0.0962963 key: world output: 世界!\n```\n\nLicense\n-------\n\nMIT license (© 2022 Yuji Hirose)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyhirose%2Fcpp-fstlib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyhirose%2Fcpp-fstlib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyhirose%2Fcpp-fstlib/lists"}