Skip to content

Instantly share code, notes, and snippets.

@DeoluA
Last active September 9, 2018 06:48
Show Gist options
  • Save DeoluA/eaa4182451ea9689653af67af768b406 to your computer and use it in GitHub Desktop.
Save DeoluA/eaa4182451ea9689653af67af768b406 to your computer and use it in GitHub Desktop.
Get the frequency of words in a string using Lodash. AngularJS is used here for interactive functionality.
<!DOCTYPE html>
<head>
<script type="text/javascript" src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js'></script>
<script type="text/javascript" src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js'></script>
<script type="text/javascript" src='wordFreq.js'></script>
</head>
<body ng-app='wordFreqApp'>
<div ng-controller='wordFreqCtrl'>
<h2>Input something below</h2>
<input ng-model='completeString' size = '50'>
<button ng-click='getFreq(completeString)'>Get Word Frequency</button>
<br />
<h2>Raw Array Output:</h2>
{{result}}
<h2>Output, With Some AngularJS Formating</h2>
<ul ng-repeat = "x in result | orderBy: '-freq'">
<li><strong>{{x.word}}</strong> occurs <em>{{x.freq}} {{x.freq > 1 ? "times": "time"}}</em></li>
</ul>
</div>
</body>
</html>
wordFreqApp = angular.module('wordFreqApp', []);
wordFreqApp.controller('wordFreqCtrl', function($scope){
// this is what our string will initially be. Will change once user inputs something new
$scope.completeString = 'Que sera, sera. Whatever will be, will be.'
// this ng-model will display the result
$scope.result = '';
// angular function to derive frequency
$scope.getFreq = function(completeString){
// change everything to lower case, so we don't repeat counts
completeString = completeString.toLowerCase();
// replace all punctuation marks and numbers with space. Replace excess whitespace too
completeString = completeString.replace(/[!,?.":;]/gm,' ').replace(/\d/gm,' ').replace(/ /gm,' ');
// if you want to remove other unwanted charactes such as links, uncomment the line below
// completeString.replace(/(http|https|ftp):\/\/[\n\S]+/gm, ' ').replace(/@[\n\S]+/gm, ' ').replace(/[@]/gm, ' ').replace(/RT/gm, ' ').replace(/\n/gm, ' ').replace(/\r/gm, ' ').replace(/ /gm, ' ');
// this array will contain each word and its count
var allWords = [];
// lodash chain to derive frequency
_.chain(completeString)
.split(" ")
.remove(function(f) { return f.length > 2; }) // removes pronouns, initials or any string with less than 2 characters. Delete this line if you don't want this
.countBy()
.forEach(function(n_value, n_key){
allWords.push({ word: n_key, freq: n_value })
})
.value();
// pass to ng-model
$scope.result = allWords;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment