php – 如何使用可互换字母获取单词的所有可能变体?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 如何使用可互换字母获取单词的所有可能变体?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
在阿拉伯语中,像“ا”(Alef)这样的字母有很多形式/变体:

(ا,أ,Å,آ)

也是字母ي的情况相同,也可能是ى.

我想要做的是获得一个单词的所有可能的变化与许多أ和ي字母.

例如,“أين”这个词应该包含所有这些(大多数情况下都是不正确的)变体:أين,إين,اين,آين,أىن,اىن,آىن……等等.

为什么?我正在构建一个小的文本更正系统,可以处理语法错误并用正确的单词替换错误的单词.

我一直试图以最干净的方式做到一点,但我最终得到一个8 for / foreach循环只是为了处理“أ”这个词

必须有一个更好的更干净的方式来做到这一点!有什么想法吗?

这是我的代码到目前为止:

$alefVARiations = ['ا','إ','أ','آ'];
        $word = 'أيامنا';

        // break into letters
        $wordLetters = preg_split('//u',$word,null,PREG_SPLIT_NO_EMPTY);
        $worDalefLettersIndexes = [];

        // Get the أ letters
        for($letterIndex = 0; $letterIndex < count($wordLetters); $letterIndex++){
            if(in_array($wordLetters[$letterIndex],$alefVariations)){
                $worDalefLettersIndexes[] = $letterIndex;
            }
        }

        $eachLetterVariations = [];
        foreach($worDalefLettersIndexes as $alefLettersIndex){
            foreach($alefVariations as $alefVariation){
                $wordCopy = $wordLetters;
                $wordCopy[$alefLettersIndex] = $alefVariation;

                $eachLetterVariations[$alefLettersIndex][] = $wordCopy;
            }
        }

        $variations = [];
        foreach($worDalefLettersIndexes as $alefLettersIndex){
            $alefWordVariations = $eachLetterVariations[$alefLettersIndex];

            foreach($worDalefLettersIndexes as $alefLettersIndex_inner){
                if($alefLettersIndex == $alefLettersIndex_inner) continue;

                foreach($alefWordVariations as $alefWordVariation){
                    foreach($alefVariations as $alefVariation){
                        $alefWordVariationCopy = $alefWordVariation;
                        $alefWordVariationCopy[$alefLettersIndex_inner] = $alefVariation;

                        $variations[] = $alefWordVariationCopy;
                    }
                }
            }
        }

        $finalList = [];
        foreach($variations as $variation){
            $finalList[] = implode('',$variation);
        }

        return array_unique($finalList);
我不认为这是自动更正的方法,但这里是您提出的问题的通用解决方案.它使用递归,它是在JavaScript(我不知道PHP).
function solve(word,sameLetters,customIndices = []){
    var splitLetters = word.split('')
                .map((char,index) => { // check if the current letter is within any variation
                    if(customIndices.length == 0 || customIndices.includes(index)){
                        var variations = sameLetters.find(arr => arr.includes(char));
                        if(variations != undefined) return variations;
                    }
                    return [char];
                 });

    // up to this point splitLetters will be like this
    //  [["ا","إ","أ","آ"],["ي","ى","ي"],["ا"],["م"],["ن"],["ا"]]
    var res = [];
    recurse(splitLetters,'',res); // this function will generate all the PErmuations
    return res;
}

function recurse(letters,index,cur,res){
    if(index == letters.length){
        res.push(cur);
    } else {
        for(var letter of letters[index]) {
            recurse(letters,index + 1,cur + letter,res );
        }
    }
}

var sameLetters = [     // represents the variations that you want to enumerate
    ['ا','آ'],['ي','ى','ي']
];

var word = 'أيامنا';    
var customIndices = [0,1]; // will make variations to the letters in these indices only. leave it empty for all indices

var ans = solve(word,customIndices);
console.LOG(ans);

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如何使用可互换字母获取单词的所有可能变体?全部内容,希望文章能够帮你解决php – 如何使用可互换字母获取单词的所有可能变体?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。