A
A
Anton2017-08-23 23:32:08
Algorithms
Anton, 2017-08-23 23:32:08

Algorithm for handling negative words?

Hello. I am writing an application for working with Yandex.Direct and I need to somehow handle negative keywords. How negative words work: there are search queries, for example, "buy a car moscow" and if we have a negative word "car" advertising will not be shown in the search results for this query.
The essence of my problem is that there are many types of negative words / phrases that are expressed using special characters:

  • "car" - the usual minus word, the phrase is minus when this word is in the search query
  • "buy a car" - a common negative phrase, the phrase is minus when all the words from the minus phrase are present in the search query, regardless of the order
  • "!car" - a minus word with a fixed word form, that is, it will only minus the query if the phrase contains the word "car", and requests
    with the word "cars" will not be minus as it would be with the usual minus word
  • "[buy a car]" - a minus phrase with a fixed word order, it will negate the request if it contains these words in the same order, unlike the usual minus phrase
  • and other special characters

The problem is that one minus phrase can contain a lot of special characters, for example, "-buy !cars [cheap Moscow] [new or !bu]" and I need to process them somehow.
I came up with the idea that I need to create classes for all types of negative phrases, with a common interface for checking a search query, I got this list of classes:
  • Negative word, example: "car"
  • Minus word with a fixed word form, example: "!cars"
  • Negative phrase, example: "buy a car"
  • Minus a phrase with words with a fixed word form, example: "buy! cars"
  • Negative phrase with a fixed word order, example: "[buy a car]"
  • Minus phrase with a fixed word form and a fixed word order, example: "[buy! cars moscow]"
  • A complex minus phrase that includes all types, it parses the phrase and creates the appropriate classes, for example: "buy !cars [cheap moscow] [new or !bu]"
  • A class that defines the minus phrase type, and creates the required class

This option works, but it seems redundant to me, due to the large number of classes. Please tell me if a more elegant structure comes to mind

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