Text completion: Difference between revisions
Content added Content deleted
m (C++ - reject empty word) |
m (→{{header|Phix}}: use apply()/calc once) |
||
Line 479: | Line 479: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
⚫ | |||
{{trans|Julia}} |
|||
⚫ | |||
<lang Phix>string word = "complition" |
<lang Phix>string word = "complition" |
||
sequence words = get_text(join_path({"demo","unixdict.txt"}),GT_LF_STRIPPED) |
sequence words = get_text(join_path({"demo","unixdict.txt"}),GT_LF_STRIPPED), |
||
leven = apply(words,levenshtein,word) |
|||
function |
function ln(string /*w*/, integer i, n) return leven[i]=n end function |
||
for n=1 to 4 do |
for n=1 to 4 do |
||
printf(1,"Words at Levenshtein distance of %d (%g%% similarity) from \"%s\": \n%s\n", |
printf(1,"Words at Levenshtein distance of %d (%g%% similarity) from \"%s\": \n%s\n", |
||
{n,100-round(100*n/length(word)),word,join_by(filter(words, |
{n,100-round(100*n/length(word)),word,join_by(filter(words,ln,n),1,6)}) |
||
end for</lang> |
end for</lang> |
||
Note the parameters of the filter routine lt() can be quite flexible, for instance you could instead do this (and get the same results) |
|||
<lang Phix>function lt(string w, sequence nw) |
|||
{integer n, string word} = nw |
|||
... |
|||
{n,100-round(100*n/length(word)),word,join_by(filter(words,lt,{n,word}),1,6)})</lang> |
|||
{{out}} |
{{out}} |
||
(matches Delphi/Go/Wren) |
|||
<pre> |
<pre> |
||
Words at Levenshtein distance of 1 (90% similarity) from "complition": |
Words at Levenshtein distance of 1 (90% similarity) from "complition": |