2

Example string: "outofthebox"

I want to get output like this: Array ( [0] => out [1] => of [2] => the [3] => box )

What do I get right now: Array ( [0] => out [1] => oft [2] => heb [3] => ox )

I don't know how it's possible. I need that logic, how can I get more meaningful results.

I'm building it on PHP based on this https://stackoverflow.com/a/481773/17035224 Python answer. But I'm not good at Python. In this python script it's returning results like exactly what I want. Another script in python called 'wordninja' is working great too.

My PHP script:

<?php
$db = new PDO("mysql:host=localhost;dbname=strings", "root", "");
$text = "outofthebox";

$finish = false;
$words = [];
$find = false;
$start = -1;
$added = false;
$comp = [];
for($i = 0; $i<strlen($text); $i++) {
    if(count($words) > 0) {
        $last = max($words);
        $comp[] = $last;
        $words = [];
        $start = strlen(implode("", $comp));
        if($added === true) {
            $added = false;
        }else {
            $start++;
        }
    }else {
        $start++;
    }

    $part = "";
    for($j = $start; $j<strlen($text); $j++) {
        $part .= $text[$j];
        echo $part."<br>";
        $check = checkWord($part);
        if($check === true) {
            $added = true;
            $words[] = $part;
        }
    }
}

print_r($comp);

function checkWord($text) {
    global $db;
    $check = $db->query("select * from strings where string='".$text."'")->fetch(PDO::FETCH_ASSOC);
    if(isset($check["id"]) == true) {
        return true;
    }
    return false;
}

Other difference is as you see I'm using mysql database for dictionary instead of txt.

cihan100
  • 21
  • 1
  • 3
    Making a new database query every time `checkWord` is called, is very inefficient. You should rather read your whole dictionary from the database into an internal data structure first, and then do your look-ups in the latter. – CBroe Sep 29 '21 at 14:33
  • @CBroe thanks, I will make that improvements after build the main logic. – cihan100 Sep 29 '21 at 14:35
  • Did you check if those unexpected words are inside the dictionary? If the select returns a result then you have those words inside the mySQL dictionary itself! – Agamemnon Katradis Sep 29 '21 at 14:56
  • @AgamemnonKatradis yes, it's a real word so it's in the dictionary of course. But with the same dictionary, that Python scripts I mentioned works better. – cihan100 Sep 29 '21 at 15:37

1 Answers1

1

If you change the checkWord function to this :

function checkWord($text) {
   
    $arr = [
        'out',
        'of',
        'the',
        'box',
    ];

    if(in_array($text, $arr)) {
        return true;
    }
    
    return false;
}

You will see that the result will be :

Array ( [0] => out [1] => of [2] => the [3] => box )

So my guess is that you have false-positives coming from your query, check that and you'll solve the problem.

Mihai C
  • 33
  • 7
  • So how I'm gonna create that array like that, that is the main question.. In dictionary there is a word called 'oft', it's a real word. But other Python scripts I mentioned returning more meaningful results – cihan100 Sep 29 '21 at 15:33
  • Try putting a break after your word was added, after this line : $words[] = $part; put break; – Mihai C Sep 29 '21 at 16:02