Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在拆分单词时获取单词的相对起始索引和结束索引_Javascript_Jquery_Arrays_Json_String - Fatal编程技术网

Javascript 在拆分单词时获取单词的相对起始索引和结束索引

Javascript 在拆分单词时获取单词的相对起始索引和结束索引,javascript,jquery,arrays,json,string,Javascript,Jquery,Arrays,Json,String,我写了一个函数,它根据空格分割一个单词,并保留标点符号 function tokenizeUtterance( utterance ) { let spilittedUserText = utterance.toString().match( /[\w-']+|[^\w\s]+/g ); console.log(spilittedUserText); } 假设我有一个字符串,比如:“你好,世界” 我想以以下格式提取JSON对象中拆分单词的相对起始位置和结束位置 +------

我写了一个函数,它根据空格分割一个单词,并保留标点符号

function tokenizeUtterance( utterance )
{
  let spilittedUserText = utterance.toString().match( /[\w-']+|[^\w\s]+/g ); 
  console.log(spilittedUserText);   
} 
假设我有一个字符串,比如:“你好,世界”

我想以以下格式提取JSON对象中拆分单词的相对起始位置和结束位置

+-------+-------+-----+
| word  | start | end |
+-------+-------+-----+
| HELLO |     0 |   4 |
| ,     |     5 |   6 |
| WORLD |     7 |  11 |
+-------+-------+-----+ 

一种相对简单的方法是,假设输出保持输入字符串的顺序,只需计算每个输出字符串上的字符数或位置:

function tokenizeUtterance(utterance) {
  return utterance.toString().match( /[\w-']+|[^\w\s]+/g );  
}

function getStartAndEnd(tokenizedUtterance) {
    let counter = 0;
    const result = [];
    for (const word of tokenizedUtterance) {
        const res = {
            word,
            start: counter,
            end: counter + word.length - 1
        };
        counter += word.length;
        result.push(res);
    }
    return result;
}
这就是你得到的回报:

[
    {"word":"HELLO","start":0,"end":4},
    {"word":",","start":5,"end":5},
    {"word":"WORLD","start":6,"end":10}
]

下面是使用
indexOf
为每个单词创建索引的方法:

function getResult(utterance){
  let spilittedUserText = utterance.toString().match( /[\w-']+|[^\w\s]+/g ); 
  let result = [];
  let currenSearchIndex = 0;
  for (var i = 0; i < spilittedUserText.length; i++){
    let startIndex = utterance.indexOf(spilittedUserText[i], currenSearchIndex);
    currenSearchIndex = startIndex;
    let resultItem = {
        word: spilittedUserText[i],
        start: startIndex,
        end: startIndex + spilittedUserText[i].length - 1
    }
    result.push(resultItem);
  }
  return result;
}
console.log(JSON.stringify(getResult('Hello, world Hello')));

您只需:

  • 字符串
    拆分为
    单词
    索引
    对的数组
  • 用于返回每个
    单词的自定义数据
这是您的代码应该如何编写的:

function tokenizeUtterance(utterance) {
  let spilittedUserText = [];

  utterance.toString().replace(/[\w-']+|[^\w\s]+/g, function(s, i) {
    spilittedUserText.push({
      word: s,
      index: i
    });
  });
  return spilittedUserText.map(function(w) {
    return {
      "word": w.word,
      "start": w.index,
      "end": w.index + w.word.length - 1
    };
  });
}
演示:

function-tokenizeutrance(话语){
设spilittedUserText=[];
toString().replace(/[\w-']+|[^\w\s]+/g,函数(s,i){
spilitedUserText.push({
单词:s,
索引:i
});
});
返回spilittedUserText.map(函数(w){
返回{
“单词”:w.word,
“开始”:w.index,
“结束”:w.索引+w.字长-1
};
});
}
var string=“你好,世界”;

log(tokenizeutrance(字符串))
字符串。replace
提供匹配及其偏移量:

str=“你好,世界粮食组织你好”;
结果=[];
str.replace(/[\w'-]+|[^\w\s]+/g,(字,偏移量)=>
结果.推送([字,偏移量,偏移量+字.长度]);

控制台日志(结果)堆栈溢出不是免费的代码编写服务,请说明您的代码/努力以及实际问题是什么。很明显,这会因为重复单词而失败。这会失败,因为同一个单词多次出现。@KunalMukherjee我更新了答案,请检查它。
function tokenizeUtterance(utterance) {
  let spilittedUserText = [];

  utterance.toString().replace(/[\w-']+|[^\w\s]+/g, function(s, i) {
    spilittedUserText.push({
      word: s,
      index: i
    });
  });
  return spilittedUserText.map(function(w) {
    return {
      "word": w.word,
      "start": w.index,
      "end": w.index + w.word.length - 1
    };
  });
}