我最终想做什么?
检查两个或更多国家/地区的国家/地区关键字的新闻文章JSON,并返回这些文章。
这有点复杂(至少对我而言),所以请耐心等待。
我目前正在从API接收新闻文章JSON。我成功使用Underscore _.map来获取每个对象的描述键值。我将为关键字迭代这些值:
let combinedBing = this.bingWorldJSON.value.concat(this.bingPoliticsJSON.value);
let bingArray = _.map(combinedBing, 'description');
我正在检查那些描述我可能在另一个(动态)数组中的关键字,称为事件。我正在使用.map方法。例如:
let event = ["United States", "Russia"]
let result = event.map(function(word){
return newArray.filter(function(article){
// console.log(article);
return article.toString().indexOf(word) > -1;
});
});
这将返回一个二维数组,如下所示:
[Array(4), Array(0)]
0:Array(4)
0:"No sign probes into Russia, Trump campaign will die down"
1:"Russian Hackers Who Targeted Clinton Appear to Attack France’s Macron"
2:"Senate Russia investigation to add 2 staffers"
3:"Former Trump adviser Flynn likely broke law with Russia trip: lawmakers"
length:4
__proto__:Array(0)
1:Array(0)
length:2
__proto__: Array(0)
但是,我并不想使用事件数组中的单词,因为它非常有限。为什么?因为返回的新闻文章并不总是使用严格的国家名称,但通常具有语义等价物(即" US"而不是"美国",或&# 34;俄罗斯"而不是"俄罗斯")。
因为我的事件'数组是动态创建的(我无法更改该数组中的严格国家/地区名称),我正在将一组硬编码的等效关键字推送到另一个数组(如果该国家/地区存在于' event') 。因此,如果我的事件是'
["United States, "Russia"]
我还将创建一个二维数组,如下所示:
[["United States", "U.S.", "US", "America"], ["Russia", "Russian", "Putin", "Moscow"]]
而不是使用event.map(如上所述),我试图找出如何映射每个数组的关键字(在二维数组中)与我返回的文章。不仅如此,还要返回包含来自两个或更多国家/地区的关键字的文章。例如,如果一篇文章有关键字"美国"和"莫斯科",它将返回该文章,以及包含这两个国家的关键字组合的任何其他文章。
我知道这看起来有点令人费解。但由于我在使用这些API中没有关键字搜索,因此我尝试手动执行。
我真的很感激阅读这篇文章所需的耐心!感谢您提供有关如何处理此问题的任何帮助或想法。
答案 0 :(得分:1)
如果它是多维数组
,您可以使用find
let newArray = ["No sign probes into Moscow, Trump (America) campaign will die down", "Russian Hackers Who Targeted Clinton Appear to Attack France’s Macron", "Senate U.S.(United States) investigation to add 2 staffers", "Putin foo bar", "US test"];
let event = [["United States", "U.S.", "US", "America"], ["Russia", "Russian", "Putin", "Moscow"]]
let result = event.map(words => newArray.filter(article => words.find(word => article.toString().indexOf(word) > -1)));
console.log(result);
答案 1 :(得分:1)
如果您只关心eventKeywords数组中包含单个关键字的事件,则以下工作
const events = [
"No sign probes into Russia, Trump campaign will die down",
"Russian Hackers Who Targeted Clinton Appear to Attack France’s Macron",
"Senate Russia investigation to add 2 staffers",
"Former Trump adviser Flynn likely broke law with Russia trip: lawmakers",
]
const eventKeywords = [["United States", "U.S.", "US", "America", "Trump"], ["Russia", "Russian", "Putin", "Moscow"]]
const myDesiredEvents = events.filter(ev => {
return eventKeywords.filter(keywords => {
return keywords.filter(k => ev.toString().indexOf(k) > -1).length > 0
}).length > 0
})
// myDesiredEvents == [
// "No sign probes into Russia, Trump campaign will die down",
// "Russian Hackers Who Targeted Clinton Appear to Attack France’s Macron",
// "Senate Russia investigation to add 2 staffers",
// "Former Trump adviser Flynn likely broke law with Russia trip: lawmakers",
//]
如果您需要的事件至少包含2个不同的关键字,请使用以下内容(最后只更改.length >= 2
)
const events = [
"No sign probes into Russia, Trump campaign will die down",
"Russian Hackers Who Targeted Clinton Appear to Attack France’s Macron",
"Senate Russia investigation to add 2 staffers",
"Former Trump adviser Flynn likely broke law with Russia trip: lawmakers",
]
const eventKeywords = [["United States", "U.S.", "US", "America", "Trump"], ["Russia", "Russian", "Putin", "Moscow"]]
const myDesiredEvents = events.filter(ev => {
return eventKeywords.filter(keywords => {
return keywords.filter(k => ev.toString().indexOf(k) > -1).length > 0
}).length >= 2
})
// myDesiredEvents == [
// "No sign probes into Russia, Trump campaign will die down",
// "Former Trump adviser Flynn likely broke law with Russia trip: lawmakers"
// ]
编辑添加一个扁平选项,这是一个更少的代码和更清洁的
如果您只需要在二维关键字数组中进行一次匹配,如果您展平二维数组,则会将其转换为单个关键字数组以进行检查
const events = [
"No sign probes into Russia, Trump campaign will die down",
"Russian Hackers Who Targeted Clinton Appear to Attack France’s Macron",
"Senate Russia investigation to add 2 staffers",
"Former Trump adviser Flynn likely broke law with Russia trip: lawmakers",
]
const eventKeywords = [["United States", "U.S.", "US", "America", "Trump"], ["Russia", "Russian", "Putin", "Moscow"]]
const flattenedEventKeywords = [].concat.apply([], eventKeywords)
// flattenedEventKeywords == ["United States", "U.S.", "US", "America", "Trump", "Russia", "Russian", "Putin", "Moscow"]
const myDesiredEvents = events.filter(ev => {
return flattenedEventKeywords.filter(keywords => {
return ev.toString().indexOf(keywords) > -1
}).length > 0
})
// myDesiredEvents == [
// "No sign probes into Russia, Trump campaign will die down",
// "Russian Hackers Who Targeted Clinton Appear to Attack France’s Macron",
// "Senate Russia investigation to add 2 staffers",
// "Former Trump adviser Flynn likely broke law with Russia trip: lawmakers",
//]
答案 2 :(得分:1)
只获得那些提及至少两个不同国家(或其中一个替代词)的文章,然后:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" Width="1000px" HorizontalAlign="Center" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="IDAfspraken">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("IDAfspraken") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
如果您希望仅包含与事件的所有条目匹配的文章,而不仅仅是2,那么请使用const bingArray = [
"No sign probes into Russia, Trump campaign will die down",
"Russian Hackers Who Targeted Clinton Appear to Attack France’s Macron",
"Senate Russia investigation to add 2 staffers",
"Former Trump adviser Flynn likely broke law with Russia trip: lawmakers",
'Big meeting in Moscow tomorrow',
'Russia has U.S. ambassy closed',
'Did Putin influence the United States elections?',
];
const event = [["United States", "U.S.", "US", "America"],
["Russia", "Russian", "Putin", "Moscow"]];
const matches = bingArray.filter(
article => event.filter(
words => words.find(
word => article.includes(word)
)
).length > 1
);
console.log('articles mentioning at least 2 countries:');
console.log(matches);
:
every