Skip to content

Instantly share code, notes, and snippets.

@kalekseev
Created December 13, 2011 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kalekseev/1473393 to your computer and use it in GitHub Desktop.
Save kalekseev/1473393 to your computer and use it in GitHub Desktop.
%Stanford www.ai-class.com. Optional NLP Programming
%For test use:
%ai1:decode("Esp qtcde nzyqpcpynp zy esp ezatn zq Lcetqtntlw Tyepwwtrpynp hld spwo le Olcexzfes Nzwwprp ty estd jplc.").
%corpus test 1: http://simple.wikipedia.org/wiki/Wikipedia:List_of_1000_basic_words
%corpus test 2: http://www.wordfrequency.info/files/entriesWithoutCollocates.txt
-module(ai1).
-export([decode/1]).
decode(List) ->
{Min, L} = decode(25, List),
io:format("~nMinimal cost: ~w String: ~p~n", [Min, L]).
decode(0, _) -> {9999999999, "ERROR: set bigger initial cost"};
decode(C, List) ->
{Minlen, MinList} = decode(C-1, List),
L = lap(C, List),
Llen = length(corpus(L)),
io:fwrite("Delta: ~2w\tChars: ~w\tDecode: ~p~n", [C, Llen, L]),
if
Llen < Minlen ->
{Llen, L};
true ->
{Minlen, MinList}
end.
lap(D, [H|T]) -> lists:append([delta(H,D)], lap(D,T));
lap(_, []) -> [].
delta(S, true) -> S - 26;
delta(S, false) -> S;
delta(A, D) ->
if
A >= $a, A =< $z ->
delta(A + D, (A + D) > $z);
A >= $A, A =< $Z ->
delta(A + D, (A + D) > $Z);
true -> A
end.
corpus(TList) ->
{ok, File} = file:read_file("/tmp/corpus2"),
Crps = lists:append([binary_to_list(File),"\n",string:to_lower(TList)]),
Bin = zlib:gzip(Crps),
binary_to_list(Bin).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment