-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexio.min.js
1 lines (1 loc) · 7.62 KB
/
lexio.min.js
1
class Lexio{constructor(){this.Tokenizer=class e{constructor(e={"i'm":"i am","you're":"you are","it's":"it is","we're":"we are","they're":"they are","i've":"i have","you've":"you have","we've":"we have","they've":"they have","i'll":"i will","you'll":"you will","he'll":"he will","she'll":"she will","it'll":"it will","we'll":"we will","they'll":"they will","don't":"do not","doesn't":"does not","didn't":"did not","won't":"will not","wouldn't":"would not","shan't":"shall not","shouldn't":"should not","mustn't":"must not","ain't":"am not","let's":"let us","oughtn't":"ought not","used to":"used to","so that":"in order that","inc.":"in case","that's":"that is","this's":"this is","who's":"who is",gonna:"going to",gotta:"got to",wanna:"want to",shoulda:"should have",coulda:"could have",woulda:"would have","y'all":"you all",ya:"you",em:"them"}){this.contractions=e}removePunctuation(e){try{return e.replace(/[^\w\s]/g," ")}catch(a){throw a}}expandContractions(e){for(let a in this.contractions)e=e.replace(RegExp(a.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),"gi"),this.contractions[a]);try{return e}catch(t){throw t}}tokenize(e){e=this.expandContractions(e),e=(e=(e=this.removePunctuation(e)).trim().toLowerCase().split(/\s/g)).filter(e=>""!==e);try{return e}catch(a){throw a}}},this.StopWordRemover=class e{constructor(e=["a","an","and","are","as","at","be","but","by","for","if","in","into","is","it","no","not","of","on","or","such","that","the","their","then","there","these","they","this","to","was","were","which","while","with","above","across","against","along","among","around","at","before","behind","below","beside","between","by","down","from","in","into","near","of","off","on","over","past","since","through","throughout","till","to","toward","under","until","up","upon","with","within","without","and","but","or","nor","for","so","yet","i","me","my","mine","we","us","our","ours","you","your","yours","he","him","his","she","her","hers","it","its","they","them","their","theirs","am","is","are","was","were","be","been","being","have","has","had","do","does","did","shall","should","will","would","can","could","may","might","must","ought","shall","should","will","would"]){this.stopWords=e}removeStopWords(e){let a=[];e.forEach(e=>{this.stopWords.includes(e)||a.push(e)});try{return a}catch(t){throw t}}}}}class LexioSentimentAnalyzer{constructor(e={}){this.uDict=e,this.bDict={hell:-1,admire:1,adore:1,amazing:1,appreciate:1,awesome:1,no:-1,sucks:-1,friggin:-1,fuck:-1,none:-1,not:-1,never:-1,sad:-1,sorry:-1,unhappy:-1,bad:-1,worse:-1,worst:-1,yes:1,happy:1,glad:1,good:1,better:1,best:1,okay:0,maybe:0,possibly:0,likely:0,unlikely:-1,certainly:1,definitely:1,always:1,forever:1,rarely:-1,seldom:-1,beautiful:1,benefit:1,boost:1,brilliant:1,celebrate:1,cherish:1,comfort:1,delight:1,enjoy:1,excellent:1,fantastic:1,good:1,happy:1,harmony:1,hope:1,inspire:1,joy:1,love:1,lovely:1,motivate:1,outstanding:1,peace:1,praise:1,recommend:1,reassure:1,refresh:1,restore:1,satisfy:1,strengthen:1,thrive:1,uplift:1,value:1,welcome:1,abhor:-1,abuse:-1,annoy:-1,awful:-1,bad:-1,bitter:-1,cruel:-1,damage:-1,demolish:-1,destroy:-1,disappoint:-1,endanger:-1,evil:-1,exploit:-1,fail:-1,fear:-1,frighten:-1,harm:-1,hate:-1,horrible:-1,hurt:-1,ignore:-1,injure:-1,insult:-1,interrupt:-1,isolate:-1,lose:-1,manipulate:-1,neglect:-1,pain:-1,reject:-1,scorn:-1,suffer:-1,terrible:-1,tragedy:-1,ugly:-1,unhappy:-1,unpleasant:-1,worst:-1,describe:0,explain:0,mention:0,note:0,report:0,say:0,state:0,ask:0,analyze:0,compare:0,consider:0,debate:0,discuss:0,examine:0,explore:0,highlight:0,illustrate:0,interpret:0,outline:0,review:0,study:0,normal:0,standard:0,average:0,news:0,information:0,fact:0,data:0,detail:0,description:0,documentation:0,evidence:0,example:0,explanation:0,finding:0,information:0,instruction:0,message:0,note:0,observation:0,report:0,result:0,statement:0},this.sDict={...this.bDict,...this.uDict}}analyzeSentiment(e){let a=0,t=new Lexio,i=new t.Tokenizer,s=new t.StopWordRemover,n=s.removeStopWords(i.tokenize(e));n.forEach(e=>{this.sDict[e]&&(a+=this.sDict[e])});try{if(a<0)return"negative";if(a>0)return"positive";if(0===a)return"neutral";else return`sentimentScore:${a}`}catch(o){throw o}}}class Lner{constructor(e={},a=[]){this.gazetteer=e,this.rules=a}addGazetteer(e,a){this.gazetteer[e.toLowerCase()]=a}addRule(e){this.rules.push(e)}identifyEntities(e){let a=new Lexio,t=new a.Tokenizer,i=t.tokenize(e),s=[];i.forEach(e=>{this.gazetteer[e]&&s.push(e,this.gazetteer[e.toLowerCase()]),this.rules.forEach(a=>{a.test(e)&&s.push({token:e,type:a.type})})});try{return s}catch(n){throw n}}}class LexioPorterStemmer{constructor(e=["ATE","ATELY","ATING","ED","EN","ING","IZE","IZED","IZES","LY","ANCE","ANCES","ANT","ANTS","ATION","ATIONS","EMENT","EMENTS","ENCE","ENCES","ENT","ENTS","ER","ERS","FUL","FULS","IBILITY","IBLE","ISM","ISMS","ABLE","AL","ALIS","ALS","ICAL","ICS","IVE","LESS","OUS","WISE","CTION","CTIONS","DITION","DITIONS","GUARD","GUARDS","HOOD","HOODS","SHIP","SHIPS","BE","HAVE","S","D","NESS","NE","ING"].map(e=>e.toLowerCase())){this.porterStemmerSuffixes=e}stem(e){for(let a in(e=e.toLowerCase()).endsWith("ss")?e=e.slice(0,-2):e.endsWith("s")&&(e=e.slice(0,-1)),e.endsWith("ed")||e.endsWith("ly")?e=e.slice(0,-2):e.endsWith("ing")&&(e=e.slice(0,-3)),this.porterStemmerSuffixes)if(e.endsWith(a)){if(a.length<=3)continue;e.slice(0,-a.length)}try{return e}catch(t){throw t}}}class LexioLemmatizer{constructor(e={abandon:"abandon",abandoned:"abandon",abandoning:"abandon",moved:"move",ability:"ability",able:"able",absence:"absence",absent:"absent",accept:"accept",accepted:"accept",accepting:"accept",action:"action",active:"active",activity:"activity",actual:"actual",actually:"actually",add:"add",added:"add",adding:"add",address:"address",adjust:"adjust",adjusted:"adjust",adjusting:"adjust",advance:"advance",advanced:"advance",advancing:"advance",advice:"advice",advise:"advise",advised:"advise",advising:"advise",affect:"affect",affected:"affect",affecting:"affect",agree:"agree",agreed:"agree",agreeing:"agree",allow:"allow",allowed:"allow",allowing:"allow",almost:"almost",alone:"alone",already:"already",also:"also",although:"although",always:"always",am:"be",among:"among",amount:"amount",an:"an",and:"and",another:"another",any:"any",anyone:"anyone",anything:"anything",appear:"appear",appeared:"appear",appearing:"appear",apply:"apply",applied:"apply",applying:"apply",approach:"approach",approached:"approach",approaching:"approach",area:"area",argue:"argue",argued:"argue",arguing:"argue",argues:"argue",around:"around",arrange:"arrange",arranged:"arrange",arranging:"arrange",arrive:"arrive",arrived:"arrive",arriving:"arrive",as:"as",ask:"ask",asked:"ask",asking:"ask",aspect:"aspect",assess:"assess",assessed:"assess",assessing:"assess",assign:"assign",assigned:"assign",assigning:"assign",assist:"assist",assisted:"assist",assisting:"assist",assume:"assume",assumed:"assume",assuming:"assume",at:"at",attack:"attack",attacked:"attack",attacking:"attack",attempt:"attempt",attempted:"attempt",attempting:"attempt",attend:"attend",attended:"attend",attending:"attend",authority:"authority",available:"available",average:"average",avoid:"avoid",avoided:"avoid",avoiding:"avoid",aware:"aware",away:"away",be:"be",bear:"bear",beat:"beat",became:"become",because:"because",become:"become",becomes:"become",been:"be",before:"before",began:"begin",begin:"begin",beginning:"begin",behalf:"behalf",being:"be",believe:"believe",believed:"believe",believing:"believe",below:"below",beside:"beside",best:"best",jumping:"jump",jumped:"jump",killed:"kill",killing:"kill"},a={}){this.bDict=e,this.uDict=a,this.dict={...this.uDict,...this.bDict}}lemmatize(e){let a=new Lexio,t=new a.Tokenizer,i=t.tokenize(e),s=i.map(e=>this.dict[e]||e);try{return s}catch(n){throw n}}}