Regex 在MATLAB中检索具有查询文本的文本文件

Regex 在MATLAB中检索具有查询文本的文本文件,regex,matlab,Regex,Matlab,我有一套100个文本文件。我给出了一个查询“一个胖子正在水里游泳”,现在输出应该是包含“胖子”、“男人”或“游泳”或其中两个的文本文件的名称,或者匹配时不应考虑所有三个单词stopwords。以及每个文本文件中匹配的单词数 Matlab中的帮助。代码 谢谢,先生,但是outcell的计数值总是显示为0,chkpatt1和CHKPATT2也显示为零,这意味着找不到匹配的单词,但我已将key1和key2替换为与文本文件中相同的单词。@user3416063按照回答的最后一部分所述进行测试。我使用了测

我有一套100个文本文件。我给出了一个查询“一个胖子正在水里游泳”,现在输出应该是包含“胖子”、“男人”或“游泳”或其中两个的文本文件的名称,或者匹配时不应考虑所有三个单词stopwords。以及每个文本文件中匹配的单词数

Matlab中的帮助。

代码


谢谢,先生,但是outcell的计数值总是显示为0,chkpatt1和CHKPATT2也显示为零,这意味着找不到匹配的单词,但我已将key1和key2替换为与文本文件中相同的单词。@user3416063按照回答的最后一部分所述进行测试。我使用了测试代码,但是count仍然返回0。你能不能在文本文件中放一行,在男人游泳的时候编辑它,看看什么是counts?你知道这段代码会寻找一个类似于男人游泳的模式,对吗?人和游泳不能用别的词分开。
%%// Source of stopwords- http://norm.al/2009/04/14/list-of-english-stop-words/
stopwords_cellstring={'a', 'about', 'above', 'above', 'across', 'after', ...
    'afterwards', 'again', 'against', 'all', 'almost', 'alone', 'along', ...
    'already', 'also','although','always','am','among', 'amongst', 'amoungst', ...
    'amount',  'an', 'and', 'another', 'any','anyhow','anyone','anything','anyway', ...
    'anywhere', 'are', 'around', 'as',  'at', 'back','be','became', 'because','become',...
    'becomes', 'becoming', 'been', 'before', 'beforehand', 'behind', 'being', 'below',...
    'beside', 'besides', 'between', 'beyond', 'bill', 'both', 'bottom','but', 'by',...
    'call', 'can', 'cannot', 'cant', 'co', 'con', 'could', 'couldnt', 'cry', 'de',...
    'describe', 'detail', 'do', 'done', 'down', 'due', 'during', 'each', 'eg', 'eight',...
    'either', 'eleven','else', 'elsewhere', 'empty', 'enough', 'etc', 'even', 'ever', ...
    'every', 'everyone', 'everything', 'everywhere', 'except', 'few', 'fifteen', 'fify',...
    'fill', 'find', 'fire', 'first', 'five', 'for', 'former', 'formerly', 'forty', 'found',...
    'four', 'from', 'front', 'full', 'further', 'get', 'give', 'go', 'had', 'has', 'hasnt',...
    'have', 'he', 'hence', 'her', 'here', 'hereafter', 'hereby', 'herein', 'hereupon', ...
    'hers', 'herself', 'him', 'himself', 'his', 'how', 'however', 'hundred', 'ie', 'if',...
    'in', 'inc', 'indeed', 'interest', 'into', 'is', 'it', 'its', 'itself', 'keep', 'last',...
    'latter', 'latterly', 'least', 'less', 'ltd', 'made', 'many', 'may', 'me', 'meanwhile',...
    'might', 'mill', 'mine', 'more', 'moreover', 'most', 'mostly', 'move', 'much', 'must',...
    'my', 'myself', 'name', 'namely', 'neither', 'never', 'nevertheless', 'next', 'nine',...
    'no', 'nobody', 'none', 'noone', 'nor', 'not', 'nothing', 'now', 'nowhere', 'of', 'off',...
    'often', 'on', 'once', 'one', 'only', 'onto', 'or', 'other', 'others', 'otherwise',...
    'our', 'ours', 'ourselves', 'out', 'over', 'own','part', 'per', 'perhaps', 'please',...
    'put', 'rather', 're', 'same', 'see', 'seem', 'seemed', 'seeming', 'seems', 'serious',...
    'several', 'she', 'should', 'show', 'side', 'since', 'sincere', 'six', 'sixty', 'so',...
    'some', 'somehow', 'someone', 'something', 'sometime', 'sometimes', 'somewhere', ...
    'still', 'such', 'system', 'take', 'ten', 'than', 'that', 'the', 'their', 'them',...
    'themselves', 'then', 'thence', 'there', 'thereafter', 'thereby', 'therefore', ...
    'therein', 'thereupon', 'these', 'they', 'thickv', 'thin', 'third', 'this', 'those',...
    'though', 'three', 'through', 'throughout', 'thru', 'thus', 'to', 'together', 'too',...
    'top', 'toward', 'towards', 'twelve', 'twenty', 'two', 'un', 'under', 'until', 'up',...
    'upon', 'us', 'very', 'via', 'was', 'we', 'well', 'were', 'what', 'whatever', 'when',...
    'whence', 'whenever', 'where', 'whereafter', 'whereas', 'whereby', 'wherein',...
    'whereupon', 'wherever', 'whether', 'which', 'while', 'whither', 'who', 'whoever',...
    'whole', 'whom', 'whose', 'why', 'will', 'with', 'within', 'without', 'would', 'yet',...
    'you', 'your', 'yours', 'yourself', 'yourselves', 'the'};

%%// Keywords
key1 = 'man';
key2 = 'swimming';

%%// Read in all TXT files
txtfiles = dir('*.txt');

%%// Go through all files
for k0 = 1:numel(txtfiles)

    %%// Read in strings from one text file at a time
    fid = fopen(txtfiles(k0).name);
    textdata = textscan(fid, '%s','delimiter', '\n');
    fclose(fid);

    %%// Get the count of strings to be matched
    count1 = 0;
    for k = 1:numel(textdata{1})

        %%// Read line and split into words
        split1 = regexp(cell2mat(textdata{1}(k)),'\s','Split');

        %%// If number of words are less than two, move onto the next line
        if numel(split1)<2
            continue;
        end

        %%// Remove the stopwords
        split1 = split1(~ismember(split1,stopwords_cellstring')); %%//'

        %%// Get the count of consecutive appearance of keywords
        chkpatt1 = ismember(split1,key1);
        chkpatt2 = ismember(split1,key2);
        count1 = count1 + nnz(sum([0 chkpatt1;chkpatt2 0],1)==2);

    end

    %%// Store filenames and counts as a cell array
    outcell(k0,1) = {txtfiles(k0).name};
    outcell(k0,2) = num2cell(count1);

end
%%// Testing : Keep only one TXT file, let us name it as 'check1.txt' inside the 
%%// working directory and make sure that this has the keywords somewhere.
txtfiles = dir('*.txt');
if numel(txtfiles)~=1
    error('You do not have exactly one TXT file');
end
filename = cell2mat(outcell(1,1))
counts = cell2mat(outcell(1,2))