M
M
Medvedoc2018-03-17 09:15:31
PHP
Medvedoc, 2018-03-17 09:15:31

How to smartly modify a synonymizer written in PHP?

Hello! I found an approximate synonymizer code on the Internet. Modified it a little, but a few problems remain.
If, for example, the synonym database contains the word phone and the word he , then when synonymizing in the word phone , the last two letters change, since the ending is perceived as a word. How can this be bypassed?
And the second problem - I don’t know how best to do synonymization. If there are few words in the dictionary, then it passes normally. But if there are more words, then synonymization does not work for the entire depth of existing synonyms for one word.
The test was carried out on the banal phrase Mobile phone Samsung. I will exchange for a new iphone.
synonymizer code:

<?php
mb_internal_encoding("UTF-8");
    function synonimize($str, $repl_array){
        $keys = array_map(function($key){
            return '#'.$key.'#ui';
        }, $repl_array);
   
        foreach ($keys as $i=>$key) {
            $str = preg_replace_callback($key, function ($match) use ($repl_array, $i) {
                $syns = explode('|', $repl_array[$i]);
                array_splice($syns, array_search(mb_strtolower($match[0]), array_map('mb_strtolower', $syns)), 1);
                return $syns[rand(0, count($syns) - 1)];
            }, $str);
        }
 
        echo preg_replace_callback('~(?:^|\.\s*)\w~u', function($m) {
            return mb_strtoupper($m[0]);
        }, $str);
    }
    
    $str2 = $prefixes[mt_rand(0,count($prefixes)-1)]; //тут можно просто вставить сам текст
    $repl_array2 = "dictionary.txt"; //база с синонимами
    $repl_array3 = file($repl_array2);
    
    for ($x=0; $x<5;$x++){
        echo synonimize($str2,$repl_array3).'<br>';   
    }  
?>

base code:
ты|он|сам|такой
крутой|деловой|навороченный|блатной
cотовый телефон|мобилка|cобильник|aага|nелефон|cотовый|мобильный телефонsamsung|Самсунг|LG|Nokia
новый|новенький|обновленный|хороший
обменяю|отдам|разменяю|поменяю|дам в размен
Iphone|Айфон|IP|Nokia 3210

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question