R
R
Roman Khodakovsky2017-06-22 13:39:57
PHP
Roman Khodakovsky, 2017-06-22 13:39:57

How to find all combinations of elements in an array?

Task: you need to make a function that receives a string, for example "Grid for ventilation", and returns various forms and declensions of this string, for example "Grids for ventilation", while you also need to get options with a different order, for example "grille for ventilation"
I use phpmorphy to get all forms of a word, I end up with a 2-x dimensional array. The problem is that I can’t figure out which algorithm to get all the variations of the elements of this array.
The problem is that the dimension of the array is a variable. In which direction to dig?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Korobkov, 2017-06-22
@BorisKorobkov

Head-on solution:
- split the phrase into words (an array of N lines will be obtained),
- for each word, build all its word morphs (as a result, an array of N arrays),
- recursively take the next word N times from the Nth element of the array
- for mixing words, either a separate method on the resulting array, or the N-square of recursions of the previous paragraph
Bottom line: even a powerful server will die already in 5 words
Smart solution: feed the original phrase to Sphinx / Lucene - they will find the right one among your content

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question