Searching for and compiling words from an article/webpage?

36 views Asked by At

Nevermind the political aspect of some of the keywords, but for a project I was hoping to build a basic JS program that could scan through an article or webpage and compile predefined words, and then of course measure the counts against one another to see if targeting keywords could locate bias. I think some of this is usable but as for the actual act of scanning I'm a bit lost. I initially thought of using Python but thought JS might be more appropriate, maybe I was wrong? I'm so far from an expert. Any help would be greatly appreciated.

function wordSearch() {
    var cons = ""
    var lib = ""

    switch (words) {
        case "freedom":
        case "socialism":
        case "socialist":
        case "socialism":
        case "marxist":
        case "marxism":
        case "china":
        case "nation":
        case "God":
        case "Hunter Biden":
        case "Benghazi":
        case "antifa":
        case "Hillary":
        case "blm":
            cons++;
            break;
        case "fascist":
        case "fascists":
        case "fascism":
        case "lgbtq":
        case "racist":
        case "racists":
        case "racism":
        case "ableism":
        case "Jan 6th":
        case "insurrection":
            lib++;
            break;
    }

    if (cons >= lib + 6) {
       return "Heavily Conservative";
    } else if (cons >= lib + 3) {
        return "Conservative";
    } else if (cons >= lib + 1) {
        return "Slightly Conservative";
    } else if (cons == lib) {
        return "Moderate";
    } else if (lib >= cons + 6) {
        return "Heavily Liberal";
    } else if (lib >= cons + 3) {
        return "Liberal";
    } else if (lib >= cons + 1) {
        return "Slightly Liberal";
    }
}

1

There are 1 answers

3
crypthusiast0 On

I think you'd be better off using a list of the conservative and libertarian words and then using a .forEach function.

let con = ["freedom", "blm"];
let lib = ["lgbtq", "fascism"];

let cons;
let libe;

con.forEach((word) => {
  if (word.includes(word)) {
    cons++
  }
})

lib.forEach((word) => {
  if (word.includes(word)) {
    libe++
  }
})

if (cons >= lib + 6) {
  return "Heavily Conservative";
} else if (cons >= lib + 3) {
  return "Conservative";
} else if (cons >= lib + 1) {
  return "Slightly Conservative";
} else if (cons == lib) {
  return "Moderate";
} else if (lib >= cons + 6) {
  return "Heavily Liberal";
} else if (lib >= cons + 3) {
  return "Liberal";
} else if (lib >= cons + 1) {
  return "Slightly Liberal";
}

Let me know if it works out for you!