gpt4 book ai didi

javascript - Mongodb 上的小 NLP 请求

转载 作者:行者123 更新时间:2023-11-29 23:07:07 24 4
gpt4 key购买 nike

我们试图找出推文是正面的还是负面的我们的数据库中有两个集合:第一个集合重新组合了一个单词列表,以及它们是正面的还是负面的第二个是推文列表

我们的要求是:

t.aggregate([{$project:{wordt:{$split:["$text"," "]}}},
{$lookup:{from:"infi",localField:"wordt",foreignField:"word",as:"test_word"}},
{$project:
{tpositif:
{$cond:[{$eq:["$test_word.polarity","positive"]},1,0]},

tnegatif:
{$cond:[{$eq:["$test_word.polarity","negative"]},1,0]}}},

{$group:{
_id:"$_id",
count_pos:{$sum:"$tpositif"},
count_neg:{$sum:"$tnegatif"}
}])

t 是推文集合,inf 是单词集合。

我们无法理解为什么它总是计数为 0。

感谢您的建议。

最佳答案

您正在测试 "$test_word.polarity"test_word 是一个数组。

您可以通过展开查找来解决这个问题,它将“连接”的行拆分为它们自己的顶级行。这对您来说应该不是问题,因为每个词您应该只有一个情感记录(否则您会得到重复的)。

t.aggregate([{$project:{wordt:{$split:["$text"," "]}}},
{$lookup:
{from:"infi",localField:"wordt",foreignField:"word",as:"test_word"}},
{$unwind:"$test_word"},
{$project:
{tpositif:
{$cond:[{$eq:["$test_word.polarity","positive"]},1,0]},

tnegatif:
{$cond:[{$eq:["$test_word.polarity","negative"]},1,0]}}},

{$group:{
_id:"$_id",
count_pos:{$sum:"$tpositif"},
count_neg:{$sum:"$tnegatif"}
}])

诊断聚合查询的一个好方法是将管道切回到开始子句,并查看中间文档集合是否符合您的预期。然后将子句一一添加回去。

例如将其缩减为两个子句揭示了问题所在:

> db.tweets.aggregate([ {$project:{wordt:{$split:["$text"," "]}}}, {$lookup:{from:"infi",localField:"wordt",foreignField:"word",as:"test_word"}},  ]);
{ "_id" : ObjectId("5c59442c365f7243b44062f8"), "wordt" : [ "test", "1" ], "test_word" : [ { "_id" : ObjectId("5c594473365f7243b44062f9"), "word" : "test", "polarity" : "negative" } ] }
{ "_id" : ObjectId("5c59463fd56fd34fcc370c74"), "wordt" : [ "the", "infinite", "fool" ], "test_word" : [ { "_id" : ObjectId("5c594625d56fd34fcc370c73"), "word" : "fool", "polarity" : "positive" } ] }
{ "_id" : ObjectId("5c594657d56fd34fcc370c75"), "wordt" : [ "test", "the", "infinite", "fool" ], "test_word" : [ { "_id" : ObjectId("5c594473365f7243b44062f9"), "word" : "test", "polarity" : "negative" }, { "_id" : ObjectId("5c594625d56fd34fcc370c73"), "word" : "fool", "polarity" : "positive" } ] }

你可以在这里看到 "test_word": [ { "_id"... 是一个数组,由方括号组成。所以 polarity 属性在数组的第一个元素中,而不是在 $test_word 本身下。

顺便说一句。我首先认为您可以取消引用 $eq 中的第一个数组元素,例如 "$test_word[0].polarity" 但它似乎不起作用(我以为我做过一次)。

关于javascript - Mongodb 上的小 NLP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54518093/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com