【问题标题】:How to match certain words between two strings (in MATLAB)?如何匹配两个字符串之间的某些单词(在 MATLAB 中)?
【发布时间】:2014-03-14 17:26:00
【问题描述】:

在以下两个字符串中,“rabbit”和“tree”这两个词是匹配的:

str1 = ('rabbit is eating grass near a tree');
str2 = ('rabbit is sleeping under tree');

假设cmp 是一个声明为比较两者的变量。我希望结果为:

cmp = 2

或显示两个单词匹配的内容。我该怎么做?

【问题讨论】:

  • 我建议尝试探索 MATLAB 的字符串搜索/比较函数,尝试解决问题,然后再提出一些更具体的问题。
  • 我想要返回一个整数值,如果上面的字符串中有 3 个单词匹配,那么想要输出为 3,因为它将帮助我执行 if-else 条件,这将有助于我未来的工作
  • 不应该 cmp 为 3,因为单词 'is' 在示例中也匹配?
  • 是的,它将是 3,但我会尝试删除停用词
  • 您会自己删除它还是 MATLAB 代码必须这样做?因为如果 MATLAB 代码必须这样做,那将意味着要进行另一次比较。

标签: arrays matlab match words stop-words


【解决方案1】:

根据另一个答案,将字符串拆分为唯一单词的单元格数组。

str1= ('rabbit is eating grass near a tree');
str2= ('rabbit is sleeping under tree');

% split string into cell array of unique strings
split1 = regexp(str1,'\s','Split');
split2 = regexp(str2,'\s','Split');

或者更高版本的 MATLAB (IIRC R2013a) 包含一个 strsplit() 函数,因此拆分可以减少到

split1 = strsplit(str1);
split2 = strsplit(str2);

然后使用 intersect() 函数获取两个元胞数组之间共有元素的数量。添加长度以返回整数计数。

cmp = length(intersect(split1,split2));

【讨论】:

  • 谢谢先生,真的很有帮助。我搜索了很多以找到如何匹配给定字符串中的特定单词,但找不到任何解决方案。这真的很有帮助。
  • +1 简洁明了。我认为unique 没有必要,因为intersect 自动只考虑唯一值。正如ismember 所做的那样,与我的示例进行比较。
  • 谢谢 - 你说得对,不需要唯一。让它更简单!
【解决方案2】:

“疯狂”bsxfun 方法,可能类似于intersect,但未经测试 -

功能-

function out = cell2_matchind(split1,split2)

c1 = char(split1)-'0';
c2 = char(split2)-'0';
if size(c1,2)<size(c2,2)
    c1 = [c1 -16.*ones(size(c1,1),size(c2,2)-size(c1,2))];
else
    c2 = [c2 -16.*ones(size(c2,1),size(c1,2)-size(c2,2))];
end
out = any(squeeze(sum(bsxfun(@eq,permute(c1,[3 2 1]),c2),2))==size(c2,2),2);

主 MATLAB 脚本 -

