Skip to content

Commit

Permalink
Merge branch 'add-word-count' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
caarmen committed Mar 27, 2021
2 parents 4cd1600 + f9f9865 commit 817af49
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 5 deletions.
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<script src="src/js/model/file/FileReaderOutput.js"></script>
<script src="src/js/model/file/FilesReader.js"></script>
<script src="src/js/model/poem/PoemRepository.js"></script>
<script src="src/js/model/poem/WordCounter.js"></script>
<script src="src/js/model/rhymer/RhymerRepository.js"></script>
<script src="src/js/model/rhymer/SyllableRhymes.js"></script>
<script src="src/js/model/rhymer/WordRhymes.js"></script>
Expand Down Expand Up @@ -139,6 +140,7 @@
<div id="placeholder-reader__controls__actions"></div>
</div>
<div id="placeholder-reader__input"></div>
<div id="placeholder-reader__word-count"></div>
<div id="placeholder-reader__saved-state"></div>
</div>
<div id="placeholder-snackbar"></div>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"description": "Poet Assistant",
"main": "main.js",
"scripts": {
"test": "node test/tests.js",
"test": "node test/tests_speech.js; node test/tests_word_counter.js",
"start": "electron ."
},
"keywords": [],
Expand Down
1 change: 1 addition & 0 deletions pwa/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const contentToCache = [
"/src/js/model/definitions/DefinitionsRepository.js",
"/src/js/model/Settings.js",
"/src/js/model/poem/PoemRepository.js",
"/src/js/model/poem/WordCounter.js",
"/src/js/view/VoiceSettingsView.js",
"/src/js/view/AppBarMenuView.js",
"/src/js/view/ListWordClickActions.js",
Expand Down
10 changes: 10 additions & 0 deletions src/css/reader.css
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,21 @@ along with Poet Assistant. If not, see <http://www.gnu.org/licenses/>.
margin-right: 0;
width: max-content;
}
#placeholder-reader__word-count{
white-space:pre;
font-size: 0.6em;
color: var(--mdc-theme-text-secondary-on-light, #727272);
text-align: start;
margin-inline-start: 8px;
height: 16px;
float: left;
}
#placeholder-reader__saved-state {
white-space:pre;
font-size: 0.6em;
color: var(--mdc-theme-text-secondary-on-light, #727272);
text-align: end;
margin-inline-end: 8px;
height: 16px;
float: right;
}
3 changes: 2 additions & 1 deletion src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"app_name": "Poet Assistant",
"app_bar_menu_about_title": "About",
"app_bar_menu_random_title": "Random word",
"app_version": "0.1.14",
"app_version": "0.1.15",
"btn_favorite_title": "Favorite",
"btn_search_title": "Search",
"btn_play_title": "Play",
Expand Down Expand Up @@ -54,6 +54,7 @@
"poem_saved_state_label_saving": "Saving",
"poem_saved_state_label_saved": "Saved",
"poem_saved_state_label_waiting": " ",
"poem_word_count": "{0} words, {1} characters",
"progressbar_app_label": "Loading app",
"progressbar_db_label": "Loading dictionaries {0}%",
"reader_hint": "Compose your poem text here",
Expand Down
19 changes: 17 additions & 2 deletions src/js/model/MainModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ class MainModel {
this._poemRepository = new PoemRepository(this._settings)
this._favoritesRepository = new FavoritesRepository(this._settings)
this.isSpeechPlaying = this._speechEngine.isPlaying
this.poemText = this._poemRepository.poemText
this.poemText = new ObservableField("")
this.poemWordCount = new ObservableField(0)
this.poemCharacterCount = new ObservableField(0)
this.favoritesObserver = (newFavorites) => { }
this.rhymerSettingsChangedObserver = () => { }
this.thesaurusSettingsChangedObserver = () => { }
this._poemRepository.poemText.observer = (newPoemText) => this._onPoemTextUpdated(newPoemText)
this._favoritesRepository.observer = (newFavorites) => this.favoritesObserver(newFavorites)
}
async loadDb(progressCallback) {
Expand Down Expand Up @@ -109,7 +112,19 @@ class MainModel {
}
}

