0
In C++ âtextual analysisâ
How could be computed all words in a .txt file, and also, get the frequency of each word?
1 Answer
+ 4
Miguel Angel Santos Saldivar ,
[edited]: please keep in mind that there may be punctuation characters in the text, that should be removed before splitting is applied
âąfollowing your description, the first task is to read the content from a txt file:
- open the file for reading
- read all lines from the file, but only one at a time. we can do this iteration with a loop
âąthe second task is to get the frequency of the words that build the content of the file:
- we need a dictionary / map that can hold the words and their number of occurrence
- to do so, the text from each line has to be split to individual words
- iterate thtrough the list of words.
- check if the word is already in the dictionary
- if yes: increment the value of this specific word
- if no: add the word to the dictionary and set its value to 1
- after having read all content from the file and process it as mentioned above, our dictionary contains all words as keys and the numbers as values
- close the file