我有一组包含来自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字符的一部分文本完全消失。
我的最终要求可以完成吗?他们大多数涉及字符'_'
和'-'
。
不错我刚刚了解到这个功能谢谢! –
整洁的功能:)感谢分享! – rayryeng
你可以看看我的更新要求吗? – roni