Advertisement
Guest User

Untitled

a guest
May 24th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. interface Sentence {
  2. start: number
  3. end: number
  4. }
  5.  
  6. const outputExample = [
  7. { start: 0, end: 28 },
  8. { start: 29, end: 117 },
  9. { start: 118, end: 145 },
  10. { start: 146, end: 196 },
  11. { start: 197, end: 229 },
  12. { start: 230, end: 233 },
  13. { start: 234, end: 303 }
  14. ]
  15.  
  16. /**
  17. * Implement a function that takes a text as input and returns an array of
  18. * sentence ranges.
  19. *
  20. * Your solution will be validated against an extended data set :)
  21. */
  22. export function splitter(text: string): Sentence[] {
  23. let output = [];
  24. let start = 0, end = 0, lvlBrackets = 0;
  25. const regex = new RegExp('[ A-Z]+');
  26. const spaces = new RegExp('\\s');
  27.  
  28. for(let i = 0; i < text.length; i++) {
  29. if(text[i] == '"') {
  30. if(lvlBrackets == 1) {
  31. lvlBrackets = 0;
  32. let next_symbol = i + 1;
  33. while(spaces.test(text[next_symbol])) {
  34. next_symbol += 1;
  35. }
  36. if (regex.test(text[next_symbol]) {
  37. output.push({start, end: i+1});
  38. start = next_symbol;
  39. continue;
  40. }
  41. }
  42. else {
  43. lvlBrackets = 1;
  44.  
  45. }
  46. }
  47. else if(i == text.length - 1 ||
  48. (
  49. (text[i] == '.' || text[i] == '?' || text[i] == '!' ) &&
  50. spaces.test(text[i+1]) &&
  51. // (i + 2 >= text.length || (text[i+2] !== ' ' && text[i+2] !== '.')) &&
  52. // text[i+1] !== '.' &&
  53. !lvlBrackets &&
  54. !(text[i-2] == 'M' && text[i-1] == 's') &&
  55. !(text[i-2] == 'M' && text[i-1] == 'r') &&
  56. !(text[i-3] == 'M' && text[i-2] == 'r' && text[i-1] != 's') &&
  57. !(text[i-4] == 'M' && text[i-3] == 'i' && text[i-2] == 's' && text[i-1] != 's')
  58. )
  59. ) {
  60. end = i + 1;
  61. output.push({start, end});
  62.  
  63. start = i + 2;
  64. while(spaces.test(text[start])) {
  65. start += 1;
  66. }
  67. end = 0;
  68. }
  69. }
  70. for(let j in output) {
  71. let t = "";
  72. for(let k = output[j].start; k <= output[j].end; k++) {
  73. t+=text[k];
  74. }
  75. console.log(t);
  76. console.log("+++++++++++++");
  77. }
  78. return output;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement