我使用的是Google的自然语言analyzeEntities
api,响应中有一个嵌套的EntityMention.TextSpan
对象,其中包含2个字段:content和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
我从analyticsEntities 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是如何计算的?
谢谢!
答案 0 :(得分:0)
您可以控制请求中的编码(用于计算偏移量)。 (encodingType:https://cloud.google.com/natural-language/docs/analyzing-entities#language-entities-string-protocol)。 对于python,您需要将其设置为UTF32(https://cloud.google.com/natural-language/docs/reference/rest/v1/EncodingType)。 gcloud使用的是UTF-8编码,基本上可以为您提供字节级的偏移量。
答案 1 :(得分:0)
看起来$
是这里的问题。
gcloud ml language analyze-entities --content="it will cost you \$350 - \$600,. test. Alexander. How are you?"
{
"entities": [
{
"mentions": [
{
"text": {
"beginOffset": 31,
"content": "test"
},
"type": "COMMON"
}
],
"metadata": {},
"name": "test",
"salience": 0.7828024,
"type": "OTHER"
},
{
"mentions": [
{
"text": {
"beginOffset": 37,
"content": "Alexander"
},
"type": "PROPER"
}
],
"metadata": {},
"name": "Alexander",
"salience": 0.2171976,
"type": "PERSON"
},
{
"mentions": [
{
"text": {
"beginOffset": 17,
"content": "$350"
},
"type": "TYPE_UNKNOWN"
}
],
"metadata": {
"currency": "USD",
"value": "350.000000"
},
"name": "$350",
"salience": 0.0,
"type": "PRICE"
},
{
"mentions": [
{
"text": {
"beginOffset": 24,
"content": "$600"
},
"type": "TYPE_UNKNOWN"
}
],
"metadata": {
"currency": "USD",
"value": "600.000000"
},
"name": "$600",
"salience": 0.0,
"type": "PRICE"
},
{
"mentions": [
{
"text": {
"beginOffset": 18,
"content": "350"
},
"type": "TYPE_UNKNOWN"
}
],
"metadata": {
"value": "350"
},
"name": "350",
"salience": 0.0,
"type": "NUMBER"
},
{
"mentions": [
{
"text": {
"beginOffset": 25,
"content": "600"
},
"type": "TYPE_UNKNOWN"
}
],
"metadata": {
"value": "600"
},
"name": "600",
"salience": 0.0,
"type": "NUMBER"
}
],
"language": "en"
}
如果将$
的符号更改为#
,它似乎可以正常工作。
gcloud ml language analyze-entities --content="it will cost you #350 - #600,. test. Alexander. How are you?"
{
"entities": [
{
"mentions": [
{
"text": {
"beginOffset": 31,
"content": "test"
},
"type": "COMMON"
}
],
"metadata": {},
"name": "test",
"salience": 0.9085014,
"type": "OTHER"
},
{
"mentions": [
{
"text": {
"beginOffset": 37,
"content": "Alexander"
},
"type": "PROPER"
}
],
"metadata": {},
"name": "Alexander",
"salience": 0.09149864,
"type": "PERSON"
},
{
"mentions": [
{
"text": {
"beginOffset": 18,
"content": "350"
},
"type": "TYPE_UNKNOWN"
}
],
"metadata": {
"value": "350"
},
"name": "350",
"salience": 0.0,
"type": "NUMBER"
},
{
"mentions": [
{
"text": {
"beginOffset": 25,
"content": "600"
},
"type": "TYPE_UNKNOWN"
}
],
"metadata": {
"value": "600"
},
"name": "600",
"salience": 0.0,
"type": "NUMBER"
}
],
"language": "en"
}