Anagram generator: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 34: Line 34:
Implementation:
Implementation:


<lang J>anagen=: {{
<syntaxhighlight lang=J>anagen=: {{
seed=. (tolower y)([-.-.)a.{~97+i.26
seed=. (tolower y)([-.-.)a.{~97+i.26
letters=. ~.seed
letters=. ~.seed
Line 55: Line 55:
end.
end.
EMPTY
EMPTY
}}</lang>
}}</syntaxhighlight>


Conceptually, we're working with a graph here -- given a seed word, we have sequence of letters and a count of each of those letters which we must supply. Given a list of words whose letters do not exceed that count, we extend that list with an additional word, discarding word sequences which go over the letter count. We would repeat as necessary until we satisfy the needed letter counts. Also, we inspect the "final result" and remove our seed word or phrase from it (if it was present -- and if that would have been our only result we would extend our under budget word sequences until they satisfy our letter counts). It seems like two words is usually sufficient here.
Conceptually, we're working with a graph here -- given a seed word, we have sequence of letters and a count of each of those letters which we must supply. Given a list of words whose letters do not exceed that count, we extend that list with an additional word, discarding word sequences which go over the letter count. We would repeat as necessary until we satisfy the needed letter counts. Also, we inspect the "final result" and remove our seed word or phrase from it (if it was present -- and if that would have been our only result we would extend our under budget word sequences until they satisfy our letter counts). It seems like two words is usually sufficient here.
Line 65: Line 65:
Examples:
Examples:


<lang J> 'unixdict.txt' anagen 'Rosettacode'
<syntaxhighlight lang=J> 'unixdict.txt' anagen 'Rosettacode'
cetera stood
cetera stood
coat oersted
coat oersted
Line 135: Line 135:
piazza trim type
piazza trim type
piety pizza tram
piety pizza tram
pizza pyrite tam</lang>
pizza pyrite tam</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
<lang ruby>const unixwords = split(read("unixdict.txt", String) |> lowercase, r"\s+")
<syntaxhighlight lang=ruby>const unixwords = split(read("unixdict.txt", String) |> lowercase, r"\s+")


function findphrases(anastring::AbstractString, choices, sizelong = 4, n_shortpermitted = 1)
function findphrases(anastring::AbstractString, choices, sizelong = 4, n_shortpermitted = 1)
Line 175: Line 175:
foreach(println, findphrases(s, unixwords, 4, 0) |> unique |> sort!)
foreach(println, findphrases(s, unixwords, 4, 0) |> unique |> sort!)
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
From 'Rosetta code':
From 'Rosetta code':
Line 210: Line 210:
=={{header|Phix}}==
=={{header|Phix}}==
Couldn't really think of a better way than just building a dirty great filter list to get rid of the less interesting answers....
Couldn't really think of a better way than just building a dirty great filter list to get rid of the less interesting answers....
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang=Phix>(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">bo_ring</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"al"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"alex"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"am"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"an"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"and"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"anent"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ann"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ant"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ar"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ares"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"art"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"at"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ax"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"axle"</span><span style="color: #0000FF;">,</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">bo_ring</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"al"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"alex"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"am"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"an"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"and"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"anent"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ann"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ant"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ar"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ares"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"art"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"at"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ax"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"axle"</span><span style="color: #0000FF;">,</span>
Line 266: Line 266:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"Rosetta"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"PureFox"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"PeteLomax"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Wherrera"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Thundergnat"</span><span style="color: #0000FF;">},</span><span style="color: #000000;">test</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"Rosetta"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"PureFox"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"PeteLomax"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Wherrera"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Thundergnat"</span><span style="color: #0000FF;">},</span><span style="color: #000000;">test</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 318: Line 318:
Using the unixdict.txt word file by default.
Using the unixdict.txt word file by default.


<lang perl6>unit sub MAIN ($in is copy = '', :$dict = 'unixdict.txt');
<syntaxhighlight lang=perl6>unit sub MAIN ($in is copy = '', :$dict = 'unixdict.txt');


say 'Enter a word or phrase to be anagramed. (Loading dictionary)' unless $in.chars;
say 'Enter a word or phrase to be anagramed. (Loading dictionary)' unless $in.chars;
Line 357: Line 357:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out|Truncated to only show the best few as subjectively determined by me}}
{{out|Truncated to only show the best few as subjectively determined by me}}
''Punctuation, capitalization and (in some cases) word order manually massaged.''
''Punctuation, capitalization and (in some cases) word order manually massaged.''
Line 382: Line 382:


Alternatives formed by simply changing the order of the two words have been suppressed.
Alternatives formed by simply changing the order of the two words have been suppressed.
<lang ecmascript>import "io" for File
<syntaxhighlight lang=ecmascript>import "io" for File
import "./str" for Str, Char
import "./str" for Str, Char
import "./perm" for Comb
import "./perm" for Comb
Line 445: Line 445:
System.print("\n%(tests[i]):")
System.print("\n%(tests[i]):")
anagramGenerator.call(tests[i])
anagramGenerator.call(tests[i])
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}