Skip to content

Commit

Permalink
Improve typing (#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
amitmarkel authored Dec 20, 2023
1 parent 39df6fb commit d2ca364
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions javascript/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
*/
export class Parser {
/** BudouX model data */
model;
private readonly model: Map<string, Map<string, number>>;
private readonly baseScore: number;

/**
* Constructs a BudouX parser.
Expand All @@ -29,6 +30,12 @@ export class Parser {
this.model = new Map(
Object.entries(model).map(([k, v]) => [k, new Map(Object.entries(v))])
);
this.baseScore =
-0.5 *
[...this.model.values()]
.map(group => [...group.values()])
.flat()
.reduce((prev, curr) => prev + curr, 0);
}

/**
Expand Down Expand Up @@ -58,15 +65,10 @@ export class Parser {
*/
parseBoundaries(sentence: string): number[] {
const result = [];
const baseScore =
-0.5 *
[...this.model.values()]
.map(group => [...group.values()])
.flat()
.reduce((prev, curr) => prev + curr, 0);

for (let i = 1; i < sentence.length; i++) {
let score = baseScore;
let score = this.baseScore;
// NOTE: Score values in models may be negative.
/* eslint-disable */
score += this.model.get('UW1')?.get(sentence.substring(i - 3, i - 2)) || 0;
score += this.model.get('UW2')?.get(sentence.substring(i - 2, i - 1)) || 0;
Expand Down

0 comments on commit d2ca364

Please sign in to comment.