Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface Sentence {
- start: number
- end: number
- }
- const outputExample = [
- { start: 0, end: 28 },
- { start: 29, end: 117 },
- { start: 118, end: 145 },
- { start: 146, end: 196 },
- { start: 197, end: 229 },
- { start: 230, end: 233 },
- { start: 234, end: 303 }
- ]
- /**
- * Implement a function that takes a text as input and returns an array of
- * sentence ranges.
- *
- * Your solution will be validated against an extended data set :)
- */
- export function splitter(text: string): Sentence[] {
- let output = [];
- let start = 0, end = 0, lvlBrackets = 0;
- const regex = new RegExp('[ A-Z]+');
- const spaces = new RegExp('\\s');
- for(let i = 0; i < text.length; i++) {
- if(text[i] == '"') {
- if(lvlBrackets == 1) {
- lvlBrackets = 0;
- let next_symbol = i + 1;
- while(spaces.test(text[next_symbol])) {
- next_symbol += 1;
- }
- if (regex.test(text[next_symbol]) {
- output.push({start, end: i+1});
- start = next_symbol;
- continue;
- }
- }
- else {
- lvlBrackets = 1;
- }
- }
- else if(i == text.length - 1 ||
- (
- (text[i] == '.' || text[i] == '?' || text[i] == '!' ) &&
- spaces.test(text[i+1]) &&
- // (i + 2 >= text.length || (text[i+2] !== ' ' && text[i+2] !== '.')) &&
- // text[i+1] !== '.' &&
- !lvlBrackets &&
- !(text[i-2] == 'M' && text[i-1] == 's') &&
- !(text[i-2] == 'M' && text[i-1] == 'r') &&
- !(text[i-3] == 'M' && text[i-2] == 'r' && text[i-1] != 's') &&
- !(text[i-4] == 'M' && text[i-3] == 'i' && text[i-2] == 's' && text[i-1] != 's')
- )
- ) {
- end = i + 1;
- output.push({start, end});
- start = i + 2;
- while(spaces.test(text[start])) {
- start += 1;
- }
- end = 0;
- }
- }
- for(let j in output) {
- let t = "";
- for(let k = output[j].start; k <= output[j].end; k++) {
- t+=text[k];
- }
- console.log(t);
- console.log("+++++++++++++");
- }
- return output;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement