Answer the question
In order to leave comments, you need to log in
What is the best way to organize keyword research?
Good afternoon. There is a task of importing goods to the site from xml. Actually, the unloading itself is done and everything works, but I am concerned about the fact that when searching for keywords, cubic complexity is obtained (three nested loops), which means that as both the database and the xml files increase, the duration of the unloading will increase, and significantly.
The unloading algorithm is now as follows:
We make a selection from the database from the "categories" table.
For each "correct" element from xml, we take the "category" field, then in each category that is in the database there is a "keywords" field, which, in turn, are divided into elements. We compare the elements of the "keywords" field from the database with the "category" field from xml. If found, we match our upload with a category in the database, otherwise we send it to the "Other" category. Here's a simplified code:
$validated; // "правильные" элементы из xml
$categories = getCategoriesFromDb; // выборка из таблицы категории
foreach ($validated as $item) {
foreach($categories as $category) {
$splitted = explode(',',$category['keywords']) {
foreach($splitted as $element)
// проверка нахождения элемента в поле категория в выгрузке xml
}
}
}
Answer the question
In order to leave comments, you need to log in
The correct algorithm for finding keys in a string is Aho-Korasik. It must be implemented in your xml parser.
If you're not working with XML using XPATH, it's time to start doing it.
As @Armenian Radio quite rightly pointed out, Aho-Korasik is a great algorithm and already in the box. The algorithm, however, searches for ALL occurrences of ALL substrings, which may be overkill for the task. It might be more efficient to build some kind of BinarySearchTree from the keywords, or just sort them and use some kind of BinarySearch. (I'm not a PHP expert and don't know what's in your box)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question