Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion text/packages/chaff/src/detectors/number-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,15 @@ const isCountedNoun = (token: Token | undefined, figureNouns: readonly string[])
const endsInNameWord = (before: string, nameWords: readonly string[]): boolean =>
nameWords.length > 0 && new RegExp(`(?<![\\p{L}\\p{N}])(?:${alternation(nameWords)})\\s*$`, "u").test(before);

/** A figure glued to the word or the dot before it (php7.4-fpm, v2) is part of a name or a version, not a count. */
const GLUED_TO_NAME = /[\p{L}\p{N}._]$/u;

/**
* A figure right after the clock time that opens a log entry (- 17:59 4 pages are sent) opens a log line, not prose, so
* it is not a vote on how the document writes counts.
*/
const OPENS_LOG_ENTRY = /^\s*(?:[-*+]\s+)?\d{1,2}:\d{2}(?::\d{2})?\s+$/u;

/**
* A number before a plural noun (three files, 3 files), from two to the last number word the language lists. One is a
* pronoun as often as a count. A number word that opens the sentence is spelt out by every style, so it is not counted,
Expand All @@ -234,7 +243,8 @@ const englishCountMarks = (sentence: Sentence, words: NumberWords): NumberMark[]
const value = valueOf(token.surface, words.countWords);
if (value === undefined || value < SMALL_FROM || value > words.countWords.length || index === 0 || !isCountedNoun(tokens[index + 1], words.figureNouns))
return [];
if (endsInNameWord(sentence.text.slice(0, token.span.start - sentence.span.start), words.nameWords)) return [];
const before = sentence.text.slice(0, token.span.start - sentence.span.start);
if (endsInNameWord(before, words.nameWords) || GLUED_TO_NAME.test(before) || OPENS_LOG_ENTRY.test(before)) return [];
const style = /^\d+$/u.test(token.surface) ? "digits" : "words";
return [{ kind: value < LARGE_FROM ? "count-small" : "count-large", style, written: token.surface, offset: token.span.start }];
});
Expand Down
14 changes: 14 additions & 0 deletions text/packages/lang-en/lexicons/figure-noun.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,17 @@ entries:
- pattern: degrees
- pattern: percent
- pattern: points
# Units of data and of short time, which specifications write in figures (4 bits, 2 bytes, 4 microseconds).
- pattern: milliseconds
- pattern: microseconds
- pattern: nanoseconds
- pattern: bits
- pattern: bytes
- pattern: octets
- pattern: kilobytes
- pattern: megabytes
- pattern: gigabytes
- pattern: terabytes
- pattern: kilobits
- pattern: megabits
- pattern: gigabits
4 changes: 4 additions & 0 deletions text/test/test_number_style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ describe("numberMarksIn: English", () => {
["The fund paid $12500 to 3 members.", ["grouping:plain:12500", "count-small:digits:3"]],
["We counted 12500 deaf voters.", ["grouping:plain:12500"]],
["The fund paid $12500cafe to members.", ["grouping:plain:12500"]],
["The php7.4-fpm workers were busy.", []],
["- 17:59 4 pages are sent to the team.", []],
["- 17:59 The team gets 4 pages.", ["count-small:digits:4"]],
["The field is 4 bits wide and takes 2 bytes at 2 megabits a second.", []],
];
cases.forEach(([sentence, marks]) => {
it(`${sentence} → ${marks.join(", ") || "none"}`, () =>
Expand Down
Loading