通过数组的值交换变量的值,但是在条件下 [英] exchanging values of a variable, by values of an array, but under condition

查看:125
本文介绍了通过数组的值交换变量的值,但是在条件下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码将输出与数组的值进行比较,并且只使用数组中的单词终止操作:

I have a code that compares the output with the values of the array, and only terminates the operation with words in the array:

第一个代码(只是一个例子

$myVar = 'essa pizza é muito gostosa, que prato de bom sabor';
$myWords=array(
    array('sabor','gosto','delicia'),
    array('saborosa','gostosa','deliciosa'),
);

foreach($myWords as $words){
    shuffle($words); // randomize the subarray
    // pipe-together the words and return just one match
    if(preg_match('/\K\b(?:'.implode('|',$words).')\b/',$myVar,$out)){
        // generate "replace_pair" from matched word and a random remaining subarray word
        // replace and preserve the new sentence
        $myVar=strtr($myVar,[$out[0]=>current(array_diff($words,$out))]);
    }
}
echo $myVar;






我的问题:

我有第二个代码,不适用于rand / shuffle(我不想要rand,我想要替换精度,我总是将列0改为1),总是交换价值:

I have a second code, which is not for rand/shuffle(I do not want rand, I want precision in substitutions, I always change column 0 through 1), is to always exchange the values:

// wrong output: $myVar = "minha irmã alanné é not aquela blnode, elere é a bom plperito";
$myVar = "my sister alannis is not that blonde, here is a good place";
$myWords=array(array("is","é"),
    array("on","no"),
    array("that","aquela"),
    //array("blonde","loira"),
    //array("not","não"),
    array("sister","irmã"), 
    array("my","minha"),
    //array("nothing","nada"),
    array("myth","mito"),
    array("he","ele"),
    array("good","bom"),
    array("ace","perito"),
   // array("here","aqui"), //if [here] it does not exist, it is not to do replacement from the line he=ele = "elere" non-existent word  
); 
$replacements = array_combine(array_column($myWords,0),array_column($myWords,1));
$myVar = strtr($myVar,$replacements);
echo $myVar;
// expected output:  minha irmã alannis é not aquela blonde, here é a bom place
//  avoid replace words slice!

预期输出:minhairmãalannisé不是aquela金发女郎,这里a一个地方

    //  avoid replace words slice! always check if the word exists in the array before making the substitution.

alanné blnode elere plperito

它会检查输出是否是真实的单词,它存在于数组myWords中,这样可以避免输入错误如:

it examines whether the output will be of real words, which exist in the array myWords, this avoids typing errors like:

这4个单词不是现有单词,写错误。如何为第二个代码执行此操作?

that 4 words is not an existent words, a writing error. how do you do that for the second code?

简而言之,交换必须由完整的单词/键,现有单词进行。而不是使用关键字切片创建奇怪的东西!

推荐答案

我以前的方法非常低效。我没有意识到你正在处理多少数据,但如果我们超过4000行,那么效率是至关重要的(我想我的大脑一直在考虑 strtr()基于您之前的问题的相关处理)。这是我的新/改进的解决方案,我希望将以前的解决方案留在尘埃中。

My previous method was incredibly inefficient. I didn't realize how much data you were processing, but if we are upwards of 4000 lines, then efficiency is vital (I think I my brain was stuck thinking about strtr() related processing based on your previous question(s)). This is my new/improved solution which I expect to leave my previous solution in the dust.

代码:(演示

$myVar="My sister alannis Is not That blonde, here is a good place. I know Ariane is not MY SISTER!";
echo "$myVar\n";

$myWords=array(
    array("is","é"),
    array("on","no"),
    array("that","aquela"),
    array("sister","irmã"), 
    array("my","minha"),
    array("myth","mito"),
    array("he","ele"),
    array("good","bom"),
    array("ace","perito"),
    array("i","eu")  // notice I must be lowercase
);
$translations=array_combine(array_column($myWords,0),array_column($myWords,1));  // or skip this step and just declare $myWords as key-value pairs

// length sorting is not necessary
// preg_quote() and \Q\E are not used because dealing with words only (no danger of misinterpretation by regex)

$pattern='/\b(?>'.implode('|',array_keys($translations)).')\b/i';  // atomic group is slightly faster (no backtracking)
/* echo $pattern;
   makes: /\b(?>is|on|that|sister|my|myth|he|good|ace)\b/i
   demo: https://regex101.com/r/DXTtDf/1
*/
$translated=preg_replace_callback(
    $pattern,
    function($m)use($translations){  // bring $translations (lookup) array to function
        $encoding='UTF-8';  // default setting
        $key=mb_strtolower($m[0],$encoding);  // standardize keys' case for lookup accessibility
        if(ctype_lower($m[0])){ // treat as all lower
            return $translations[$m[0]];
        }elseif(mb_strlen($m[0],$encoding)>1 && ctype_upper($m[0])){  // treat as all uppercase
            return mb_strtoupper($translations[$key],$encoding);
        }else{  // treat as only first character uppercase
            return mb_strtoupper(mb_substr($translations[$key],0,1,$encoding),$encoding)  // uppercase first
                  .mb_substr($translations[$key],1,mb_strlen($translations[$key],$encoding)-1,$encoding);  // append remaining lowercase
        }
    },
    $myVar);

echo $translated;

输出:

My sister alannis Is not That blonde, here is a good place. I know Ariane is not MY SISTER!
Minha irmã alannis É not Aquela blonde, here é a bom place. Eu know Ariane é not MINHA IRMÃ!

此方法:


  • 只有 1 通过 $ myVar ,而不是 $ myWords

  • 不打扰排序查找数组( $ myWords / $ translations )。

  • 不打扰正则表达式转义( preg_quote())或使模式组件成为文字( \ Q.. \ E )因为只翻译单词

  • 使用字边界所以只更换完整的单词匹配。

  • 使用原子组作为微优化,保持准确性,同时拒绝回溯。

  • 声明 $ encoding 稳定性/可维护性/可重用性的值。

  • 与不区分大小写匹配,但替换为区分大小写...如果英语比赛是:

  • does only 1 pass through $myVar, not 1 pass for every subarray of $myWords.
  • does not bother with sorting the lookup array ($myWords/$translations).
  • does not bother with regex escaping (preg_quote()) or making pattern components literal (\Q..\E) because only words are being translated.
  • uses word boundaries so that only complete word matches are replaced.
  • uses an atomic group as a micro-optimization which maintains accuracy while denying backtracking.
  • declares an $encoding value for stability / maintainability / re-usability.
  • matches with case-insensitivity but replaces with case-sensitivity ...if the English match is:

  1. 全部小写,替换也是如此

  2. 所有上限e(并且大于单个字符),替换

  3. 大写(仅限多字符串的第一个字符),替换


这篇关于通过数组的值交换变量的值,但是在条件下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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