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>