gpt4 book ai didi

javascript - 回答了 : Streamlining

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

上下文:我正在从日志文件中读取如下所示的数据:

{"action": "tweet", id: 1234, user: "user1", timestamp: 3120}
{"action": "retweet", target_id: 1234, user: "user2", timestamp: 4020}
{"action": "tweet", id: 1235, user: "user3", timestamp: 5320}
{"action": "retweet", target_id: 1235, user: "user4", timestamp: 5820}
{"action": "retweet", target_id: 1235, user: "user2", timestamp: 6540}

日志文件中有更多数据,但这些是最重要的字段。

我创建了以下函数来读取日志文件的每一行,并检测用户是否是垃圾邮件制造者。垃圾邮件发送者的标准是:

  • 不得有“推文”操作
  • 必须至少有一个“转推”操作
  • 他们的所有转推必须在目标推文的 1000 毫秒内发生(显示为 target_id 与 id 的关联)。

该函数应该返回一个垃圾邮件发送者数组,但不会重复任何已经被标记至少一次的人。

我相信我可以清理很多东西:

identifySpammers = (file_path) => {
const spammers = [];
const notSpammers = []; // I"m only using this to track when someone comes up as having a retweet that was after 1000 milliseconds
let previousId;
let previousTimeStamp;
const data = fs.readFileSync(`/${file_path}`, 'utf8').trim().split("\n")
for(index in data) {
const parsedData = JSON.parse(data[index]);
if(parsedData.timestamp - previousTimeStamp >= 1001 && spammers.includes(parsedData.user)) {
notSpammers.push(parsedData.user)
} else if (!spammers.includes(parsedData.user) && parsedData.target_id === previousID && parsedData.timestamp - previousTimeStamp < 1001) {
spammers.push(parsedData.user)
} else if (parsedData.action === "tweet") {
previousId = parsedData.id
previousTimeStamp = parsedData.timestamp
notSpammers.push(parsedData.user)
}
}

for(let i = 0; i < spammers.length; i++) {
if(notSpammers.includes(spammers[i])) {
spammers.splice(i, 1)
}
}

return spammers;
}

我真正应该做的是利用类似的条件,但写入一个对象,其中每个键都是用户名,如果值是他们是否是垃圾邮件发送者,那么就像:

  const spammers = {
user1: null, // Initialize as null
user2: false, // Once a user is marked as false, it is impossible for them to ever be considered a spammer.
user3: true // This could end up being false later on
}

我尝试使用与我的双数组解决方案类似的逻辑来执行此操作,但没有成功。

根据上述代码,如果有人可以提供任何指导,让我可以提出更高效的对象驱动解决方案,我将不胜感激。不需要完整的答案,只需要一些关于如何处理的建议以及您可能拥有的任何想法。

谢谢!

编辑 1:清除了一些错误并阐明了用户成为垃圾邮件制造者时的情况。编辑 2:扩展日志文件以演示用户如何更早地被标记为垃圾邮件,但随后通过必要的条件才不会成为垃圾邮件。

最佳答案

我以更简洁的代码方式重构并使用建议的数据结构:

var data = [
{"action": "tweet", id: 1234, user: "user1", timestamp: 1341414},
{"action": "retweet", target_id: 1234, user: "user2", timestamp: 1341415},
{"action": "tweet", id: 1235, user: "user3", timestamp: 2341414},
{"action": "retweet", target_id: 1235, user: "user1", timestamp: 2341415},
]

const isSpammerByUser = {};
let previousId;
let previousTimeStamp;
for(index in data) {
const parsedData = data[index];
const user = parsedData.user;
const isUserASpammer = (isSpammerByUser[user] === true);
const hasUserProvenToNotBeASpammer = (isSpammerByUser[user] === false);
const isFastAction = (parsedData.timestamp - previousTimeStamp < 1001);
const isRetweetFromPrevious = (parsedData.target_id === previousId);

if (parsedData.action === "tweet") {
isSpammerByUser[user] = false;
previousId = parsedData.id
previousTimeStamp = parsedData.timestamp

} else if(isUserASpammer && !isFastAction) {
isSpammerByUser[user] = false;

} else if (!hasUserProvenToNotBeASpammer && isRetweetFromPrevious && isFastAction) {
isSpammerByUser[user] = true;
}
}

const spammers = [];
for (user in isSpammerByUser)
if (isSpammerByUser[user])
spammers.push(user);

console.log("spammers: " + spammers.join(","));

关于javascript - 回答了 : Streamlining,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49833131/

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