【问题标题】:Tokenize in JavaScript在 JavaScript 中标记化
【发布时间】:2015-08-24 17:47:28
【问题描述】:

如果我有一个字符串,我如何将它拆分成一个单词数组并过滤掉一些停用词?我只想要长度为 2 或更大的单词。

如果我的字符串是

var text = "This is a short text about StackOverflow.";

我可以分开

var words = text.split(/\W+/);

但是使用split(/\W+/),我得到了所有的单词。我可以检查单词的长度是否至少为 2

function validate(token) {
  return /\w{2,}/.test(token);
}

但我想我可以使用正则表达式更智能/更快地做到这一点。

我还有一个数组 var stopwords = ['has', 'have', ...] 不应该出现在数组中。

实际上,如果我能找到过滤掉停用词的方法,我可以将所有字母 a、b、c、...、z 添加到停用词数组中,以仅接受至少包含 2 个字符的单词。

【问题讨论】:

  • 这可以使用数组和过滤器方法轻松完成,您是否希望使用正则表达式来完成所有这些操作?
  • 我不认为text.split(/\W+/).filter(validate) 有什么问题。无需编写过于复杂的正则表达式。
  • 你可以用text.split(/\W+|\b\w\b/)去掉非单词符号和长度小于1的所有单词。

标签: javascript arrays regex split tokenize


【解决方案1】:

我会按照您开始的操作:按/W+/ 拆分,然后使用.filter() 验证数组中的每个标记(长度和停用词)。

var text = "This is a short text about StackOverflow.";
var stopwords = ['this'];

var words = text.split(/\W+/).filter(function(token) {
    token = token.toLowerCase();
    return token.length >= 2 && stopwords.indexOf(token) == -1;
});

console.log(words); // ["is", "short", "text", "about", "StackOverflow"]

您可以轻松调整正则表达式以查找单词>= 2 字符,但如果您已经需要进行后处理以删除停用词(token.length 将比您编写的任何花哨的正则表达式更快) )。

【讨论】:

    【解决方案2】:

    使用 Ramda 轻松实现:

    var text       = "This is a short text about how StackOverflow has gas.";
    var stopWords  = ['have', 'has'];
    var isLongWord = R.compose(R.gt(R.__, 2), R.length);
    var isGoWord   = R.compose(R.not, R.contains(R.__, stopWords));
    var tokenize   = R.compose(R.filter(isGoWord), R.filter(isLongWord), R.split(' '));
    
    tokenize(text); // ["This", "short", "text", "about", "how", "StackOverflow", "gas."]
    

    http://bit.ly/1V5bVrP

    【讨论】:

      【解决方案3】:

      如果您想使用纯正则表达式方法,如何拆分类似的内容:

      \W+|\b\w{1,2}\b
      

      https://regex101.com/r/rB4cJ4/1

      【讨论】:

        【解决方案4】:

        这样的?

        function filterArray(a, num_words, stop_words) {
            b = [];
            for (var ct = 0; ct <= a.length - 1; ct++) {
                if (!(a[ct] <= num_words) && !ArrayContains[a[ct], stop_words) {
                    b.push(a[ct]);
                }
            }
            return b
        }
        function ArrayContains(word, a) {
            for (var ct = 0; ct <= a.length - 1; ct++) {
                if (word == a[ct]) {
                    return true
                }
                return false
            }
        }
        
        var words = "He walks the dog";
        var stops = ["dog"]
        var a = words.split(" ");
        var f = filterArray(a, 2, stops);
        

        【讨论】:

          【解决方案5】:

          这应该有帮助

          (?:\b\W*\w\W*\b)+|\W+
          

          输出:

          ThisisashorttextaboutStackOverflow. A..Zabc..xyz.

          是匹配字符串。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-01-05
            • 2021-12-02
            • 2010-09-18
            • 1970-01-01
            • 2010-10-30
            • 2016-10-30
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多