% Source of stopwords- http://norm.al/2009/04/14/list-of-english-stop-words/
stopwords_cellstring={'a', 'about', 'above', 'above', 'across', 'after', ...
    'afterwards', 'again', 'against', 'all', 'almost', 'alone', 'along', ...
    'already', 'also','although','always','am','among', 'amongst', 'amoungst', ...
    'amount',  'an', 'and', 'another', 'any','anyhow','anyone','anything','anyway', ...
    'anywhere', 'are', 'around', 'as',  'at', 'back','be','became', 'because','become',...
    'becomes', 'becoming', 'been', 'before', 'beforehand', 'behind', 'being', 'below',...
    'beside', 'besides', 'between', 'beyond', 'bill', 'both', 'bottom','but', 'by',...
    'call', 'can', 'cannot', 'cant', 'co', 'con', 'could', 'couldnt', 'cry', 'de',...
    'describe', 'detail', 'do', 'done', 'down', 'due', 'during', 'each', 'eg', 'eight',...
    'either', 'eleven','else', 'elsewhere', 'empty', 'enough', 'etc', 'even', 'ever', ...
    'every', 'everyone', 'everything', 'everywhere', 'except', 'few', 'fifteen', 'fify',...
    'fill', 'find', 'fire', 'first', 'five', 'for', 'former', 'formerly', 'forty', 'found',...
    'four', 'from', 'front', 'full', 'further', 'get', 'give', 'go', 'had', 'has', 'hasnt',...
    'have', 'he', 'hence', 'her', 'here', 'hereafter', 'hereby', 'herein', 'hereupon', ...
    'hers', 'herself', 'him', 'himself', 'his', 'how', 'however', 'hundred', 'ie', 'if',...
    'in', 'inc', 'indeed', 'interest', 'into', 'is', 'it', 'its', 'itself', 'keep', 'last',...
    'latter', 'latterly', 'least', 'less', 'ltd', 'made', 'many', 'may', 'me', 'meanwhile',...
    'might', 'mill', 'mine', 'more', 'moreover', 'most', 'mostly', 'move', 'much', 'must',...
    'my', 'myself', 'name', 'namely', 'neither', 'never', 'nevertheless', 'next', 'nine',...
    'no', 'nobody', 'none', 'noone', 'nor', 'not', 'nothing', 'now', 'nowhere', 'of', 'off',...
    'often', 'on', 'once', 'one', 'only', 'onto', 'or', 'other', 'others', 'otherwise',...
    'our', 'ours', 'ourselves', 'out', 'over', 'own','part', 'per', 'perhaps', 'please',...
    'put', 'rather', 're', 'same', 'see', 'seem', 'seemed', 'seeming', 'seems', 'serious',...
    'several', 'she', 'should', 'show', 'side', 'since', 'sincere', 'six', 'sixty', 'so',...
    'some', 'somehow', 'someone', 'something', 'sometime', 'sometimes', 'somewhere', ...
    'still', 'such', 'system', 'take', 'ten', 'than', 'that', 'the', 'their', 'them',...
    'themselves', 'then', 'thence', 'there', 'thereafter', 'thereby', 'therefore', ...
    'therein', 'thereupon', 'these', 'they', 'thickv', 'thin', 'third', 'this', 'those',...
    'though', 'three', 'through', 'throughout', 'thru', 'thus', 'to', 'together', 'too',...
    'top', 'toward', 'towards', 'twelve', 'twenty', 'two', 'un', 'under', 'until', 'up',...
    'upon', 'us', 'very', 'via', 'was', 'we', 'well', 'were', 'what', 'whatever', 'when',...
    'whence', 'whenever', 'where', 'whereafter', 'whereas', 'whereby', 'wherein',...
    'whereupon', 'wherever', 'whether', 'which', 'while', 'whither', 'who', 'whoever',...
    'whole', 'whom', 'whose', 'why', 'will', 'with', 'within', 'without', 'would', 'yet',...
    'you', 'your', 'yours', 'yourself', 'yourselves', 'the'};

str1 = ('rabbit is eating grass near a tree and will be sleeping inside the tree-hole');
str2 = ('rabbit is sleeping under tree and after waking up will be eating the nuts nearby');

split1 = unique(regexp(str1,'\s','Split'),'stable');
split2 = unique(regexp(str2,'\s','Split'),'stable');

cw_split2 = split2(cell2_matchind(split1,split2))
cw_split2_nostopwd = cw_split2(~cell2_matchind(stopwords_cellstring,cw_split2))
cmp = numel(cw_split2_nostopwd)

输出 -

cw_split2 = 
    'rabbit'    'is'    'sleeping'    'tree'    'and'    'will'    'be'    'eating'    'the'

cw_split2_nostopwd = 
    'rabbit'    'sleeping'    'tree'    'eating'

cmp =
     4

【讨论】:

【解决方案3】:

我假设它们匹配的位置或顺序没有限制。首先,您需要将句子拆分为单个单词,删除所有重复项,然后查看句子 2 中的任何单词是否与第一个句子中的单词匹配。

现在,如果订购确实很重要,那就不是那么简单了,但是您的问题并未表明此类限制

str1= ('rabbit is eating grass near a tree');
str2= ('rabbit is sleeping under tree');
split1 = unique(regexp(str1,'\s','Split'));
split2 = unique(regexp(str2,'\s','Split'));

% Storing all words in the first sentence into a map for quick search/access
dict = containers.Map();
for ii = 1:numel(split1)
   dict(split1{ii}) = true; 
end

% create temp holding cell array, then loop through, looking to see if 
% any word in the second sentence is stored in the dictionary made from
% the first sentence. 
matches = {};
for jj = 1:numel(split2)
    if dict.isKey(split2{jj})
        matches = [matches,split2{jj}]; % not best but length initially unknown
    end
end

numMatches = numel(matches) % return the number of matches

变量matches 将包含两个句子之间匹配的所有单词。

【讨论】:

  • 谢谢先生的帮助,但我希望返回一个整数值,如果上面的字符串中有 3 个单词匹配,则希望输出为 3,因为它将帮助我执行 if-else条件,这将对我以后的工作有所帮助
  • @user3416063 说真的,你可以做一个 numel 。但如果你想让我确定添加,它是固定的。我请您在发布可以很容易解决的问题/cmets 之前对理解语言进行研究
  • 应该不需要地图和循环的复杂性。 intersect 函数将返回两个字符串元胞数组中的公共元素。
  • 谢谢先生,@ MZimmerman6,我是 MATLAB 新手,他们没有时间对 MATLAB 进行一些研究,因为我完成工作的时间有限。以后一定会这样做的。
  • @Adrian 我忘记了 intersect 函数。这是我能想到的第一件事,并且可能类似于 intersect 在后台所做的事情。而且,如果你想用另一种语言做类似的事情,这提供了一个非常快速和简单的解决方案,可以在线性时间内执行。
【解决方案4】:

ismember 只需要一行。

str1 = ('rabbit is eating grass near a tree');
str2 = ('rabbit is sleeping under a tree');

result = sum( ismember( strsplit(str1), strsplit(str2) ) )

result =

    4               %// I included also the article "a"

请注意,以下句子的结果是相同的:

str1 = ('rabbit is eating grass near a tree, an oak tree');
str2 = ('rabbit is sleeping under a tree and is dreaming about a tree');

result = sum( ismember( strsplit(str1), strsplit(str2) ) )

不需要 MZimmerman6 建议的提前删除重复项。


如果您想过滤不需要的字符串的结果,您可以引入另一个字符串元胞数组(所有例外):

str3 = {'is','a'}
unwanted = sum( ismember( intersect( strsplit(str1), strsplit(str2) ), str3 ) )

unwanted =

     2

总起来可能是这样的:

str1 = ('rabbit is eating grass near a tree, an oak tree');
str2 = ('rabbit is sleeping under a tree and is dreaming about a tree');
str3 = {'is','a'}

[x,y,z] = deal( strsplit(str1), strsplit(str2), str3 )
result = sum(ismember(x,y)) - sum(ismember(intersect(x,y),z))
       =       4            -            2           =        2

【讨论】:

  • 谢谢先生,这很有帮助,在我为如何使用 setdiff 命令而苦苦挣扎之前。
【解决方案5】:

将此用于不区分大小写;

CMP = strcmpi(string,string)

将此用于区分大小写;

CMP = strcmpi(string,string)

如果 CMP 为 1,则它们相同,如果 0,则它们不是。

如果您不想使用空格,以便更好地比较,请先修剪它们并进行比较。

用于修剪;

newString = strtrim(str)

【讨论】:

  • 这不是比较整个字符串而不是单个单词吗?
  • 它应该比较整个字符串为什么不呢?
  • 因为它们是在匹配单词的计数之后——而不是整个字符串是否匹配。
猜你喜欢
  • 2015-04-08
  • 2017-04-02
  • 1970-01-01
  • 2021-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-29
  • 1970-01-01
相关资源
最近更新 更多