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}}==
uses levenshtein() from [[Levenshtein_distance#Phix]] and [http://wiki.puzzlers.org/pub/wordlists/unixdict.txt this unixdict.txt]
{{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"
<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 lt(string w, integer n) return levenshtein(w,word)=n end 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,lt,n),1,6)})
{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":