Count the number of times a word begins with "co" in a text using Text Analytics Toolbox

1 view (last 30 days)
Dear community,
I have a pdf with news headlines, and I need to count the number of words each title has and the number of times the words starting with "co" and the word "price" appear in each title. I have not much experience using the Text Analytics Toolbox in Matlab. As far as I can see, "tokenizedDocument" already gives you the total number of words (or tokens) per headline, and "context" counts a specific word. However, I do not know how to ask Matlab to look for words starting with "co". Also, how do I get this information displayed in a table?
I leave my pdf and my code.
I really appreciate any help you can provide!
filename = "Factiva_sample_headlines_1.pdf";
str = extractFileText(filename);
textData = split(str,[newline newline]); %split the text into separate news using split
textData = textData(cellfun(@(s)isempty(regexp(s,'Page')),textData)); %Erase data related to number of page
cleanedDocuments = tokenizedDocument(textData); %Create an array of tokenized documents.
  12 Comments
Angelavtc
Angelavtc on 22 Apr 2022
Oh la la, it seems more complex than expected :( perhaps I should move to another software 😭. In any case, thank you very much @Stephen!
Angelavtc
Angelavtc on 23 Apr 2022
@Stephen Sorry for the inconvenience again, but I have managed to transform the file to html format (https://drive.google.com/file/d/1Z5bW98_gWohr2appS8zKgxCC1_mlLpzc/view?usp=sharing) Now the problem is that when I use :
filename = "Factiva_1.html";
str = extractFileText(filename);
I only get one article loaded. Any idea how to make matlab read all of them and classify them by title, date and body?
Thank you very much!

Sign in to comment.

Accepted Answer

Jonas
Jonas on 21 Apr 2022
Edited: Jonas on 21 Apr 2022
are your searching for something like in this example, applied to your textData?
a={'cotrol', 'alcotro','conect','trial','co'};
cellfun(@(in) strcmp(in(1:2),'co'),a)
ans =
1×5 logical array
1 0 1 0 1
you can sum that array to get the total number of words starting with "co"
  8 Comments
Walter Roberson
Walter Roberson on 25 Apr 2022
See also contains() and patternarray
(but I am old fashioned and find regexp easier to work with... but it can get tricky!)

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!