In “search” mode, if no anagram is found, return nothing; other minor changes

This commit is contained in:
pezcurrel 2024-12-31 07:41:47 +01:00
parent 4fd09e314b
commit cf617d54af

15
anagram
View file

@ -2,7 +2,7 @@
<?php
$SCRIPTNAME='anagram';
$SCRIPTVERSION='0.1.1';
$SCRIPTVERSION='0.1.2';
$SCRIPTURL='https://git.lattuga.net/Jones/anagram';
$conf=[
@ -87,16 +87,17 @@ if ($conf['action']==='genadict' && is_null($conf['ofp'])) dieyoung("Error: you
if ($conf['action']==='search' && is_null($conf['inpword'])) dieyoung("Error: you have not specified a word to anagram.\n",1);
if ($conf['action']==='genadict' || $conf['action']==='search') {
$dict=@file($conf['ifp'],FILE_SKIP_EMPTY_LINES|FILE_IGNORE_NEW_LINES);
if ($dict===false) dieyoung("Error: could not read «{$conf['ifp']}».\n",1);
foreach ($dict as $key=>$word)
$dict[$key]=mb_strtolower($word,'UTF-8');
$buff=@file($conf['ifp'],FILE_SKIP_EMPTY_LINES|FILE_IGNORE_NEW_LINES);
if ($buff===false) dieyoung("Error: could not read «{$conf['ifp']}».\n",1);
foreach ($buff as $key=>$word)
$buff[$key]=mb_strtolower($word,'UTF-8');
$anagrams=[];
foreach ($dict as $key=>$word) {
foreach ($buff as $key=>$word) {
$sword=sortword($word);
if (!array_key_exists($sword,$anagrams) || !in_array($word,$anagrams[$sword]))
$anagrams[$sword][]=$word;
}
unset($buff);
}
if ($conf['action']==='genadict') {
@ -119,8 +120,6 @@ if ($conf['action']==='genadict') {
sort($buff);
if (count($buff)>1)
echo implode(' ',$buff)."\n";
} else {
echo "Found no anagrams for «{$conf['inpword']}».\n";
}
} elseif ($conf['action']==='combine') {
$conf['inpword']=mb_str_split($conf['inpword']);