Do I need to make one sql query for each keyword in the text?

I am working on a content redirector, basically it will replace the words with its synonyms.

I have synonyms in mySQL database, the table contains 3 columns

id        int(11)
keyword      varchar(50)
synonyms    varchar(255)

The entries are as follows:

50  slake       abate,slack,decrease,lessen,minify
51  abate       slake,slack,decrease,lessen,minify
52  slack       slake,abate,decrease,lessen,minify
53  decrease    slake,abate,slack,lessen,minify
54  lessen      slake,abate,slack,decrease,minify
55  minify      slake,abate,slack,decrease,lessen

So, my first idea was to first get each word in the text for rewriting (ignoring some keywords in the blacklist) and then make a sql query to see if there is a synonym for this word in the database. But if I have text with 1000 words, will there be too many 1000 sql queries? In addition, some synonyms have 2 words (for example, β€œthrow away”), so I may need to make a lot more queries than a word in the text.

Is there a better way to achieve this?

+3
3

:

Word Table:
    id        int(11) 
    word      varchar(50) 

Synonym Table
    WordId     int(11)
    SynonymId  int(11)

, :

Select W2.Word 
from SynonymTable S
join WordTable W on S.WordId = W.Id
join WordTable W2 on S.SynonymId = W2.Id
where W.word = 'slake' 

WordTable (Word), SynonymTable (WordId) SynonymTable (SynonymId)

:

  • :
  • : - , , .
+5

, . , , , .

0

, SQL . SQL , .

SQL, , , JZD . , (1- - 1, - 2 ..), .

. , SQL- .

. , , . , .

EDIT If you really want to make a robust implementation that can handle verbose phrases, you should use the Rabin-Karp string search algorithm.

0
source

Source: https://habr.com/ru/post/1791770/


All Articles