Skip to content

Commit 1cf54a8

Browse files
committed
feat: add unique words count and read time
Fix CorentinTh#1214
1 parent af1013d commit 1cf54a8

File tree

3 files changed

+64
-9
lines changed

3 files changed

+64
-9
lines changed

src/tools/text-statistics/text-statistics.service.test.ts

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('text-statistics', () => {
1414

1515
describe('textStatistics', () => {
1616
it('should return text statistics', () => {
17-
expect(textStatistics('a')).toEqual({
17+
expect(textStatistics('a')).to.deep.eq({
1818
chars: 1,
1919
chars_digits: 0,
2020
chars_lower: 1,
@@ -26,8 +26,11 @@ describe('text-statistics', () => {
2626
sentences: 1,
2727
words: 1,
2828
words_no_puncs: 1,
29+
read_time: 0.3,
30+
words_uniques: 1,
31+
words_uniques_ci: 1,
2932
});
30-
expect(textStatistics('A')).toEqual({
33+
expect(textStatistics('A')).to.deep.eq({
3134
chars: 1,
3235
chars_digits: 0,
3336
chars_lower: 0,
@@ -39,8 +42,11 @@ describe('text-statistics', () => {
3942
sentences: 1,
4043
words: 1,
4144
words_no_puncs: 1,
45+
read_time: 0.3,
46+
words_uniques: 1,
47+
words_uniques_ci: 1,
4248
});
43-
expect(textStatistics('a a')).toEqual({
49+
expect(textStatistics('a a')).to.deep.eq({
4450
chars: 3,
4551
chars_digits: 0,
4652
chars_lower: 2,
@@ -52,8 +58,11 @@ describe('text-statistics', () => {
5258
sentences: 1,
5359
words: 2,
5460
words_no_puncs: 2,
61+
read_time: 0.6,
62+
words_uniques: 1,
63+
words_uniques_ci: 1,
5564
});
56-
expect(textStatistics('A a ; 1')).toEqual({
65+
expect(textStatistics('A a ; 1')).to.deep.eq({
5766
chars: 7,
5867
chars_digits: 1,
5968
chars_lower: 1,
@@ -65,8 +74,11 @@ describe('text-statistics', () => {
6574
sentences: 1,
6675
words: 4,
6776
words_no_puncs: 3,
77+
read_time: 0.8999999999999999,
78+
words_uniques: 3,
79+
words_uniques_ci: 2,
6880
});
69-
expect(textStatistics('Some sentence! Une autre phrase ? « et avec des chiffres 1234 ! »')).toEqual({
81+
expect(textStatistics('Some sentence! Une autre phrase ? « et avec des chiffres 1234 ! »')).to.deep.eq({
7082
chars: 65,
7183
chars_digits: 4,
7284
chars_lower: 41,
@@ -78,9 +90,12 @@ describe('text-statistics', () => {
7890
sentences: 3,
7991
words: 14,
8092
words_no_puncs: 10,
93+
read_time: 3,
94+
words_uniques: 10,
95+
words_uniques_ci: 10,
8196
});
8297
expect(textStatistics(`Some sentence! Une autre phrase ?
83-
« et avec des chiffres 1234 ! »`)).toEqual({
98+
« et avec des chiffres 1234 ! »`)).to.deep.eq({
8499
chars: 72,
85100
chars_digits: 4,
86101
chars_lower: 41,
@@ -92,8 +107,11 @@ describe('text-statistics', () => {
92107
sentences: 3,
93108
words: 14,
94109
words_no_puncs: 10,
110+
read_time: 3,
111+
words_uniques: 10,
112+
words_uniques_ci: 10,
95113
});
96-
expect(textStatistics('12 35')).toEqual({
114+
expect(textStatistics('12 35')).to.deep.eq({
97115
chars: 5,
98116
chars_digits: 4,
99117
chars_lower: 0,
@@ -105,8 +123,11 @@ describe('text-statistics', () => {
105123
sentences: 1,
106124
words: 2,
107125
words_no_puncs: 2,
126+
read_time: 0.6,
127+
words_uniques: 2,
128+
words_uniques_ci: 2,
108129
});
109-
expect(textStatistics(' 1 2 3. Other ')).toEqual({
130+
expect(textStatistics(' 1 2 3. Other ')).to.deep.eq({
110131
chars: 14,
111132
chars_digits: 3,
112133
chars_lower: 4,
@@ -118,6 +139,26 @@ describe('text-statistics', () => {
118139
sentences: 2,
119140
words: 4,
120141
words_no_puncs: 4,
142+
read_time: 1.2,
143+
words_uniques: 4,
144+
words_uniques_ci: 4,
145+
});
146+
147+
expect(textStatistics('Az az er')).to.deep.eq({
148+
chars: 8,
149+
chars_digits: 0,
150+
chars_lower: 5,
151+
chars_no_spaces: 6,
152+
chars_puncts: 0,
153+
chars_spaces: 2,
154+
chars_upper: 1,
155+
lines: 1,
156+
read_time: 0.8999999999999999,
157+
sentences: 1,
158+
words: 3,
159+
words_no_puncs: 3,
160+
words_uniques: 3,
161+
words_uniques_ci: 2,
121162
});
122163
});
123164
});

src/tools/text-statistics/text-statistics.service.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ export function getStringSizeInBytes(text: string) {
33
}
44

55
export function textStatistics(text: string) {
6+
const words_no_puncts = text.replace(/\p{P}/ug, '').trim().split(/\s+/);
7+
const read_word_per_minutes = 200;
68
return {
79
chars: text.length,
810
chars_no_spaces: text.replace(/\s+/ug, '').length,
@@ -12,7 +14,10 @@ export function textStatistics(text: string) {
1214
chars_puncts: text.replace(/[^\p{P}]/ug, '').length,
1315
chars_spaces: text.replace(/\S/ug, '').length,
1416
words: text.trim().split(/\s+/).length,
15-
words_no_puncs: text.replace(/\p{P}/ug, '').trim().split(/\s+/).length,
17+
read_time: words_no_puncts.length / read_word_per_minutes * 60,
18+
words_no_puncs: words_no_puncts.length,
19+
words_uniques: (new Set(words_no_puncts)).size,
20+
words_uniques_ci: (new Set(words_no_puncts.map(s => s.toLowerCase()))).size,
1621
sentences: (`${text} `).split(/\w\s*[\.!\?][\s\p{P}]*\s/u).filter(s => s && s?.length > 0).length,
1722
lines: text.split(/\r\n|\r|\n/).length,
1823
};

src/tools/text-statistics/text-statistics.vue

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<script setup lang="ts">
2+
import { formatMsDuration } from '../eta-calculator/eta-calculator.service';
23
import { getStringSizeInBytes, textStatistics } from './text-statistics.service';
34
import { formatBytes } from '@/utils/convert';
45
@@ -20,6 +21,14 @@ const stats = computed(() => textStatistics(text.value));
2021

2122
<n-divider />
2223

24+
<n-space mt-3>
25+
<n-statistic label="Unique Word count" :value="stats.words_uniques" />
26+
<n-statistic label="Unique Word count (case insensitive)" :value="stats.words_uniques_ci" />
27+
<n-statistic label="Read Time" :value="formatMsDuration(stats.read_time * 1000)" />
28+
</n-space>
29+
30+
<n-divider />
31+
2332
<n-space>
2433
<n-statistic label="Chars (no spaces)" :value="stats.chars_no_spaces" />
2534
<n-statistic label="Uppercase chars" :value="stats.chars_upper" />

0 commit comments

Comments
 (0)