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

标签 php arabic

在阿拉伯语中,像“ا”(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, 0, '', 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, sameLetters, customIndices);
console.log(ans);

关于php - 如何使用可互换字母获取单词的所有可能变体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50210473/

相关文章:

php - 基于 MySQL 数据渲染 HTML 复选框输入

javascript - CKEditor RTL问题

windows - 用户使用阿拉伯/回历日历的 Delphi 问题

html - SVG 文本 getComputedTextLength 对于阿拉伯字体来说太短

unicode - 在 Unicode 中,为什么阿拉伯数字有两种表示形式?

php - 无法使用 PHP 在 MYSQL 数据库中存储阿拉伯语

php - 从Android应用程序上传图像到服务器不工作

php - ES 5.5.1 [size]查询格式错误,查询名称后没有start_object

php - Laravel 5.5 Cookie 不存储

php - htaccess mod_rewrite