how to create a function that counts all words in a document?

조회 수: 8 (최근 30일)
Noah Swiston
Noah Swiston 2015년 3월 25일
답변: Christopher Creutzig 2018년 11월 26일
I am attempting to make a function that takes in a speech as an input argument in a text file. I would then like to read the speech and have matlab count and return the number of occurrences for every word in the speech. Any ideas?

답변 (3개)

John D'Errico
John D'Errico 2015년 3월 25일
편집: John D'Errico 2015년 3월 25일
Easy, peasy.
I'd...
1. Using lower, make all words lower case, so you need not worry about capitalization.
2. Apply my allwords code to the string. Use spaces and any appropriate punctuation as separator characters.
3. Use unique to get the distinct words in that list. The third argument from unique will be important.
4. Apply accumarray to that third argument from unique to count the words.
Essentially 4 quick lines of code.
  댓글 수: 2
Noah Swiston
Noah Swiston 2015년 3월 25일
The allwords function only takes in a string as an input. How would i take the text file, open it, and convert it into a string so the allwords function could be used?
John D'Errico
John D'Errico 2015년 3월 26일
textread is the simple answer. Or use fopen, then fread, or fgets or fgetl.
No need to convert anything to a string if it is a string already as read in.

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


Sarah Palfreyman
Sarah Palfreyman 2018년 4월 30일
Get the size of the string returned by extractFileText in Text Analytics Toolbox

Christopher Creutzig
Christopher Creutzig 2018년 11월 26일
Check out the bagOfWords class in Text Analytics Toolbox. It solves exactly this problem.
(You may want to have some preprocessing to match exactly what you consider to be the same word. E.g., lowercasing, so “House” and “house” are the same. Unless you want Dr. House not to be counted as a house, of course …)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by