-
Notifications
You must be signed in to change notification settings - Fork 0
Sanjana's Implementation of Identifying the Most Common Word in a Paragraph #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
b5c5d05
5bb7144
801cdcb
8d7f8c0
4db60c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,18 +14,44 @@ Words only consist of letters, never apostrophes or other punctuation symbols. | |
| - identify most frequent word and return | ||
| */ | ||
|
|
||
| var nDictionary = Object.create(null); | ||
| function getWordsWithHighestFreq(paragraphArr) { | ||
| var frequencyDict = new Object(); | ||
|
|
||
| function setDictionary(index, value) { | ||
| nDictionary[index] = value; | ||
| } | ||
|
|
||
| function getDictionary(index) { | ||
| return nDictionary[index]; | ||
| } | ||
| //Adds words from paragraph into a dictionary | ||
| for (var i = 0; i < paragraphArr.length; i++) { | ||
| var key = paragraphArr[i]; | ||
|
|
||
| getDictWithFrequencies(paragraphArr, bannedWordsArr) { | ||
| var frequencyDict = new Object(); | ||
| if(key in frequencyDict) { | ||
| value = frequencyDict[key]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm pretty sure you should have declare 'value' with var or let here, unlike Object.entries which provides [key, value]. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pretty sure just in case I'm missing something like Object.entries |
||
| frequencyDict[key] = value + 1; | ||
| } else { | ||
| frequencyDict[key] = 1; | ||
| } | ||
| } | ||
| console.log("The values in the dictioanry are the following: ", frequencyDict); | ||
|
|
||
|
|
||
| //Identify the number of the maximum occurance of a word | ||
| var max = 0; | ||
|
|
||
| for(const [key, value] of Object.entries(frequencyDict)) { | ||
| if(value > max) { | ||
| max = value; | ||
| } | ||
| } | ||
| console.log("The maximum occurance of a word is: ", 2) | ||
|
|
||
| //Parse dictionary to retrieve words of highest count | ||
| var wordsOfHighFrequency = [] | ||
|
|
||
| for(const [key, value] of Object.entries(frequencyDict)) { | ||
| if(value == max) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. === |
||
| wordsOfHighFrequency.push(key); | ||
| } | ||
| } | ||
| console.log("The most common words are", wordsOfHighFrequency); | ||
|
|
||
| return wordsOfHighFrequency; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -35,18 +61,16 @@ Words only consist of letters, never apostrophes or other punctuation symbols. | |
| */ | ||
| const mostCommonWord = (paragraph, banned) => { | ||
| var noPunctuationParagraph = paragraph.toLowerCase().replace(/(!|;|'|,|\.|\?)/g,"").trim(); | ||
| console.log("The paragraph written in lowercase and without punctuation is the following: " ,noPunctuationParagraph); | ||
|
|
||
| var wordsInPargrphArr = noPunctuationParagraph.split(" "); | ||
|
|
||
| var bannedArray = []; | ||
| bannedArray.push(banned); | ||
| wordsInPargrphArr = wordsInPargrphArr.filter(word => word != banned); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Always use !== instead of != (and same for ===) in Javascript, unless you're specifically looking to coerce values. I know the udemy course has more info somewhere in the first half |
||
| console.log("The array of words in paragraph without the banned word is the following: ", wordsInPargrphArr); | ||
|
|
||
| //method retrieves the word with the highest frequency | ||
| getWordWithHighestFreq(wordsInPargrphArr,bannedArray); | ||
|
|
||
| const mostCommonWord = getWordsWithHighestFreq(wordsInPargrphArr); | ||
| return mostCommonWord.values(); | ||
|
|
||
| console.log(noPunctuationParagraph); | ||
| console.log(wordsInPargrphArr) | ||
| console.log(bannedArray); | ||
| }; | ||
|
|
||
| mostCommonWord("THE CAT IS HERE, and dog is here?", "cat"); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd add a space between 'if' and first (
Same for 'for' loops (line 21 looks good)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, haven't seen (propertyName in objectName) used before--that's cool. Most of the time I tend to see objectName.hasOwnProperty(propName), which makes sure that the property is not on the object's prototype