2015-11-19 135 views
4

我有一组包含来自html页面的预处理文本的文档。他们已经交给我了。我只想从中提取单词。我不想要提取任何数字或常用词或任何单个字母。我面临的第一个问题是这个。从matlab中提取单元格数组中的单词

假设我有一个单元阵列:

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

我要让只有单词的单元阵列 - 这样。

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

然后将它转换为这个单元阵列

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

有没有办法做到这一点?


更新要求:感谢所有的答案的。但是我面临一个问题!让我来说明这一点(请注意,单元格值从HTML文档中提取文本,因此可能包含非ASCII值) -

{'!/bin/bash' '![endif]' '!take-a-long' '!–photo'} 

这给了我答案

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

我的问题:

  • 为什么bin/bash和take-a-long被分成三个单元格?它对我来说不是问题,但仍然是为什么?这可以避免。我的意思是来自一个单元格的所有单词被合并为一个单元格。
  • 请注意,在'!–photo'中存在非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 我发现这个代码here,告诉我如何检查非ASCII字符。结合在Matlab此代码段为

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

然后除去空单元格看来我的第二个要求是满足,虽然含有非ASCII字符的一部分文本完全消失。


我的最终要求可以完成吗?他们大多数涉及字符'_''-'

回答

4

我觉得@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' 
+2

不错我刚刚了解到这个功能谢谢! –

+0

整洁的功能:)感谢分享! – rayryeng

+0

你可以看看我的更新要求吗? – roni

5

一个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' 

编辑:

为什么斌/ bash和走,一长被分成三个单元格?它对我来说不是问题,但仍然是为什么?这可以避免。我的意思是来自一个单元格的所有单词被合并为一个单元格。

因为我扁平单元格数组来移除嵌套单元格。如果删除B = [B{:}];,则每个单元格都将有一个嵌套单元格,其中包含输入单元格阵列的所有匹配项。无论你想要什么,你都可以结合这些。

请注意,在'!'照片'中存在一个非ascii字符â本质上意味着一个。可以合并一个步骤,使这种转换是自动的吗?

是的,你必须根据字符代码。

我注意到文字“it?__________关于作者:”给我“__________”作为单词。这是为什么?

正如我所说的,正则表达式匹配字母,数字或下划线字符。您可以更改过滤器以排除_,这也将解决第四个要点:B = regexp(A, '[a-zA-Z0-9]*', 'match');这仅匹配a-z,A-Z0-9。这也将排除非ASCII字符,它看起来像\w*标志匹配。

+0

我更喜欢这种方法。它对我来说更自然。 – rayryeng

+0

你可以看看我的更新要求吗? – roni

+0

@roni我已经更新了我的回答 – excaza