如何使该功能适用​​于成对单词? [英] How to make the function work for pair words?

查看:91
本文介绍了如何使该功能适用​​于成对单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的功能现在只能使用一个单词。例如,我在关联数组中有单词。我的函数用文本中的值替换数组键。该函数将单词保留为小写,但是当您替换单词时,它将返回写在文本上的传入单词寄存器。现在,该功能无法使用单词对替换其他单词对替换单词对。

My function now only works with one word. For example, I have words in an associative array. And my function replaces the array key with its value in the text. And the function to keep the words in the lower case, but when you replace words it will return the incoming word register that is written on the text. Now the function can not work with pairs of words to replace pairs of words with other pairs of words.

// Function:

function replaceKeyToValue($request, $dict){
    $response = preg_replace_callback("/\pL+/u", function ($m) use ($dict) {
        $word = mb_strtolower($m[0]);
        if (isset($dict[$word])) {
            $repl = $dict[$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 $response;
 }

    // Example associative array

    $dict = array
    (
      "make"=>"take",
      "cool"=>"pool",
      "узбек"=>"ӯзбек",
    );

    $text = 'Make COOL узБЕК';

    echo replaceKeyToValue($text, $dict);



输出:



Output:

Take POOL ӯзБЕК

如何重做该函数,以便它也可以配对单词

How will the function be redone so that it can also pair words into pair words?

$array = array
(
  "take pool" => "pool take", 
  "get book" => "set word", 
  "узбек точик" => "ӯзбек тоҷик"
);

$example_text = "Take POOL Get BooK УзБеК ТоЧИК";


推荐答案

第一件事:将案例转换排除在问题之外并编写一个专用的函数来处理它。

First thing: push your case transformation out of the problem and write a dedicated function to handle it.

关于单词对:您可以使用以下方法解决问题:

About the word pairs: You can solve the problem using:


  • 具有可选子模式的前瞻以捕获第二个单词

  • a static 布尔变量(在回调函数中定义)以了解如果先前的匹配项是现有的两个单词子字符串中的第一个单词。

  • a lookahead with an optional subpattern to capture a second word
  • a static boolean variable (defined in the callback function) to know if the previous match was the first word of an existing two words substring.

您仅需要以下模式:

~\b\pL+\b(?=( \pL+\b)?)~u

提前行允许在单词的每个开头(甚至从开始,也可以在字符串的结尾)遍历字符串?=(\pL + \b)?)是一个始终正确的断言()。因为它不占用任何字符。

The lookahead allows to walk the string at each start of word (even at the end of the string since (?=( \pL+\b)?) is an always true assertion.) since it doesn't consume any character.

这非常简单:


  • 布尔变量设置为 false

  • 当布尔值为false且 $ m [0]。$ m [1] 小写时存在于字典键中,然后将布尔值设置为 true 并返回字典值,否则返回 $ m [0]

  • 当布尔值为true时,将其设置为 false 并返回空字符串

  • the boolean variable is set to false at the beginning.
  • when the boolean is false and $m[0].$m[1] in lowercase exists in the dict keys, then set the boolean to true and return the dict value, else return $m[0]
  • when the boolean is true, set it to false and return an empty string

优势:您不必担心dict的大小。使用相同的想法,您甚至可以将算法扩展为更多的单词而几乎不做任何更改,也可以处理其中项目键具有不同单词数的字典。

Advantage: You don't have to take care about the dict size. Using the same idea, you can even extend the algorithm to more words with little changes or handle a dict in which items keys have a different number of words.

建议:何时您认为要更改回溯限制或建立巨大的替代,请不要这样做。这仅表示您的方法不是好方法。

Advice: when you think to change the backtracking limit or to build a giant alternation, don't do it. It only means your approach isn't the good one.

这篇关于如何使该功能适用​​于成对单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