How do I compare two sentences with similar words?
Show older comments
I have two string sentences with similar words, I want to know if there is a common word between them, case insensitive.
string1='Install peoplenet antenna';
string2='ANT-PMG,PEOPLENET,30';
So I want the answer to be 'true' or logical because 'peoplenet' is there in both the sentences.
Accepted Answer
More Answers (1)
I wouldn't be surprised if a more robust algorithm already exists but this simple appraoch takes the following steps.
- Replace the non-letters with empty spaces {, . ; ( 0 - etc... }
- Converts all characters to lower case
- Splits the strings into words
- Detects if there are matching words.
isMatch is the logical output that identifies if there is a match or not.
string1='Install peoplenet antenna';
string2='ANT-PMG,PEOPLENET,30';
% replace non-letters with a space and convert to lower case.
str1 = lower(string1);
str2 = lower(string2);
str1(~isstrprop(str1,'alpha')) = ' ';
str2(~isstrprop(str2,'alpha')) = ' ';
% Split the strings into words
words1 = strsplit(str1);
words2 = strsplit(str2);
% Detect matches
isMatch = any(ismember(words1, words2));
% Note, the following line will show which words were similar
% ismember(words1, words2)
1 Comment
Prajwal Venkatesh
on 20 Jan 2020
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!