Counting number of words and their characters

조회 수: 5 (최근 30일)
Joe Ainsworth
Joe Ainsworth 2021년 9월 3일
댓글: Stephen23 2021년 10월 24일
oo
  댓글 수: 1
Stephen23
Stephen23 2021년 10월 24일
Original question by Joe Ainsworth on 3rd Septempber 2021 retrieved from Google Cache:
Counting number of words and their characters
Hi all,
currenlty wirting a script which reads a text file, splits the lines into the individual words and stores the word lengths in an array.
i beleive this may be achieveable with a bag of words but it may not be neccessary or the simpliest way to do so.
print the total number of words, the number of characters within the words but dosesnt count spaces but does incude punctuation and the lengths of the individual words.
Needs to be outputted like so.
This file has zz words with bb characters
* Word x has yy characters
any insight is much appreciated

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

답변 (1개)

Ive J
Ive J 2021년 9월 3일
Maybe this help
txt = ["Why MATLAB doesn't offer much for data science?"; "Python and R beat MATLAB in ML"];
voc = tokenizedDocument(txt).Vocabulary;
len = arrayfun(@(x)numel(x{:}), voc);
% print document stat
wordn = voc + " has " + len + " characters";
fprintf('file txt has %d words with %d characters:\n', numel(voc), sum(len))
file txt has 15 words with 58 characters:
fprintf('\t%s\n', wordn.')
Why has 3 characters MATLAB has 6 characters doesn't has 7 characters offer has 5 characters much has 4 characters for has 3 characters data has 4 characters science has 7 characters ? has 1 characters Python has 6 characters and has 3 characters R has 1 characters beat has 4 characters in has 2 characters ML has 2 characters
  댓글 수: 8
Joe Ainsworth
Joe Ainsworth 2021년 9월 5일
using matlab2021a, error message
tokenizedDocument' requires Text Analytics Toolbox.
Error in Word_lengths (line 5)
voc = tokenizedDocument(inputfile).Vocabulary;
Ive J
Ive J 2021년 9월 5일
편집: Ive J 2021년 9월 5일
Well, seems you haven't Text Analytics Toolbox.
contains(struct2array(ver), 'Text Analytics Toolbox') % true is installed
ans = logical
1
In that case you can use a mixture of regexp and split to get the words. The challenge lies in punctuations. Consider my first example again:
txt = ["Why MATLAB (R2021) doesn't offer much for data science?!"; "Python and R, both beat MATLAB in ML."];
txt = join(txt, newline);
% replace the above lines with the following when reading from text files
% txt = string(fileread('myfile.txt'));
voc = unique(split(txt)); % we keep unique words, so each word is counted only once through the document.
% find punctuations
patt = regexpPattern('[^\w\s]');
punc = extract(txt, patt);
punc(punc == "'") = []; % keep apostrophes intact
voc = replace(voc, punc, ''); % remove punctuations from voc
voc = [voc; punc]; % merge all together
len = arrayfun(@(x)numel(x{:}), voc);
% print document stat
wordn = voc + " has " + len + " characters";
fprintf('file txt has %d words with %d characters:\n', numel(voc), sum(len))
file txt has 22 words with 72 characters:
fprintf('\t%s\n', wordn.')
R2021 has 5 characters MATLAB has 6 characters ML has 2 characters Python has 6 characters R has 1 characters Why has 3 characters and has 3 characters beat has 4 characters both has 4 characters data has 4 characters doesn't has 7 characters for has 3 characters in has 2 characters much has 4 characters offer has 5 characters science has 7 characters ( has 1 characters ) has 1 characters ? has 1 characters ! has 1 characters , has 1 characters . has 1 characters

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

카테고리

Help CenterFile Exchange에서 Networks에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by