【问题标题】:Remove stop words from sentence从句子中删除停用词
【发布时间】:2018-01-15 13:57:31
【问题描述】:

我有一个句子,但是这个句子被分割成每个空格。

我的数据输出是这样的

const escapeRE = new RegExp(/([/\?""])/g);
const myDatas = data.map(des => des.Sentence.toLowerCase().replace(escapeRE, '').split(' '));

[ [ 'yes',
'keep',
'go',
'apple',
'tabacco',
'javascript',
'no',
'uhh',
'omg',
'hi.' ],
['say',
'hello',
'me',
'allright',
'maybe',
'mi',
'say.' 
....] ]

比我有一个停用词JSON 文件。

停用词JSON文件的内容

['yes',
'hi',
'so',
'say',
'me',
'uhh',
'omg',
'go',
'hello',
'hi' 
 ...]

所以我想从数组句子中删除停用词。 我想要纯句子,没有停用词。 stopwords定义;

const stopwords = require('./stop_words.json');

那我该怎么办?我没有任何想法。我尝试了myDatas.replace('stopwords', '' ) 函数,但它没用

【问题讨论】:

    标签: javascript json regex reactjs stop-words


    【解决方案1】:

    您可以像这样使用 Array 原型:

    Array.prototype.diff = function(stopwords) {
        return this.filter(function(word) {
            var punctuationlessWord = word.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g, "");
            return stopwords.indexOf(punctuationlessWord) < 0;
        });
    };
    

    及用法:

    myDatas.forEach(function(part, index, theArray) {
      theArray[index] = theArray[index].diff( stopwords );
    });
    

    var myDatas = [ [ 'yes',
    'keep',
    'go',
    'apple',
    'tabacco',
    'javascript',
    'no',
    'uhh',
    'omg',
    'hi.' ],
    ['say',
    'hello',
    'me',
    'allright',
    'maybe',
    'mi',
    'say.'] ];
    
    var stopwords = ['yes',
    'hi',
    'so',
    'say',
    'me',
    'uhh',
    'omg',
    'go',
    'hello',
    'hi'];
    
    Array.prototype.diff = function(stopwords) {
        return this.filter(function(word) {
    		var punctuationlessWord = word.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"");
    		return stopwords.indexOf(punctuationlessWord) < 0;
    	});
    };
    
    myDatas.forEach(function(part, index, theArray) {
      theArray[index] = theArray[index].diff( stopwords );
    });
    
    console.log(myDatas);

    【讨论】:

    • 谢谢。我解决了你的案子。这是 ES6 解决方案。我想对我来说这个问题的最佳实践myDatas.map(des =&gt; des.filter(word =&gt; stopWords.indexOf(word) &lt; 0));
    • 如果支持 ES6 就可以了。
    【解决方案2】:

    您可以使用 jQuery grep 函数来实现您的目标。你可以像下面这样使用。

        var withoutStopWords = jQuery.grep(myDatas, function(element, index){
      return stopwords.indexOf(element)<0;
                      });
    

    Javascript 示例

    var filtered=myDatas.filter(function(e){return this.indexOf(e)<0;},stopwords);
    

    【讨论】:

    • 但我只使用 react es6。
    • 可能像下面的 ES6 语法:let filtered = myDatas.filter(e =&gt; this.indexOf(e) &lt; 0, stopwords);
    【解决方案3】:

    我想到的第一个大腿是,您可以创建递归函数,该函数将遍历句子数组并检查句子单词是否在 stopWords 数组中,如下所示:

    function removeStopWords(sentenceArray, stopWords, result = []) {
        sentenceArray.forEach((sentence) => {
            if (Array.isArray(sentence)) {
                result = removeStopWords(sentence, stopWords, result);
            } else if (!stopWords.includes(sentence)) {
                result = result.concat(sentence)
            }
        });
    
        return result;
    }
    

    【讨论】:

      【解决方案4】:

      这是 ES6 孤岛

        myDatas.map(des => des.filter(word => stopWords.indexOf(word) < 0));
      

      【讨论】:

        猜你喜欢
        • 2019-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-27
        • 2021-08-20
        • 2013-05-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多