setPoemText = (text, writeNow) => this._poemRepository.setPoemText(text, writeNow)
loadPoem = () => this._poemRepository.load()
setPoemText(text, writeNow){
this._poemRepository.setPoemText(text, writeNow)
this._updateWordCount(text)
}
_onPoemTextUpdated(text) {
this.poemText.value = text
this._updateWordCount(text)
}
_updateWordCount(text) {
this.poemWordCount.value = WordCounter.countWords(text)
this.poemCharacterCount.value = WordCounter.countCharacters(text)
}
readFile(file) {
const reader = new FileReader()
reader.onload = (e) => {
Expand Down
5 changes: 4 additions & 1 deletion src/js/model/poem/PoemRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ class PoemRepository {
constructor(settings) {
this._settings = settings
this.savedState = new ObservableField(PoemRepository.SaveState.WAITING)
this.poemText = new ObservableField(this._settings.getSetting(PoemRepository.SETTINGS_KEY_POEM_TEXT, ""))
this.poemText = new ObservableField("")
this._settings.addObserver((key, newValue) => this._onSettingsChanged(key, newValue))
this._timeoutId
}

load() {
this.poemText.value = this._settings.getSetting(PoemRepository.SETTINGS_KEY_POEM_TEXT, "")
}
_onSettingsChanged(key, newValue) {
if (key == PoemRepository.SETTINGS_KEY_POEM_TEXT) {
this.poemText.value = newValue
Expand Down
37 changes: 37 additions & 0 deletions src/js/model/poem/WordCounter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
Copyright (c) 2021 - present Carmen Alvarez
This file is part of Poet Assistant.
Poet Assistant is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Poet Assistant is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Poet Assistant. If not, see <http://www.gnu.org/licenses/>.
*/

class WordCounter {
}
// The following will each be considered as one word: good-hearted, don't, variable_name
WordCounter._NON_SPLITTING_PUNCTUATION = "-'’_"
WordCounter._REGEX_STRIP = new RegExp(`[${WordCounter._NON_SPLITTING_PUNCTUATION}]`)
WordCounter._REGEX_SPLIT = new RegExp(`[^${WordCounter._NON_SPLITTING_PUNCTUATION}\\p{L}0-9]`, "u")
WordCounter.countWords = (text) => {
if (!text || text == "") return 0
const tokens = text.replace(WordCounter._REGEX_STRIP, "")
.split(WordCounter._REGEX_SPLIT)
.filter((token) => token != "")
return tokens.length
}
WordCounter.countCharacters = (text) => text ? text.length : 0

if (typeof module != 'undefined') {
module.exports = WordCounter
}
1 change: 1 addition & 0 deletions src/js/view/MainView.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class MainView {
this._viewModel.activeTab.observer = (newActiveTab) => { this._viewTabs.switchToTab(newActiveTab) }
this._viewModel.loadingProgress.observer = (newLoadingProgress) => { this._updateLoadingProgress(newLoadingProgress) }
this._viewModel.isSpeechPlaying.observer = (newIsSpeechPlaying) => { this._viewReader.updateSpeechPlayingState(newIsSpeechPlaying) }
this._viewModel.poemWordCountLabel.observer = (wordCountLabel) => { this._viewReader.updatePoemWordCountLabel(wordCountLabel) }
this._viewModel.poemSavedStateLabel.observer = (savedStateLabel) => { this._viewReader.updatePoemSavedState(savedStateLabel) }
this._viewModel.poemText.observer = (newPoemText) => { this._viewReader.setPoemText(newPoemText) }
this._viewModel.voices.observer = (newVoices) => this._viewVoiceSettings.updateVoicesList(newVoices)
Expand Down
4 changes: 4 additions & 0 deletions src/js/view/ReaderView.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ReaderView {
this._elemPlaceholderReaderPlay = document.querySelector("#placeholder-reader__controls__play")
this._elemPlaceholderReaderActions = document.querySelector("#placeholder-reader__controls__actions")
this._elemPlaceholderReaderInput = document.querySelector("#placeholder-reader__input")
this._elemPlaceholderReaderWordCount = document.querySelector("#placeholder-reader__word-count")
this._elemPlaceholderReaderSavedState = document.querySelector("#placeholder-reader__saved-state")

this._elemBtnPlay
Expand Down Expand Up @@ -106,6 +107,9 @@ class ReaderView {
this._elemBtnPlay.innerText = "play_circle_filled"
}
}
updatePoemWordCountLabel(wordCountLabel) {
this._elemPlaceholderReaderWordCount.innerText = wordCountLabel
}
updatePoemSavedState(newSavedState) {
this._elemPlaceholderReaderSavedState.innerText = this._i18n.translate(newSavedState)
}
Expand Down
10 changes: 10 additions & 0 deletions src/js/viewmodel/MainViewModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class MainViewModel {
this.isRhymerLoading = new ObservableField(false)
this.isThesaurusLoading = new ObservableField(false)
this.isDefinitionsLoading = new ObservableField(false)
this.poemWordCountLabel = new ObservableField()
this.poemSavedStateLabel = new ObservableField()
this.activeTab = new ObservableField(MainViewModel.TabIndex.RHYMER)
this.loadingProgress = new ObservableField(0)
Expand All @@ -58,10 +59,12 @@ class MainViewModel {
this._model._speechEngine.pitch.observer = (newPitch) => this.voicePitch.value = newPitch
this._model._poemRepository.savedState.observer = (newSavedState) => this.poemSavedStateLabel.value = this._getSavedStateLabel(newSavedState)
this._model.favoritesObserver = (newFavorites) => this._onFavoritesChanged(newFavorites)
this._model.poemCharacterCount.observer = (characterCount) => { this._updateWordCountLabel(this._model.poemWordCount.value, characterCount) }
this._onFavoritesChanged(this._model.getFavorites())
}

loadTranslations = () => this.i18n.load()
.then(() => { this._model.loadPoem() })

loadDb() {
this._model.loadDb((dbOpenProgress) => {
Expand Down Expand Up @@ -359,6 +362,13 @@ class MainViewModel {
this._model.copyText(text, start, value)
this.snackbarText.value = "snackbar_copied_poem"
}
_updateWordCountLabel(wordCount, characterCount) {
if (wordCount > 0 && characterCount > 0) {
this.poemWordCountLabel.value = this.i18n.translate("poem_word_count", wordCount, characterCount)
} else {
this.poemWordCountLabel.value = ""
}
}
onClearClicked() {
this.dialogInfo.value = DialogInfo.prompt(
"reader_clear_poem_dialog_title",
Expand Down
File renamed without changes.
91 changes: 91 additions & 0 deletions test/tests_word_counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
Copyright (c) 2021 - present Carmen Alvarez
This file is part of Poet Assistant.
Poet Assistant is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Poet Assistant is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Poet Assistant. If not, see <http://www.gnu.org/licenses/>.
*/

require("./test_apis.js")()
require("./test_runner.js")()
WordCounter = require("../src/js/model/poem/WordCounter.js")

_testWordCounter = (input, expectedWordCount, expectedCharacterCount) => {
let actualWordCount = WordCounter.countWords(input)
let actualCharacterCount = WordCounter.countCharacters(input)
assertEquals(expectedWordCount, actualWordCount)
assertEquals(expectedCharacterCount, actualCharacterCount)
}

const tests = [
testEmpty = () => {
_testWordCounter("", 0, 0)
},
testSimpleSentence = () => {
_testWordCounter("See spot run.", 3, 13)
},
testApostrophe = () => {
_testWordCounter("I can't even", 3, 12)
},
testTaleOfTwoCities = () => {
_testWordCounter("we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way— in short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only.",
59,
325
)
},
testHuckleberryFinn = () => {
_testWordCounter("“Did I give you the letter?”\n" +
"“What letter?”\n" +
"“The one I got yesterday out of the post-office.”\n" +
"“No, you didn’t give me no letter.”\n" +
"“Well, I must a forgot it.”",
30,
157
)
},
testWarAndPeace = () => {
_testWordCounter("The count came waddling in to see his wife with a rather guilty look as usual.\n" +
"“Well, little countess? What a sauté of game au madère we are to have, my dear! I tasted it. The thousand rubles I paid for Tarás were not ill-spent. He is worth it!”",
49,
245
)
},
testShakespeare = () => {
_testWordCounter("Where wasteful Time debateth with decay\n" +
"To change your day of youth to sullied night,\n" +
" And all in war with Time for love of you,\n" +
" As he takes from you, I engraft you new.\n",
34,
175)
},
testDracula = () => {
_testWordCounter("Algernon. And, speaking of the science of Life, have you got the cucumber sandwiches cut for Lady Bracknell?\n" +
"\n" +
"Lane. Yes, sir. [Hands them on a salver.]",
26,
154
)
},
testDate1 = () => {
_testWordCounter("On the 5th of November, I wrote this test.",
9,
42
)
},
testDate2 = () => {
_testWordCounter("On 11/5/2017, I wrote this test.", 8, 32)
}
]

runTests(tests)

0 comments on commit 817af49

Please sign in to comment.