Created
December 25, 2009 04:48
Revisions
-
wolever revised this gist
Dec 26, 2009 . 1 changed file with 12 additions and 0 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -44,3 +44,15 @@ at_word(Trie) -> { ok, _ } -> true; error -> false end. % % Example usage % word_in_trie([], Trie) -> at_word(Trie); word_in_trie([Ch|Rest], Trie) -> case trie:step(Ch, Trie) of { ok, NextTrie } -> word_in_trie(Rest, NextTrie); error -> false end. -
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,46 @@ -module(trie). -export([new/0, add/2, step/2, from_dict/0, at_word/1]). setdefault(Key, Default, Dict) -> case dict:find(Key, Dict) of { ok, Value } -> { Dict, Value }; error -> { dict:store(Key, Default, Dict), Default } end. new() -> dict:new(). add([], Trie) -> dict:store(stop, true, Trie); add([Ch|Rest], Trie) -> { NewTrie, SubTrie } = setdefault(Ch, dict:new(), Trie), NewSubTrie = add(Rest, SubTrie), dict:store(Ch, NewSubTrie, NewTrie). step(Ch, Trie) -> dict:find(Ch, Trie). from_dict() -> { ok, File } = file:open("/usr/share/dict/words", [read]), %{ ok, File } = file:open("/tmp/x", [read]), from_dict(new(), File). from_dict(CurTrie, File) -> case io:get_line(File, "") of eof -> file:close(File), CurTrie; UnfixedLine -> Line = string:strip(string:to_lower(UnfixedLine), both, $\n), NewTrie = add(Line, CurTrie), from_dict(NewTrie, File) end. at_word(Trie) -> case dict:find(stop, Trie) of { ok, _ } -> true; error -> false end.