Text completion: Difference between revisions

Content added Content deleted
m (tidy up some Wikipedia links)
m (syntax highlighting fixup automation)
Line 31: Line 31:
=={{header|C++}}==
=={{header|C++}}==
{{trans|Julia}}
{{trans|Julia}}
<lang cpp>#include <algorithm>
<syntaxhighlight lang="cpp">#include <algorithm>
#include <fstream>
#include <fstream>
#include <iostream>
#include <iostream>
Line 99: Line 99:
}
}
return EXIT_SUCCESS;
return EXIT_SUCCESS;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 125: Line 125:
{{Trans|Wren}}
{{Trans|Wren}}
Using '''unixdict.txt'''.
Using '''unixdict.txt'''.
<syntaxhighlight lang="delphi">
<lang Delphi>
program Text_Completion;
program Text_Completion;


Line 236: Line 236:
end.
end.


</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 255: Line 255:
{{trans|Julia}}
{{trans|Julia}}
{{works with|Factor|0.99 2020-07-03}}
{{works with|Factor|0.99 2020-07-03}}
<lang factor>USING: formatting fry http.client io kernel lcs literals math
<syntaxhighlight lang="factor">USING: formatting fry http.client io kernel lcs literals math
math.ranges namespaces prettyprint.config sequences splitting ;
math.ranges namespaces prettyprint.config sequences splitting ;


Line 273: Line 273:
dup [ similarity ] [ drop word ] [ word swap lev-dist-of ] tri
dup [ similarity ] [ drop word ] [ word swap lev-dist-of ] tri
"Words at Levenshtein distance of %d (%.1f%% similarity) from %u:\n%u\n\n" printf
"Words at Levenshtein distance of %d (%.1f%% similarity) from %u:\n%u\n\n" printf
] each</lang>
] each</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 292: Line 292:
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
This uses '[http://wiki.puzzlers.org/pub/wordlists/unixdict.txt unixdict]' and the [https://www.rosettacode.org/wiki/Levenshtein_distance#FreeBASIC Levenshtein distance] algorithm to test for similarity.
This uses '[http://wiki.puzzlers.org/pub/wordlists/unixdict.txt unixdict]' and the [https://www.rosettacode.org/wiki/Levenshtein_distance#FreeBASIC Levenshtein distance] algorithm to test for similarity.
<lang freebasic>#define MIN(a, b) iif((a) < (b), (a), (b))
<syntaxhighlight lang="freebasic">#define MIN(a, b) iif((a) < (b), (a), (b))


Dim As String palabra = "complition"
Dim As String palabra = "complition"
Line 323: Line 323:
Print
Print
Next n
Next n
Sleep</lang>
Sleep</syntaxhighlight>
{{out}}
{{out}}
<pre>Input word: complition
<pre>Input word: complition
Line 339: Line 339:
=={{header|Go}}==
=={{header|Go}}==
{{trans|Wren}}
{{trans|Wren}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 402: Line 402:
fmt.Println()
fmt.Println()
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 420: Line 420:
=={{header|Java}}==
=={{header|Java}}==
[https://github.com/dwyl/english-words Github Repo Uses dependencies given].
[https://github.com/dwyl/english-words Github Repo Uses dependencies given].
<syntaxhighlight lang="java">
<lang Java>
import java.io.File;
import java.io.File;
import java.io.IOException;
import java.io.IOException;
Line 483: Line 483:
}
}
}
}
</syntaxhighlight>
</lang>
;Output
;Output
<pre>
<pre>
Line 512: Line 512:


The "debug" statements for showing the number of words under consideration for best Levenshtein matches is retained.
The "debug" statements for showing the number of words under consideration for best Levenshtein matches is retained.
<syntaxhighlight lang="jq">
<lang jq>
include "levenshtein-distance" {search: "."}; # https://rosettacode.org/wiki/Levenshtein_distance#jq
include "levenshtein-distance" {search: "."}; # https://rosettacode.org/wiki/Levenshtein_distance#jq


Line 546: Line 546:


task
task
</syntaxhighlight>
</lang>
{{out}}
{{out}}
Invocation: jq -nR -f text-completion.jq unixdict.txt
Invocation: jq -nR -f text-completion.jq unixdict.txt
Line 614: Line 614:
=={{header|Julia}}==
=={{header|Julia}}==
See https://en.wikipedia.org/wiki/Levenshtein_distance, the number of one character edits to obtain one word from another.
See https://en.wikipedia.org/wiki/Levenshtein_distance, the number of one character edits to obtain one word from another.
<lang julia>using StringDistances
<syntaxhighlight lang="julia">using StringDistances


const fname = download("https://www.mit.edu/~ecprice/wordlist.10000", "wordlist10000.txt")
const fname = download("https://www.mit.edu/~ecprice/wordlist.10000", "wordlist10000.txt")
Line 627: Line 627:
levdistof(n, wrd), "\n")
levdistof(n, wrd), "\n")
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Words at Levenshtein distance of 1 (90% similarity) from "complition":
Words at Levenshtein distance of 1 (90% similarity) from "complition":
Line 643: Line 643:


