Text Extraction and retrieval
古いコメントを表示
<P ID=1>
A LITTLE BLACK BIRD.
</P>
<P ID=2>
Story about a bird,
(1811)
</P>
<P ID=3>
Part 1.
</P>
As I am new to text extraction, I need help in;
採用された回答
その他の回答 (2 件)
Cedric
2017 年 10 月 26 日
Here is another approach based on pattern matching:
>> data = regexp(fileread('data.txt'), '(?<=<P[^>]+>\s*)[\w ]+', 'match' )
data =
1×3 cell array
{'A LITTLE BLACK BIRD'} {'Story about a bird'} {'Part 1'}
if you don't need the IDs (e.g. if in any case they will go from 1 to the number of P tags), you are done.
If you needed the IDs, you could get both IDs and content as follows:
>> data = regexp(fileread('data.txt'), '<P ID=(\d+)>\s*([\w ]+)', 'tokens' ) ;
data = vertcat( data{:} ) ;
ids = str2double( data(:,1) )
data = data(:,2)
ids =
1
2
3
data =
3×1 cell array
{'A LITTLE BLACK BIRD'}
{'Story about a bird' }
{'Part 1' }
6 件のコメント
John
2017 年 10 月 28 日
data = regexp( lower( fileread( 'data.txt' )), '(?<=<p[^>]+>\s*)[^<]+', 'match' ) ;
data = regexp( data, '[a-z\-]+', 'match' ) ;
allWords = [data{:}] ;
[allUniqueWords, ~, ic] = unique( allWords ) ;
counts = accumarray( ic, 1 ) ;
After running this, you'll have all words in the allWords cell array (so numel(allWords) is the total number of words), a list of unique words in allUniqueWords (so numel(allUniqueWords) is the number of unique words), and a count of occurrence of unique words in counts.
MATLAB R2017b has a text analytics toolbox that may do this better, but I am not using it. Maybe Akira is and can develop on this. Now I think that your best option is learning the basics and studying well Akira's solution, which is the most natural approach using MATLAB base features. Mine relies more on pattern matching; while it is fairly concise, it will not teach you MATLAB to spend hours understanding regular expressions.
John
2017 年 10 月 31 日
Cedric
2017 年 10 月 31 日
My pleasure!
John
2017 年 11 月 7 日
If you have a count per document, finding the number of documents a keyword is in is easy:
counts = [7, 0 ,3] ;
hasKey = counts > 0 ; % [1,0,1]
nDocs = sum( hasKey ) ; % 2
Christopher Creutzig
2017 年 11 月 2 日
編集済み: Christopher Creutzig
2017 年 11 月 2 日
It's probably easiest to split the text and then check the number of splits created to count, using string functions:
str = extractFileText('file.txt');
paras = split(str,"</P>");
paras(end) = []; % the split left an empty last entry
paras = extractAfter(paras,">") % Drop the "<P ID=n>" from the beginning
Then, numel(paras) will give you the number of </P>.
If you do not have extractFileText, calling string(fileread('file.txt')) should work just fine, too.
In one of the comments, you indicated you also need to count the frequency of words in documents. That is what bagOfWords is for:
tdoc = tokenizedDocument(lower(paras));
bag = bagOfWords(tdoc)
bag =
bagOfWords with 13 words and 3 documents:
a little black bird . …
1 1 1 1 1
1 0 0 1 0
…
2 件のコメント
John
2017 年 11 月 7 日
shilpa patil
2019 年 9 月 23 日
編集済み: shilpa patil
2019 年 9 月 23 日
how to rewrite the above code for a document image
instead of text file
カテゴリ
ヘルプ センター および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
