Skip to content
This repository has been archived by the owner on Jul 12, 2021. It is now read-only.

Commit

Permalink
Improve word count accuracy
Browse files Browse the repository at this point in the history
- Fixes punctuation marks being counted as words
- Fixes counting of CJK characters
  • Loading branch information
samuelmeuli committed Jan 21, 2020
1 parent a37b825 commit 70f1ddd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
"remark-stringify": "^7.0.3",
"semver": "^6.1.2",
"strip-markdown": "^3.1.0",
"type-fest": "^0.8.1"
"type-fest": "^0.8.1",
"word-count": "^0.2.2"
},
"devDependencies": {
"@babel/core": "^7.7.7",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import moment from "moment";
import React, { PureComponent, ReactNode } from "react";
import countWords from "word-count";

import { getLang } from "../../../../electron/ipcRenderer/senders";
import { momentIndex } from "../../../../utils/dateFormat";
Expand All @@ -24,13 +25,6 @@ interface Stats {
}

export default class StatsOverlay extends PureComponent<Props, {}> {
/**
* Count and return the number of words in the provided text
*/
static countWords(text: string): number {
return text.split(" ").filter((word): boolean => word !== "").length;
}

/**
* Return a string representation of the provided number, with thousands separators and at most
* one digit after the decimal point
Expand Down Expand Up @@ -71,7 +65,7 @@ export default class StatsOverlay extends PureComponent<Props, {}> {
}

// Count number of words in title and text, add to total
nrWords += StatsOverlay.countWords(title) + StatsOverlay.countWords(text);
nrWords += countWords(`${title}\n${text}`);

prevDate = date;
});
Expand Down
5 changes: 5 additions & 0 deletions src/renderer/d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ declare module "*.svg" {
declare module "markdown-draft-js";

declare module "strip-markdown";

// TODO: Remove once https://github.com/yuehu/word-count/pull/5 is merged
declare module "word-count" {
export default function count(data: string): number;
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11780,6 +11780,11 @@ widest-line@^2.0.0:
dependencies:
string-width "^2.1.1"

word-count@^0.2.2:
version "0.2.2"
resolved "https://registry.yarnpkg.com/word-count/-/word-count-0.2.2.tgz#699192fca6829fe936d41cb0d95db9248c570451"
integrity sha1-aZGS/KaCn+k21Byw2V25JIxXBFE=

word-wrap@~1.2.3:
version "1.2.3"
resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.3.tgz#610636f6b1f703891bd34771ccb17fb93b47079c"
Expand Down

0 comments on commit 70f1ddd

Please sign in to comment.