=={{header|Mathematica}}==
=={{header|Mathematica}}==
<lang Mathematica>Module[
<syntaxhighlight lang="mathematica">Module[
{word = "complition"},
{word = "complition"},
Map[
Map[
Line 649: Line 649:
SpellingCorrectionList[word]
SpellingCorrectionList[word]
]
]
] // Grid</lang>
] // Grid</syntaxhighlight>
{{out}}
{{out}}
<pre>completion 90%
<pre>completion 90%
Line 663: Line 663:
We use the function <code>editDistance</code> from the <code>std/editdistance</code> module to get the Levenshtein distance (computed by considering in Unicode codepoints).
We use the function <code>editDistance</code> from the <code>std/editdistance</code> module to get the Levenshtein distance (computed by considering in Unicode codepoints).


<lang Nim>import std/editdistance, sequtils, strformat, strutils
<syntaxhighlight lang="nim">import std/editdistance, sequtils, strformat, strutils


let search = "complition"
let search = "complition"
Line 679: Line 679:
echo &"Words which are {similarity:4.1f}% similar:"
echo &"Words which are {similarity:4.1f}% similar:"
echo lev[i].join(" ")
echo lev[i].join(" ")
echo()</lang>
echo()</syntaxhighlight>


{{out}}
{{out}}
Line 695: Line 695:
=={{header|Perl}}==
=={{header|Perl}}==
Inspired by Raku Sorenson-Dice implementation (doesn't handle Unicode, but module <code>Text::Dice</code> can).
Inspired by Raku Sorenson-Dice implementation (doesn't handle Unicode, but module <code>Text::Dice</code> can).
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature 'say';
use feature 'say';
Line 733: Line 733:
push @ranked, sprintf "%.3f $_", $scored{$_} for sort { $scored{$b} <=> $scored{$a} } keys %scored;
push @ranked, sprintf "%.3f $_", $scored{$_} for sort { $scored{$b} <=> $scored{$a} } keys %scored;
say "\n$word:\n" . join("\n", head 10, @ranked);
say "\n$word:\n" . join("\n", head 10, @ranked);
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>complition:
<pre>complition:
Line 757: Line 757:
=={{header|Phix}}==
=={{header|Phix}}==
uses levenshtein() from [[Levenshtein_distance#Phix]] (reproduced below for your convenience) and the standard unix_dict().
uses levenshtein() from [[Levenshtein_distance#Phix]] (reproduced below for your convenience) and the standard unix_dict().
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">levenshtein</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">levenshtein</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">),</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">),</span>
Line 785: Line 785:
<span style="color: #0000FF;">{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">-</span><span style="color: #7060A8;">round</span><span style="color: #0000FF;">(</span><span style="color: #000000;">100</span><span style="color: #0000FF;">*</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">)),</span><span style="color: #000000;">word</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ln</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)})</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">-</span><span style="color: #7060A8;">round</span><span style="color: #0000FF;">(</span><span style="color: #000000;">100</span><span style="color: #0000FF;">*</span><span style="color: #000000;">n</span><span style="color: #0000FF;">/</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">)),</span><span style="color: #000000;">word</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #000000;">words</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ln</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
(matches Delphi/Go/Wren)
(matches Delphi/Go/Wren)
Line 816: Line 816:
===Hamming distance===
===Hamming distance===
{{Trans|Java}}
{{Trans|Java}}
<lang perl6>sub MAIN ( Str $user_word = 'complition', Str $filename = 'words.txt' ) {
<syntaxhighlight lang="raku" line>sub MAIN ( Str $user_word = 'complition', Str $filename = 'words.txt' ) {
my @s1 = $user_word.comb;
my @s1 = $user_word.comb;
my @listed = gather for $filename.IO.lines -> $line {
my @listed = gather for $filename.IO.lines -> $line {
Line 834: Line 834:


say "{.[0].fmt('%.2f')}% {.[1]}" for @listed;
say "{.[0].fmt('%.2f')}% {.[1]}" for @listed;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>80.00% compaction
<pre>80.00% compaction
Line 847: Line 847:
Using unixdict.txt from www.puzzlers.org
Using unixdict.txt from www.puzzlers.org


<lang perl6>sub sorenson ($phrase, %hash) {
<syntaxhighlight lang="raku" line>sub sorenson ($phrase, %hash) {
my $match = bigram $phrase;
my $match = bigram $phrase;
%hash.race.map: { [(2 * ($match ∩ .value) / ($match + .value)).round(.001), .key] }
%hash.race.map: { [(2 * ($match ∩ .value) / ($match + .value)).round(.001), .key] }
Line 861: Line 861:
say "\n$w:";
say "\n$w:";
.say for sorenson($w, %hash).grep(*.[0] >= .55).sort({-.[0],~.[1]}).head(10);
.say for sorenson($w, %hash).grep(*.[0] >= .55).sort({-.[0],~.[1]}).head(10);
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 919: Line 919:


No attempt was made to change (by any method) three letters or more.
No attempt was made to change (by any method) three letters or more.
<lang rexx>/*REXX pgm finds (dictionary) words which can be found in a specified word wheel (grid).*/
<syntaxhighlight lang="rexx">/*REXX pgm finds (dictionary) words which can be found in a specified word wheel (grid).*/
parse arg what iFID . /*obtain optional arguments from the CL*/
parse arg what iFID . /*obtain optional arguments from the CL*/
if what==''|what=="," then what= 'complition' /*Not specified? Then use the default.*/
if what==''|what=="," then what= 'complition' /*Not specified? Then use the default.*/
Line 982: Line 982:
if @.z\==. then $= $ z; if r==1 then call recur
if @.z\==. then $= $ z; if r==1 then call recur
end /*k*/
end /*k*/
end /*j*/; return space($)</lang>
end /*j*/; return space($)</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 1,053: Line 1,053:
=={{header|Vlang}}==
=={{header|Vlang}}==
{{trans|Go}}
{{trans|Go}}
<lang vlang>import os
<syntaxhighlight lang="vlang">import os


fn levenshtein(s string, t string) int {
fn levenshtein(s string, t string) int {
Line 1,102: Line 1,102:
println('')
println('')
}
}
}}</lang>
}}</syntaxhighlight>


{{out}}
{{out}}
Line 1,121: Line 1,121:
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
This uses 'unixdict' and the Levenshtein distance algorithm to test for similarity.
This uses 'unixdict' and the Levenshtein distance algorithm to test for similarity.
<lang ecmascript>import "io" for File
<syntaxhighlight lang="ecmascript">import "io" for File
import "/fmt" for Fmt
import "/fmt" for Fmt


Line 1,165: Line 1,165:
System.print(lev[i])
System.print(lev[i])
System.print()
System.print()
} </lang>
} </syntaxhighlight>


{{out}}
{{out}}