Extract only words from an array of cells in matlab

I have a set of documents containing pre-processed texts from html pages. I have already been given. I want to extract only words from it. I do not want any numbers or common words or any single letters to be extracted. The first problem I encountered is this.

Suppose I have an array of cells:

{'!' '!!' '!!!!)'  '!!!!thanks' '!!dogsbreath'    '!)'    '!--[endif]--'    '!--[if'}

I want the cell array to have only words - like this.

{'!!!!thanks' '!!dogsbreath' '!--[endif]--' '!--[if'}

And then convert it to this array of cells

{'thanks' 'dogsbreath' 'endif' 'if'}

Is there any way to do this?


Updated requirement . Thanks to all your answers. However, I ran into a problem! Let me illustrate this (note that cell values ​​are extracted from HTML documents and therefore may contain non-ASCII values) -

{'!/bin/bash'    '![endif]'    '!take-a-long'    '!â€"photo'}

It gives me an answer

{'bin'    'bash'    'endif'    'take'    'a'    'long'    'â'    'photo' }

My questions:

  • bin/ bash take-a-long ? , ? . , , , .
  • , '!â€"photo' non-ascii â, a. , ?
  • , "it? __________ About the Author:" "__________" . ?
  • "2. areoplane 3. cactus 4. a_rinny_boo... 5. trumpet 6. window 7. curtain ... 173. gypsy_wagon..." 'areoplane' 'cactus' 'a_rinny_boo' 'trumpet' 'window' 'curtain' 'gypsy_wagon'. , 'a_rinny_boo' ''gypsy_wagon 'a' 'rinny' 'boo' 'gypsy' 'wagon'. ?

1. , , , .

function [Text_Data] = raw_txt_gn(filename)

% This function will convert the text documnets into raw text
% It will remove all commas empty cells and other special characters
% It will also convert all the words of the text documents into lowercase

T = textread(filename, '%s');

% find all the important indices
ind1=find(ismember(T,':WebpageTitle:'));
T1 = T(ind1+1:end,1);

% Remove things which are not basically words
not_words = {'##','-',':ImageSurroundingText:',':WebpageDescription:',':WebpageKeywords:',' '};

T2 = []; count = 1;
for j=1:length(T1)    
    x = T1{j};
    ind=find(ismember(not_words,x), 1);
    if isempty(ind)

        B = regexp(x, '\w*', 'match');
        B(cellfun('isempty', B)) = []; % Clean out empty cells
        B = [B{:}]; % Flatten cell array

        % convert the string into lowecase
        % so that while generating the features the case sensitivity is
        % handled well
        x = lower(B);        

        T2{count,1} = x;
        count = count+1;
    end
end
T2 = T2(~cellfun('isempty',T2));


% Getting the common words in the english language
% found from Wikipedia
not_words2 = {'the','be','to','of','and','a','in','that','have','i'};
not_words2 = [not_words2, 'it' 'for' 'not' 'on' 'with' 'he' 'as' 'you' 'do' 'at'];
not_words2 = [not_words2, 'this' 'but' 'his' 'by' 'from' 'they' 'we' 'say' 'her' 'she'];
not_words2 = [not_words2, 'or' 'an' 'will' 'my' 'one' 'all' 'would' 'there' 'their' 'what'];
not_words2 = [not_words2, 'so' 'up' 'out' 'if' 'about' 'who' 'get' 'which' 'go' 'me'];
not_words2 = [not_words2, 'when' 'make' 'can' 'like' 'time' 'no' 'just' 'him' 'know' 'take'];
not_words2 = [not_words2, 'people' 'into' 'year' 'your' 'good' 'some' 'could' 'them' 'see' 'other'];
not_words2 = [not_words2, 'than' 'then' 'now' 'look' 'only' 'come' 'its' 'over' 'think' 'also'];
not_words2 = [not_words2, 'back' 'after' 'use' 'two' 'how' 'our' 'work' 'first' 'well' 'way'];
not_words2 = [not_words2, 'even' 'new' 'want' 'because' 'any' 'these' 'give' 'day' 'most' 'us'];

for j=1:length(T2)
    x = T2{j};
    % if a particular cell contains only numbers then make it empty
    if sum(isstrprop(x, 'digit'))~=0
        T2{j} = [];
    end
    % also remove single character cells
    if length(x)==1
        T2{j} = [];
    end
    % also remove the most common words from the dictionary
    % the common words are taken from the english dicitonary (source
    % wikipedia)
    ind=find(ismember(not_words2,x), 1);
    if isempty(ind)==0
        T2{j} = [];
    end
end

Text_Data = T2(~cellfun('isempty',T2));

2 , , -ascii. Matlab

% remove the non-ascii characters
if all(x  < 128)
else
  T2{j} = [];
end

, , -ascii, .


? '_' '-'.

+4
2

A regexp, :

A = {'!' '!!' '!!!!)'  '!!!!thanks' '!!dogsbreath'    '!)'    '!--[endif]--'    '!--[if'};

B = regexp(A, '\w*', 'match');
B(cellfun('isempty', B)) = []; % Clean out empty cells
B = [B{:}]; % Flatten cell array

, . 1x4:

B = 

    'thanks'    'dogsbreath'    'endif'    'if'

Edit:

bin/ bash take-a-long ? , ? . , , , .

. B = [B{:}];, , . , .

, "!" " -ascii", a. , ?

, .

, "?" : " " __________ " . ?

, , . , _, : B = regexp(A, '[a-zA-Z0-9]*', 'match'); a-z, a-z 0-9. , ASCII, , -, \w*.

+5

, @excaza , isstrprop, 'alpha' -

A(cellfun(@(x) any(isstrprop(x, 'alpha')), A))

-

>> A
A = 
    '!'    '!!'    '!!!!)'    '!!!!thanks'    '!!dogsbreath'    '!)'    '!--[endif]--'    '!--[if'
>> A(cellfun(@(x) any(isstrprop(x, 'alpha')), A))
ans = 
    '!!!!thanks'    '!!dogsbreath'    '!--[endif]--'    '!--[if'

, , :

B = cellfun(@(x) x(isstrprop(x, 'alpha')), A,'Uni',0);
out = B(~cellfun('isempty',B))

-

A = 
    '!'    '!!'    '!!!!)'    '!!!!thanks'    '!!dogsbreath'    '!)'    '!--[endif]--'    '!--[if'
out = 
    'thanks'    'dogsbreath'    'endif'    'if'
+4

Source: https://habr.com/ru/post/1616608/


All Articles