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 3 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
20 changes: 12 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,25 +65,22 @@ 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;
/* 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;
score += this.model.get('UW3')?.get(sentence.substring(i - 1, i)) || 0;
if (score > 0) { result.push(i); continue; }
amitmarkel marked this conversation as resolved.
Show resolved Hide resolved
score += this.model.get('UW4')?.get(sentence.substring(i, i + 1)) || 0;
score += this.model.get('UW5')?.get(sentence.substring(i + 1, i + 2)) || 0;
score += this.model.get('UW6')?.get(sentence.substring(i + 2, i + 3)) || 0;
if (score > 0) { result.push(i); continue; }
score += this.model.get('BW1')?.get(sentence.substring(i - 2, i)) || 0;
score += this.model.get('BW2')?.get(sentence.substring(i - 1, i + 1)) || 0;
score += this.model.get('BW3')?.get(sentence.substring(i, i + 2)) || 0;
if (score > 0) { result.push(i); continue; }
score += this.model.get('TW1')?.get(sentence.substring(i - 3, i)) || 0;
score += this.model.get('TW2')?.get(sentence.substring(i - 2, i + 1)) || 0;
score += this.model.get('TW3')?.get(sentence.substring(i - 1, i + 2)) || 0;
Expand Down
Loading