简体   繁体   中英

php algorithm for creating a list of keywords from a string of keywords

I have a string of keywords to search for in the format: A,B+C,D+E,B+F,E+G+H,...
each letter represent a keyword, and the + is for when I would want all keywords to appear.
There is no guaranteed order to the keywords.
later I will search a DB for names that contain these keywords.
So following my example I will be interesting in names that contain:
A or (B and C) or (D and E) or (B and F) or (E and G and H), etc.

The problem is that I can only query the DB for names that contain a single keyword (it is an API not my DB) so I need to make a list of keywords to retrieve names for, and then check each name to see if it contains all relevant keywords.

I need an algorithm to parse the string and create the list of keywords that will minimize the queries to the DB.

For instance, in my example, I would get names for:
A
B and check if they also contain C or F
E and check if they also contain D or (G and H)

so the algorithm should create keywords list of A,B,E to query for, and also add to each one the keywords it should appear with in the name.

I am working in php so I prefer answers in php, but pseudo code will be fine as well. I hope it is clear...

Make empty array B.
Pass over the given array A, for each word X:
    If not apperars in B, add X to B as index, and set him value to 1/(No. of parts in this conjunction).
    Else, add 1/(No. of parts in this conjunction) to exist value.
Sort B by values, biggest first.
Make your queries from start to end.

The logic is rate by importance of word in all conjunction.

A word that is alone is pretty important, but a word that appears four times with another word is more important.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM