在以下兩個字符串中,在「兔子」和「樹」是匹配的:如何匹配兩個字符串之間的某些詞(在MATLAB中)?
str1 = ('rabbit is eating grass near a tree');
str2 = ('rabbit is sleeping under tree');
假設cmp
是宣告比較這兩個變量。我想要的結果如下:
cmp = 2
或者表明兩個單詞匹配的東西。我該怎麼做呢?
在以下兩個字符串中,在「兔子」和「樹」是匹配的:如何匹配兩個字符串之間的某些詞(在MATLAB中)?
str1 = ('rabbit is eating grass near a tree');
str2 = ('rabbit is sleeping under tree');
假設cmp
是宣告比較這兩個變量。我想要的結果如下:
cmp = 2
或者表明兩個單詞匹配的東西。我該怎麼做呢?
「瘋狂」bsxfun
方法,其中mig HT類似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
謝謝先生,很好的幫助。 – user3416063
@ user3416063閱讀關於在stackoverflow上參與的幾件事情 - http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work和http://stackoverflow.com/help/privileges/投票機 – Divakar
我假設它們的位置或順序沒有限制。首先,您需要將句子拆分爲單個單詞,刪除任何重複單詞,然後查看句子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
@ user3416063認真,你可以做一個這樣的事情。但如果你想讓我確定,那就修正了。我問你,請在你發佈問題/評論之前進行研究,以便很容易地解決 – MZimmerman6
不應該需要地圖和循環的複雜性。函數intersect將從兩個單元格數組中返回公共元素。 – Adrian
按照其他答案將字符串拆分爲單個單詞數組。
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);
然後使用交叉()函數來獲得共同的元素數在兩個單元陣列之間。添加一個長度以返回整數計數。
cmp = length(intersect(split1,split2));
謝謝先生,真的很有幫助。我搜索了很多,以找到如何匹配給定字符串中的特定單詞,但無法找到任何解決方案。這真的很有幫助。 – user3416063
+1簡潔明瞭。我認爲'獨一無二'是沒有必要的,因爲'交叉'自動只考慮唯一值。作爲'ismember',與我的例子相比。 – thewaywewalk
謝謝 - 你是對的,獨特不是必需的。使它更簡單! – Adrian
隨着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命令來達到這個目的。 – user3416063
我建議嘗試探索MATLAB的字符串搜索/比較功能,對問題進行刺探,然後再回來一些更具體的問題。 –
我想要返回一個整數值,如果3個單詞在上面的字符串匹配,然後希望輸出爲3,因爲它會幫助我執行if-else條件,這將幫助我在未來的工作 – user3416063
不應該cmp是3,因爲'is'這個詞在樣本中也是匹配的? – Divakar