-
Notifications
You must be signed in to change notification settings - Fork 0
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
ws-irc3sp: Split value into sentences #5
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"info": { | ||
"version": "1.0.0" | ||
"version": "1.1.0" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const LETTERS = "ABCDEFHIJKLMNOPQRSTUVWXYZ"; | ||
const SENTENCE_ENDING = ".?!"; | ||
|
||
const sentences = (data, feed, ctx) => { | ||
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. could be moved to a generic package for reuse and documentation 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. and tested ;-) 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.
Yes, but which one ? 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. @ezs/basics contains TXTParser which allows to split text into segments, we can imagine adding TXTSentences which splits into sentences. 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. It was eventually integrated into |
||
if (ctx.isLast()) { | ||
return feed.close(); | ||
} | ||
|
||
let value = data?.value; | ||
if (Array.isArray(value)) { | ||
if (value.length === 1) { | ||
value = value[0]; | ||
} | ||
} | ||
if (typeof value !== 'string') { | ||
return feed.send({ ...data, value }); | ||
} | ||
|
||
value = value.split("").reduce((a, c) => { | ||
const currentSentence = a.slice(-1); | ||
const [prev1, prev2] = a.slice(-1)[0].slice(-2); | ||
if (SENTENCE_ENDING.includes(c)) { | ||
if (c !== ".") { | ||
return [...a.slice(0, -1), currentSentence + c, " "]; | ||
} | ||
|
||
if (prev1 !== " ") { | ||
return [...a.slice(0, -1), currentSentence + c, " "]; | ||
} | ||
|
||
if (!LETTERS.includes(prev2)) { | ||
return [...a.slice(0, -1), currentSentence + c, " "]; | ||
} | ||
} | ||
return [...a.slice(0, -1), currentSentence + c] | ||
}, | ||
[" "]) | ||
.filter(sentence => sentence !== " ") | ||
.map(s => s.trimStart()); | ||
feed.send({ ...data, value }); | ||
}; | ||
|
||
module.exports = { | ||
sentences, | ||
}; |
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.
@parmentf fast but dangerous...
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.
Dangerous ?
Because of the lack of testing ?
Sure.
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.
bad structure causes a crash cf. tsouza/yajs#5