【发布时间】:2019-07-18 19:28:02
【问题描述】:
我正在使用 Google 的自然语言 analyzeEntities api,在响应中,有一个嵌套的 EntityMention.TextSpan 对象,有 2 个字段:内容和 beginOffset。
我想利用 beginOffset 进行进一步分析。所以我试图映射原始文本中的单词索引并将它们与 beginOffset 进行比较,但我注意到索引不同。
我正在使用一种相当幼稚的方法来构建这个索引:
const msg = "it will cost you $350 - $600,. test. Alexander. How are you?"
let index = 0
msg.split(" ").forEach(part => {
console.log(part + ":" + index)
index = index + part.length + 1 // + 1 for the split on space
})
结果是:
it:0
will:3
cost:8
you:13
$350:17
-:22
$600,.:24
test.:31
Alexander.:37
How:48
are:52
you?:56
我从 analyzeEntities api 得到的结果是:
gcloud ml language analyze-entities --content="it will cost you $350 - $600,. test. Alexander. How are you?"
{
"entities": [
{
"mentions": [
{
"text": {
"beginOffset": 23,
"content": "test"
},
"type": "COMMON"
}
],
"metadata": {},
"name": "test",
"salience": 0.7828024,
"type": "OTHER"
},
{
"mentions": [
{
"text": {
"beginOffset": 29,
"content": "Alexander"
},
"type": "PROPER"
}
],
"metadata": {},
"name": "Alexander",
"salience": 0.2171976,
"type": "PERSON"
}
],
"language": "en"
}
我了解非字母数字字符具有特殊含义和处理方式,我希望偏移量代表真正的索引。
既然,解析查询文本的规则是什么,beginOffset是如何计算的?
谢谢!
【问题讨论】: