dt614037527 2017-10-26 15:46
浏览 45

如何分阶段替换单词以获得所需的结果?

Brief

Hello. I just can not get the result I want. So I divided the problem into pieces to solve. If of course someone will decide. If in general this is possible. I have text and three arrays. It is necessary to replace in three stages to solve the problem.

Examples:

$array_for_step1 = array // Array for 1-st step
(
    "ewery day"     => "every day",
    "to school"       => "to univer",
);

$array_for_step2 = array // Array for 2-nd step
(
    'I',
    'go',
    'metro',
);

$array_for_step3 = array // Array for last 3-rd step
(
    "my"   => "he",
    "metro"  => "bus",
);

Input text:

$input = "Ewery day I go To SchooL with metro. My friend too go to school but without metro.";

Comments to the decision stages:

Step 1:

Here you need to replace the keys of the array with their values in the text. Input text form after 1-st step replace using array $array_for_step1:

Highlighted bold - changed words in the text.

Every day I go To UniveR with metro. My friend too go To UniveR but without metro.

Step 2:

Highlighted bold - words that do not need to be replaced in the next step

Here you need to find words from the array and mark them out from the text so that they are not replaced or not available for replacement for the third step. Input text form after 2-nd step replace using array `$array_for_step2`:

Every day I go To UniveR with metro. My friend too go To UniveR but without metro.

Step 3:

Here you need to replace the keys of the array with their values in the text. Input text form after 3-rd step replace using array $array_for_step3:

Highlighted bold - Allocated those words that are not given a change.

Every day I go To UniveR with metro. My friend too go To UniveR but without metro.

Final result:

"Every day I go To UniveR with metro. My friend too go To UniveR but without metro."

My example function for replace array keys to value an any string cases:

function ReplKeyToValue($request, $dictionary) // $request = string && $dictionary associative 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;
}

I gave an example of the text and the value of the array in English. But this is so that you can decide and understand easily. But I need that your solution work for Cyrillic text and array.

  • 写回答

1条回答 默认 最新

  • doulu4413 2017-10-27 06:20
    关注

    I am pasting the code for your reference in Perl language for this very task.

    use strict;
    
    my $text = "Ewery day I go To SchooL with metro. My friend too go to school but without metro.";
    
    #Applying Regex Search and Replace for the 1st hash.
    my %hash1 = (
    
            "ewery day" => "every day",
            "to school" => "to univer",
        );
    
    for my $key_hash1(keys %hash1){
    
        if($text =~ m/\Q$key_hash1\E/ig){
            $text =~ s/$key_hash1/$hash1{$key_hash1}/ig;
        }
    }
    
    print "Result after 1st task:".$text."
    ";
    
    #2nd and 3rd Task
    
    my @array2 = ('I','go','metro');
    
    my %hash2 = (
    
            "my" => "he",
            "metro" => "bus",
        );
    
    for my $key_hash2(keys %hash2){
    
        if ( grep(/^\Q$key_hash2\E$/, @array2 ) ) {
           #print "$key_hash2 already present in the exceptional array
    ";
            next;
        }
        else{
            #print "Replace in text $key_hash2 with $hash2{$key_hash2}
    ";
            $text =~ s/$key_hash2/$hash2{$key_hash2}/ig;
        }
    }
    print "Result after 2nd task:".$text."
    ";
    

    You can play with it in the following url - Code Link

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题