Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve typing #426

Merged
merged 6 commits into from
Dec 20, 2023
Merged
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
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
Loading