Word Search/Collector

I’ll put my code below, but essentially I just made (and inefficiently, I’m sure. I tried using switches but couldn’t quite find it) something that scans a page for target words, and keeps a count/score of them. For this example I just used political keywords because I thought bias would be something easy to scan for. Now when I run this code on a JS sandbox site it works, so I’m assuming that, although bulky, the code itself is capable.

My question is - how exactly could I deploy this code to scan any page at random? Is there a way to utilize a URL and do so via terminal (I’m on Windows, for what it’s worth)? Or is there a better way? I’d like to be able to see a link to an article or something and bam, send the code in to see if it’s bias detection is actually working. Any advice would be greatly appreciated.

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

    if (
      (
        document.documentElement.textContent || document.documentElement.innerText  
      ) .indexOf("freedom") > -1 
    ) {
        cons++;
    }
    if (
      (
       document.documentElement.textContent || document.documentElement.innerText
      ).indexOf("socialism") > -1 
    ) {
        cons++;
    }
        if (
      (
       document.documentElement.textContent || document.documentElement.innerText
      ).indexOf("socialist") > -1 
    ) {
        cons++;
    }
        if (
      (
       document.documentElement.textContent || document.documentElement.innerText
      ).indexOf("socialists") > -1 
    ) {
        cons++;
    }
        if (
      (
       document.documentElement.textContent || document.documentElement.innerText
      ).indexOf("marxism") > -1 
    ) {
        cons++;
    }
        if (
      (
       document.documentElement.textContent || document.documentElement.innerText
      ).indexOf("marxist") > -1 
    ) {
        cons++;
    }
        if (
      (
       document.documentElement.textContent || document.documentElement.innerText
      ).indexOf("marxists") > -1 
    ) {
        cons++;
    }
        if (
      (
       document.documentElement.textContent || document.documentElement.innerText
      ).indexOf("china") > -1 
    ) {
        cons++;
    }
        if (
      (
       document.documentElement.textContent || document.documentElement.innerText
      ).indexOf("nation") > -1 
    ) {
        cons++;
    }
        if (
      (
       document.documentElement.textContent || document.documentElement.innerText
      ).indexOf("God") > -1 
    ) {
        cons++;
    }
        if (
      (
       document.documentElement.textContent || document.documentElement.innerText
      ).indexOf("Hunter Biden") > -1 
    ) {
        cons++;
    }
        if (
      (
       document.documentElement.textContent || document.documentElement.innerText
      ).indexOf("Hillary") > -1 
    ) {
        cons++;
    }
        if (
      (
       document.documentElement.textContent || document.documentElement.innerText
      ).indexOf("benghazi") > -1 
    ) {
        cons++;
    }
        if (
      (
       document.documentElement.textContent || document.documentElement.innerText
      ).indexOf("antifa") > -1 
    ) {
        cons++;
    }
        if (
      (
       document.documentElement.textContent || document.documentElement.innerText
      ).indexOf("blm") > -1 
    ) {
        cons++;
    }
        if (
      (
       document.documentElement.textContent || document.documentElement.innerText
      ).indexOf("fascism") > -1 
    ) {
        lib++;
    }
        if (
      (
       document.documentElement.textContent || document.documentElement.innerText
      ).indexOf("fascist") > -1 
    ) {
        lib++;
    }
        if (
      (
       document.documentElement.textContent || document.documentElement.innerText
      ).indexOf("fascists") > -1 
    ) {
        lib++;
    }
        if (
      (
       document.documentElement.textContent || document.documentElement.innerText
      ).indexOf("racism") > -1 
    ) {
        lib++;
    }
        if (
      (
       document.documentElement.textContent || document.documentElement.innerText
      ).indexOf("racist") > -1 
    ) {
        lib++;
    }
        if (
      (
       document.documentElement.textContent || document.documentElement.innerText
      ).indexOf("racists") > -1 
    ) {
        lib++;
    }
        if (
      (
       document.documentElement.textContent || document.documentElement.innerText
      ).indexOf("lgbtq") > -1 
    ) {
        lib++;
    }
        if (
      (
       document.documentElement.textContent || document.documentElement.innerText
      ).indexOf("ableism") > -1 
    ) {
        lib++;
    }
        if (
      (
       document.documentElement.textContent || document.documentElement.innerText
      ).indexOf("Jan 6th") > -1 
    ) {
        lib++;
    }
        if (
      (
       document.documentElement.textContent || document.documentElement.innerText
      ).indexOf("insurrection") > -1 
    ) {
        lib++;
    }

    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";
    }
}

wordSearch();

I’m not really sure what you’re trying to do, but I have two notes:

  1. You would benefit greatly from learning about loops and arrays.

  2. This approach will not yield statistically meaningful information. You need more advanced statistical tools to do this sort of text analysis.

I realize this won’t yield useful results, I’m more just doing it to see if I can, as a personal little project. And if it works, sure maybe down the line I could start getting into more advanced/nuanced ways of detecting bias, but for now I’m really just curious if I can simply implement an app that can detect and compile words from a given webpage.

I’m in the middle of the fcc JS course so I realize my code isn’t outstanding, I was just curious if I could make, using JS, something I would have typically defaulted to making with Python. If I can get it to work then I can get in to how better to sort out meaningful language.

You could use node and run it at the command line or you could create a web form and run it from there. Either way, pass in a URL, use fetch (or whatever is equivalent in node) to pull down the web page and then parse it with your logic. How you want to display the results is up to you.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.