-
-
Notifications
You must be signed in to change notification settings - Fork 40
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
fix: fix custom wordbreaker output format for sil.km.gcc #265
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* | ||
Google Crawler 1.0 generated from template. | ||
|
||
This is a minimal lexical model source that uses a tab delimited wordlist. | ||
See documentation online at https://help.keyman.com/developer/ for | ||
additional parameters. | ||
|
@@ -10,59 +10,71 @@ | |
const source: LexicalModelSource = { | ||
format: 'trie-1.0', | ||
sources: ['wordlist.tsv'], | ||
wordBreaker: function (str) { | ||
const tokens = str.split(/\s|\u200b/); | ||
wordBreaker: function (str) { | ||
const whitespaceRegex = /\s|\u200b|\n|\r/; | ||
const tokens = str.split(whitespaceRegex); | ||
|
||
for(let i=0; i < tokens.length; i++) { | ||
const token = tokens[i]; | ||
if(token.length == 1) { | ||
continue; | ||
} | ||
for(let i=0; i < tokens.length; i++) { | ||
const token = tokens[i]; | ||
if(token.length == 0) { | ||
tokens.splice(i, 1); | ||
i--; | ||
continue; | ||
} else if(token.length == 1 && whitespaceRegex.test(token)) { | ||
tokens.splice(i, 1); | ||
i--; | ||
continue; | ||
} | ||
|
||
// Certain punctuation marks should be considered a separate token from the word they're next to. | ||
const punctuationMarks = ['«', '»', '$', '#' /* add extras here */]; | ||
const punctSplitIndices = []; | ||
// Certain punctuation marks should be considered a separate token from the word they're next to. | ||
const punctuationMarks = ['«', '»', '$', '#' /* add extras here */]; | ||
const punctSplitIndices = []; | ||
|
||
// Find if and where each mark exists within the token | ||
for(let i = 0; i < punctuationMarks.length; i++) { | ||
const split = token.indexOf(punctuationMarks[i]); | ||
if(split >= 0) { | ||
punctSplitIndices.push(split); | ||
// Find if and where each mark exists within the token | ||
for(let i = 0; i < punctuationMarks.length; i++) { | ||
const split = token.indexOf(punctuationMarks[i]); | ||
if(split >= 0) { | ||
punctSplitIndices.push(split); | ||
} | ||
} | ||
} | ||
|
||
// Sort and pick the earliest mark's location. If none exists, use -1. | ||
punctSplitIndices.sort(); | ||
const splitPoint = punctSplitIndices[0] === undefined ? -1 : punctSplitIndices[0]; | ||
// Sort and pick the earliest mark's location. If none exists, use -1. | ||
punctSplitIndices.sort(); | ||
const splitPoint = punctSplitIndices[0] === undefined ? -1 : punctSplitIndices[0]; | ||
|
||
if(splitPoint > -1) { | ||
const left = token.substring(0, splitPoint); // (0, -1) => '' | ||
const punct = token.substring(splitPoint, splitPoint+1); | ||
const right = token.substring(splitPoint+1); // Starting past the end of the string => '' | ||
if(splitPoint > -1) { | ||
const left = token.substring(0, splitPoint); // (0, -1) => '' | ||
const punct = token.substring(splitPoint, splitPoint+1); | ||
const right = token.substring(splitPoint+1); // Starting past the end of the string => '' | ||
|
||
if(left) { | ||
tokens.splice(i++, 0, left); | ||
} | ||
tokens.splice(i++, 1, punct); | ||
if(right) { | ||
tokens.splice(i, 0, right); | ||
if(left) { | ||
tokens.splice(i++, 0, left); | ||
} | ||
tokens.splice(i++, 1, punct); | ||
if(right) { | ||
tokens.splice(i, 0, right); | ||
} | ||
// Ensure that the next iteration puts `i` immediately after the punctuation token... even if | ||
// there was a `right` portion, as it may have extra marks that also need to be spun off. | ||
i--; | ||
} | ||
// Ensure that the next iteration puts `i` immediately after the punctuation token... even if | ||
// there was a `right` portion, as it may have extra marks that also need to be spun off. | ||
i--; | ||
} | ||
} | ||
return tokens.map(function(token) { | ||
|
||
let latestIndex = 0; | ||
return tokens.map(function(token) { | ||
const start = str.indexOf(token, latestIndex); | ||
latestIndex = start + token.length; | ||
Comment on lines
+63
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The key part of the fix: we should track the furthest index reached while iterating over each token and skip past it when determining the original position of the next token to be processed. This fixes handling for any duplicate-string cases. |
||
return { | ||
left: str.indexOf(token), | ||
start: str.indexOf(token), | ||
right: str.indexOf(token) + token.length, | ||
end: str.indexOf(token) + token.length, | ||
left: start, | ||
start: start, | ||
right: start + token.length, | ||
end: start + token.length, | ||
length: token.length, | ||
text: token | ||
} | ||
}); | ||
}, | ||
punctuation: { | ||
}, | ||
punctuation: { | ||
insertAfterWord: "\u200B" | ||
} | ||
}; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prevents empty tokens from appearing in the middle of context, especially should excessive whitespaces exist between words. Something like
the quick brown fox
should appear to skip all spaces between tokens at once, not in multiple steps.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the custom wordbreaker for sil.km.cnd.model.ts need a similar fix?
lexical-models/release/sil/sil.km.cnd/source/sil.km.cnd.model.ts
Lines 13 to 25 in a90fec5