【发布时间】:2020-07-24 17:24:31
【问题描述】:
我正在构建一个 firebase 生态系统,但我怀疑它与此无关。 这也是我在这里的第一个问题,我想请与我交流。
所以我有一个名为 articleObject 的对象,其中包含有关产品的信息。
let articleObject = {
title: 'title of a product',
tagline: 'tagline of a product'
}
然后我将 articleObject 发送到 GCP AutoML - 实体提取,将 articleObject 分成两部分并以两种不同的方式处理结果两次,因为其中一种保留了我需要的所有信息,而另一种实际上可用于 Firestore noSQL 数据库因为显然它是一个 s- 我的意思是它旨在支持具有简单查询的平面层次结构。
async function extractEntities(articleObject) {
const projectId = 'suck-it-1454';
const location = 'us-central1';
const modelId = 'TEN657473824614fake';
let content = `${articleObject.title} ${articleObject.tagline}`
console.log(content);
let simpleObject = articleObject;
let complexObject = articleObject;
const client = new PredictionServiceClient();
// Construct request
const request = {
name: client.modelPath(projectId, location, modelId),
payload: {
textSnippet: {
content: content,
mimeType: 'text/plain', // Types: 'test/plain', 'text/html'
},
},
};
const [response] = await client.predict(request);
//simple way
for (const tagPayload of response.payload) {
let entityType = tagPayload.displayName;
let entityText = tagPayload.textExtraction.textSegment.content;
let certainty = tagPayload.textExtraction.score;
let currentCertainty;
try {
currentCertainty = simpleObject[entityType][entityText]
if (currentCertainty > certainty){
continue;
} else {
simpleObject[entityType] = entityText;
}
} catch(err) {
simpleObject[entityType] = entityText;
}
}
//complex way
for (const tagPayload of response.payload) {
let entityType = tagPayload.displayName;
let entityText = tagPayload.textExtraction.textSegment.content;
let certainty = tagPayload.textExtraction.score;
console.log(`entityType: ${entityType}, entityText: ${entityText}, certainty: ${certainty}`);
complexObject[entityType] = {[entityText]:certainty};
console.log('-------------------');
console.log(complexObject[entityType]);
console.log('-------------------');
}
console.log(complexObject);
console.log(simpleObject);
await browser.close();
return [complexObject, simpleObject]
}
好的,所以复杂的方式与它有关,给我一个复杂的结果,其中对象的格式如下:
let complexObject = {
title: 'title of a product',
tagline: 'tagline of a product',
Brand:{"brand":0.9999993},
Manufacturer:{"manufacturer":0.958499993, "possiblemanufacturer2":0.66555444}
Model:{"model":0.93719993}
}
简单的方法给了我
let simpleObject = {
title: 'title of a product',
tagline: 'tagline of a product',
Brand:"brand",
Manufacturer:"manufacturer",
Model:"model"
}
但是 - 最后,在 browser.close() 子句之前的两个 console.log 语句给了我同一个该死的对象(相同)。我的问题是为什么?如果我颠倒 for 循环的顺序,即 //simple 方式和 //complex 方式,结果是相反的。两个结果对象始终相同,但变化的是结果是复杂对象还是简单对象。
我正在使用节点 13,而 Firebase 模拟器正在使用节点 10,如果这很重要的话。 我怀疑它与 firebase 有什么关系。
【问题讨论】:
标签: javascript node.js firebase javascript-objects