There have been multiple accounts created with the sole purpose of posting advertisement posts or replies containing unsolicited advertising.

Accounts which solely post advertisements, or persistently post them may be terminated.

davel ,
@davel@lemmy.ml avatar

Optimize Memory and Performance with the CVM Algorithm in JavaScript: A Comprehensive Guide for Text Analysis, Big Data, and More

Final CVM Algorithm Code in a JavaScript “oneliner”

You can use this JS function by providing any text string as input, and it will simulate the described algorithm, returning the final set of unique words. This is as condensed as it can get, but if you can condese it more , please do post and Ill update it! If you are interested in seeing how this ‘oneliner’ started, before it was condensed, I have included the long form version of this same code at the bottom of the article.


<span style="font-weight:bold;color:#a71d5d;">const </span><span style="font-weight:bold;color:#795da3;">cvmAlgorithm </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#323232;">t </span><span style="font-weight:bold;color:#a71d5d;">=> </span><span style="color:#323232;">{ </span><span style="font-weight:bold;color:#a71d5d;">const </span><span style="font-weight:bold;color:#795da3;">f </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#323232;">n </span><span style="font-weight:bold;color:#a71d5d;">=> !</span><span style="color:#323232;">(</span><span style="color:#0086b3;">Math</span><span style="color:#323232;">.random() </span><span style="font-weight:bold;color:#a71d5d;">* </span><span style="color:#323232;">(</span><span style="color:#0086b3;">1 </span><span style="font-weight:bold;color:#a71d5d;"><< </span><span style="color:#323232;">n) </span><span style="font-weight:bold;color:#a71d5d;">| </span><span style="color:#0086b3;">0</span><span style="color:#323232;">); </span><span style="font-weight:bold;color:#a71d5d;">const </span><span style="color:#323232;">w </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#323232;">t.match(</span><span style="color:#183691;">/</span><span style="font-weight:bold;color:#a71d5d;">b</span><span style="color:#0086b3;">w</span><span style="font-weight:bold;color:#a71d5d;">+b</span><span style="color:#183691;">/</span><span style="font-weight:bold;color:#a71d5d;">g</span><span style="color:#323232;">) </span><span style="font-weight:bold;color:#a71d5d;">|| </span><span style="color:#323232;">[]; </span><span style="font-weight:bold;color:#a71d5d;">let </span><span style="color:#323232;">r </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#0086b3;">1</span><span style="color:#323232;">, u </span><span style="font-weight:bold;color:#a71d5d;">= new </span><span style="color:#0086b3;">Set</span><span style="color:#323232;">(); </span><span style="font-weight:bold;color:#a71d5d;">for </span><span style="color:#323232;">(</span><span style="font-weight:bold;color:#a71d5d;">let </span><span style="color:#323232;">i </span><span style="font-weight:bold;color:#a71d5d;">of </span><span style="color:#323232;">w.map(x </span><span style="font-weight:bold;color:#a71d5d;">=> </span><span style="color:#323232;">x.toLowerCase())) u.size </span><span style="font-weight:bold;color:#a71d5d;">< </span><span style="color:#0086b3;">100 </span><span style="font-weight:bold;color:#a71d5d;">? </span><span style="color:#323232;">u.</span><span style="color:#0086b3;">add</span><span style="color:#323232;">(i) </span><span style="font-weight:bold;color:#a71d5d;">: </span><span style="color:#323232;">f(r) </span><span style="font-weight:bold;color:#a71d5d;">&& </span><span style="color:#323232;">u.delete(i), u.size </span><span style="font-weight:bold;color:#a71d5d;">>= </span><span style="color:#0086b3;">100 </span><span style="font-weight:bold;color:#a71d5d;">&& </span><span style="color:#323232;">r</span><span style="font-weight:bold;color:#a71d5d;">++</span><span style="color:#323232;">, u.size </span><span style="font-weight:bold;color:#a71d5d;">> </span><span style="color:#0086b3;">50 </span><span style="font-weight:bold;color:#a71d5d;">&& </span><span style="color:#323232;">(u </span><span style="font-weight:bold;color:#a71d5d;">= new </span><span style="color:#0086b3;">Set</span><span style="color:#323232;">([</span><span style="font-weight:bold;color:#a71d5d;">...</span><span style="color:#323232;">u].filter(() </span><span style="font-weight:bold;color:#a71d5d;">=> </span><span style="color:#323232;">f(r)))); </span><span style="font-weight:bold;color:#a71d5d;">return </span><span style="color:#323232;">[</span><span style="font-weight:bold;color:#a71d5d;">...</span><span style="color:#323232;">u]; };
</span><span style="color:#323232;">
</span><span style="font-style:italic;color:#969896;">// Example usage:
</span><span style="font-weight:bold;color:#a71d5d;">const </span><span style="color:#323232;">text </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#183691;">"To be, or not to be, that is the question..."</span><span style="color:#323232;">; 
</span><span style="font-weight:bold;color:#a71d5d;">const </span><span style="color:#323232;">finalWords </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#323232;">cvmAlgorithm(text);
</span><span style="color:#795da3;">console</span><span style="color:#323232;">.</span><span style="color:#0086b3;">log</span><span style="color:#323232;">(finalWords);  </span><span style="font-style:italic;color:#969896;">// [ 'to', 'be', 'or', 'not', 'that', 'is', 'the', 'question' ]
</span>
  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • [email protected]
  • lifeLocal
  • goranko
  • All magazines