1

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.

John
  • 468
  • 3
  • 16
  • You put a lot of effort explaining what you need, but you could have put all less effort making a smaller example of what you want to achieve, which I think it is the correct way to ask in SO. And by the way, IMO the vast majority of people here may find confusing to work with Cyrillic. – lilezek Oct 26 '17 at 15:48
  • I already did a lot of things I could but I can not. I asked a question like this but there was no answer. I do not know well the regular expression. And the first and second step is not yet possible for me. Because I could not replace two words from the array key in their value.And I could not exclude words for substitution in the second step.I could only replace one word from the key with a value in the text in any case. You can see my [old question](https://stackoverflow.com/questions/46914803/how-to-implement-my-algorithm-text-correction-for-the-replacement-of-words-in-th) for similar task – John Oct 26 '17 at 15:58
  • Well, I'm talking from my own experience, that if you just simplify the question to the single step you don't know how to work out, with examples written in the English alphabet, you will get an answer. By the way, if you want to replace words, it would be great if you specify that you want or not these words to be found in a context-sensitive manner. – lilezek Oct 26 '17 at 17:52
  • Is it possible to save all the steps to update the text of the question in English? @lilezek – John Oct 27 '17 at 03:20
  • Question updated with english text examples! @lilezek – John Oct 27 '17 at 04:25

1 Answers1

-1

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."\n";

#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\n";
        next;
    }
    else{
        #print "Replace in text $key_hash2 with $hash2{$key_hash2}\n";
        $text =~ s/$key_hash2/$hash2{$key_hash2}/ig;
    }
}
print "Result after 2nd task:".$text."\n";

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

Mohit
  • 608
  • 4
  • 19
  • Thanks! I need the language pph and your code, I looked and it incorrectly works. @Mohit – John Oct 27 '17 at 10:57