Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
How to use require js to do a sentiment analysis on a bunch of text

Background

I have a json file called Dictionary.js,it contains word objects. Each word has an integer value assigned to it based on whether its positive or negative. If the word is positive then its value must be greater than 0, if it is negative then the value assigned to that word must be less than 0. If a word is not found to exist in this array then it is assigned a default value of 0. The javascript file I have sums up all the values of the words and gives a resultant score that I must use to analyze if the sentiment of the comment was negative, neutral or positive.

Implementation

After learning that I cannot read the javascript object notation file directly then I decided to use require.js so that I can be able to use the requirejs(['path.json'],function(data){}) to read it.

I added an onkeydown() event listener to the input entry that accepts the comments from user and attached a javascript function called sentiments that is supposed to alert the score as the user continue to type words. There are a lot of lambda functions to handle the word processing from the json object but my code doesn't work for some reason please help...

<!--dependency for require.js-->
<head>
<script src='require.js'></script>
</head>
<body>
<input type="text" id="user_comment" onkeydown="sentiments()"/>
<script>
let comment=document.getElementById("user_comment").value;
function sentiments(){
requirejs(["Dictionary.json"],function(data){
let AFINN=json.parse(data);
const tokenize = text => text.toLowerCase().split(" ");

const deleteUselessChars = word => word.replace(/[^\w]/g, "");

const rateWord = word => (word in AFINN ? AFINN[word] : 0);

const sum = (x, y) => x + y;

const analyseText = text =>
tokenize(text)
.map(deleteUselessChars)
.map(rateWord)
.reduce(sum);

});
//supposed to get integer alerts from this
alert(analyseText(comment));
}
</script>
</body>

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!