【问题标题】:Retrieving text file having the query text in MATLAB在 MATLAB 中检索具有查询文本的文本文件
【发布时间】:2014-03-24 22:11:30
【问题描述】:

我有一组 100 个文本文件。我给出查询“一个胖子在水中游泳”,现在输出应该只是包含“fat”、“man”或“swimming”或其中两个或所有三个词的文本文件的名称(停用词不应该匹配时考虑)。还有每个文本文件中匹配了多少个单词。

在 Matlab 中的帮助。

【问题讨论】:

    标签: regex matlab


    【解决方案1】:

    代码

    %%// 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))
    

    【讨论】:

    • 谢谢先生,但 outcell 的计数值始终显示 0,chkpatt1 和 2 也显示为零,表示未找到匹配的单词,但我已将 key1 和 key2 替换为相同的文本文件中的单词。
    • @user3416063 按照答案最后部分中的说明进行测试。
    • 我使用了测试代码,但仍然count返回0。
    • 你能在文本文件中放一行,然后编辑为“那个人在游泳”,看看counts是什么?
    • 您知道这段代码会寻找像man somestopword swimming 这样的模式,对吧? manswimming 不能用停用词以外的任何内容分隔。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    • 2012-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多