如何实现我的算法文本更正来替换文本中的单词?

问题描述 投票:3回答:1

简要

帮我创建一个新函数或更改函数correct(),以便结果以case-insensitive方式为输入文本工作。


Usage

correct()方法的示例用法:

$text = "Точик ТОЧИК точик ТоЧиК тоЧИК";

$text = correct($text, $base_words);
echo "$text";

Expected Result

输入:Точик ТОЧИК точик ТоЧиК тоЧИК 输出:Тоҷик ТОҶИК тоҷик ТоҶиК тоҶИК


以下是下面的所有数组和函数,您可以轻松复制它们:

$default_words = array
(
    'бур',
    'кори',
    'давлати',
    'забони',
    'фанни'
);

$base_words = array
(
    "точик"    => "тоҷик",
    "точики"   => "тоҷики",
    "точикон"  => "тоҷикон",
    "чахонгир" => "ҷаҳонгир",
    "галат"    => "ғалат",
    "уктам"    => "ӯктам",
);

$base_special_words = array
(
    "кори хатти"     => "кори хаттӣ",
    "хатти аз"       => "хаттӣ аз",
    "забони точики"  => "забони тоҷикӣ",
    "точики барои"   => "тоҷикӣ барои",
    "забони давлати" => "забони давлатӣ",
    "давлати дар"    => "давлатӣ дар",
    "микёси чахони"  => "миқёси ҷаҳонӣ",
);


function correct($request, $dictionary)
{
    $search  = array("ғ","ӣ","ҷ","ҳ","қ","ӯ","Ғ","Ӣ","Ҷ","Ҳ","Қ","Ӯ");
    $replace = array("г","и","ч","х","к","у","Г","И","Ч","Х","К","У");
    $request = str_replace($search, $replace, $request); // replace special letters to default cyrillic letters

    $result = preg_replace_callback("/\pL+/u", function ($m) use ($dictionary) {
    $word = mb_strtolower($m[0]);
    if (isset($dictionary[$word])) {
        $repl = $dictionary[$word];
        // Check for some common ways of upper/lower case
        // 1. all lower case
        if ($word === $m[0]) return $repl;
        // 2. all upper case
        if (mb_strtoupper($word) === $m[0]) return mb_strtoupper($repl);
        // 3. Only first letters are upper case
        if (mb_convert_case($word,  MB_CASE_TITLE) === $m[0]) return mb_convert_case($repl,  MB_CASE_TITLE);
        // Otherwise: check each character whether it should be upper or lower case
        for ($i = 0, $len = mb_strlen($word); $i < $len; ++$i) {
            $mixed[] = mb_substr($word, $i, 1) === mb_substr($m[0], $i, 1) 
                ? mb_substr($repl, $i, 1)
                : mb_strtoupper(mb_substr($repl, $i, 1));
        }
        return implode("", $mixed);
    }
    return $m[0]; // Nothing changes
    }, $request);


    return $result;
}

问题

How do I properly correct the input text?

Input
Кори хатти аз фанни забони точики барои забони давлати дар микёси чахони.
Output
Кори хаттӣ аз фанни забони тоҷикӣ барои забони давлатӣ дар миқёси ҷаҳонӣ.

在这里,您很可能需要使用3个数组逐步修复文本。我的算法没有给出合适的结果。所以我创建了一个由两个单词组成的数组($base_special_words)。

我的算法通过字典中的单词来纠正句子:

Step 1.

你需要从temp array数组的元素中创建一个$base_special_words来自句子中出现的那些单词。临时数组如下所示:

$temp_for_base_special_words = array
(
    "кори хатти",
    "хатти аз",
    "забони точики",
    "точики барои",
    "забони давлати",
    "давлати дар",
    "микёси чахони",   
);

所有这些词都在句子中相遇。然后我们删除了临时数组中的那些单词。从句子中删除这些单词后,句子如下所示:

Full sentence before cutting:
Кори хатти аз фанни забони точики барои забони давлати дар микёси чахони. Точик мард аст.
Cutted part of sentence:
Кори хатти аз забони точики барои забони давлати дар микёси чахони
Sentence after cutting:
фанни. Точик мард аст.

Step 2.

然后将使用数组$ default_words检查句子的剩余部分,并剪切句子中此数组中的单词。

Sentence before cutting in step 2:
фанни. Точик мард аст.
Cutted part:
фанни
Sentence after cutting:
. Точик мард аст.
Array with cutted words:
$temp_for_default_words = array("фанни");

Step 3.

从$ base_words数组中可用的句子的其余部分剪切这些单词。

Sentence before cutting in step 3:
. Точик мард аст.
Cutted part:
Точик
Sentence after cutting:
. мард аст.
Array with cutted words:
$temp_for_base_words = array ("точик");

要约的其余部分必须暂时切割和隐藏,以便不进行任何处理。

Sentence part for hidden:
. мард аст.

最后,您需要使用字典替换使用三个新数组并返回隐藏部分。

纠正步骤

Step 1.

Usage `$temp_for_base_special_words`:

使用$temp_for_base_special_words值来查找$temp_for_base_special_words[$value]中的键($base_special_words)的值,并将这些键替换为输入文本中的值。

Step 2.

Usage `$temp_for_default_words`:

使用$temp_for_default_words值来查找$temp_for_default_words[$value]中的键($base_default_words)的值,并将这些键替换为输入文本中的值。

Step 3.

Usage `$temp_for_default_words`:

使用$temp_for_base_words值来查找$temp_for_base_words[$value]中的键($base_words)的值,并将这些键替换为输入文本中的值。

Step 4.

Return hidden part of text to input coordinates
php arrays regex preg-replace preg-replace-callback
1个回答
1
投票

@ctwheels想要告诉你的是使用str_ireplace (documentation),如果你想用不区分大小写来纠正单词。

<?php
     $test="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
     $word=explode(" ",$test); //This function is need for take all the words individually, the link of the function is above
     foreach($word as $key=>$value)
        if (array_key_exists($value,$YourArrayWithCorrectWord))
            $word[$key]=$YourArrayWithCorrectWord[$value]; //This, if i don't make mistakes, take the correct word and assigns to the wrong word.

     $TestCorrect=implode(" ",$word);
?>

如果你有什么不明白的地方,请写信给我。

我希望我能帮到你。

文档:Here the documentation of explode

Here the documentation of implode

Here the documentation of array_key_exsist

附:此方法存在无法将两个或多个单词纠正在一起的问题。

© www.soinside.com 2019 - 2024. All rights reserved.