필터 지우기
필터 지우기

How to compare strings and find most common values between them?

조회 수: 11 (최근 30일)
Johny
Johny 2013년 12월 1일
댓글: Walter Roberson 2013년 12월 1일
So I have a series of strings stored in a cell array, each of which occupies it's own element in that cell array, which for now we will call "tagstrings". Some of these strings are just repeats of each other, and they are not all the same or the same length. "tagstrings" looks something like this;
I ate 34 slices of pizza
I ate 34 slices of pizza
I ate 34 slices of pizza
I ate 35 slices of pizza
I ate 89 slices of cheese pizza
I want to be able to take each of those strings, find the terms and/or words that appear most commonly, then collect them all into one single string that summarizes the most common information word by word. I would also like to limit the term or character length of the ending result string. the order in which the terms appear in the end is not important to me, but would be awesome if we could get that to happen.
In the example above, the code I desire would see that "35" and "89" and "cheese" for example are not very common, and would output a string that excludes these terms. I've tried separating the strings into cell arrays which deconstruct each string into its own cell array word by word using the strread command, but I didn't know what to do after this and it just got more complicated.

답변 (1개)

Walter Roberson
Walter Roberson 2013년 12월 1일
splitphrases_cells = regexp(YourCellStringArray, '\s+', 'split');
splitphrases = horzcat(splitphrases_cell{:});
Now everything is word by word in one cell array of strings, ready for you to count.
Hint: Read all of the possibilities of unique()
  댓글 수: 2
Johny
Johny 2013년 12월 1일
편집: Johny 2013년 12월 1일
Is there a way for me to perform the same operation as you have done using the following code that I have? in the following case, "YourCellStringArray" is a 7*1 cell array full of strings, if that's significant:
numoftimesitappears=length(YourCellStringArray);
words=cell(YourCellStringArray,1);
for i=1:numoftimes
words{i}=strread(YourCellString{i},'%s');
end
what exactly is the difference? With my method, I get "words" to be a cell array with dimensions 7*1 and each cell within it just says "9x1 cell". clicking any of those gives me a vertical listing of the string at that position as opposed to the horizontal one which your code produces( yours makes 1x9's instead).
Walter Roberson
Walter Roberson 2013년 12월 1일
Splitting that way is fine.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by