Text completion: Difference between revisions

m
→‎{{header|Phix}}: use apply()/calc once
m (C++ - reject empty word)
m (→‎{{header|Phix}}: use apply()/calc once)
Line 479:
 
=={{header|Phix}}==
uses levenshtein() from [[Levenshtein_distance#Phix]] and [http://wiki.puzzlers.org/pub/wordlists/unixdict.txt this unixdict.txt] (not the same as the one Julia uses - I did just check and indeed it does ''not'' have "collection" in it!)
{{trans|Julia}}
uses levenshtein() from [[Levenshtein_distance#Phix]] and [http://wiki.puzzlers.org/pub/wordlists/unixdict.txt unixdict] (not the same as the one Julia uses - I did just check and indeed it does ''not'' have "collection" in it!)
<lang Phix>string word = "complition"
sequence words = get_text(join_path({"demo","unixdict.txt"}),GT_LF_STRIPPED),
leven = apply(words,levenshtein,word)
function ltln(string /*w*/, integer i, n) return levenshtein(w,word)leven[i]=n end function
for n=1 to 4 do
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,ltln,n),1,6)})
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}}
(matches Delphi/Go/Wren)
<pre>
Words at Levenshtein distance of 1 (90% similarity) from "complition":
7,796

edits