how to create a function that counts all words in a document?
Mostrar comentarios más antiguos
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?
Respuestas (3)
John D'Errico
el 25 de Mzo. de 2015
Editada: John D'Errico
el 25 de Mzo. de 2015
2 votos
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 comentarios
Noah Swiston
el 25 de Mzo. de 2015
John D'Errico
el 26 de Mzo. de 2015
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
el 30 de Abr. de 2018
0 votos
Christopher Creutzig
el 26 de Nov. de 2018
0 votos
(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 …)
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!