Reputation: 63
I have this sentence:
I want to say hi for everybody here.
I want to check a specific column in a table if it contains some similar sentence according to number of matching words.
Table rows
I need to get the third row since it contains the greatest number of matching words.
Appreciate your kind feedback. It is so urgent.
Upvotes: 0
Views: 372
Reputation: 16257
The full answer with code is a bit long to post, but a solution I have used for a similar problem dealing with people names is to have a table of Word in addition to the table containing the full Sentence. Create an intersect table to make a many to many mapping between sentences and words. When you add a sentence, you also split it into words and update the Words table and the intersect table as well.
Given a sentence to search for, you can construct a query that with a where clause like word in ("I", "want", "to"
...etc., join that to the intersect table, and group on Sentence table id that's in the intersect table. With the grouping you can return a count of the words matched for each Sentence row having one or more matching words. Order by that count and you have the Sentence that matches the most.
This approach doesn't consider order, and spelling must be exact. You will also have to consider how to treat a sentence like "All for one and one for all"
where words repeat.
Upvotes: 0
Reputation: 51504
You should set up a full text index on the column, and use the CONTAINS FREETEXTTABLE function
Upvotes: 3