import base
from blocks import Blocks
import re

class Sentences(base.Base):
    _type = 'sentences'
    child_class = Blocks

    @staticmethod
    def parse(text):
        #re.split('\!|\?|\. | \.',text)
        result = []
        prev = 0

        # we allow .09 as not end of sentences
        #for m in re.finditer('[\!\?]+|\.+(?:\s+|$|\?|\!)', text):
        for m in re.finditer('\.+(?:\s+|$)|(\.*)[\!\?]+(\.+(?:\s+|$))*', text):
            curr, _next = m.start(), m.end()
            # if prev position of delimiter < current - between exists text
            # at least 1 symbol.
            if prev < curr:
                node = text[prev:curr].strip()
                if node != '':
                    result.append(node)
            result.append(list( text[curr:_next].strip() ))
            prev = _next
        if len(text) > prev:
            result.append(text[prev:].strip())
        return result
import base
from blocks import Blocks
import re

class Sentences(base.Base):
    _type = 'sentences'
    child_class = Blocks

    @staticmethod
    def parse(text):
        _len = len(text)
        result = []
        prev = 0

        # we allow .09 as not end of sentences
        for m in re.finditer('[\!\?\.]+', text):
            curr, _next = m.start(), m.end()
            items = list( text[curr: _next].strip() )

            if (_len > _next) and not (text[_next] == ' '):
                # delete ending '.' if they not before space or end of string
                while (len(items) > 0) and (items[-1] == '.'):
                    items.pop()
                    _next = _next - 1

            if len(items) > 0:
                # if prev position of delimiter < current - between exists text
                # at least 1 symbol.
                if prev < curr:
                    node = text[prev:curr].strip()
                    if node != '':
                        result.append(node)
                result.append( items )
                prev = _next
        if _len > prev:
            result.append(text[prev:].strip())
        return result
Optimize from 5-6s to 2.9-3.0
Optimize from 5-6s to 2.9-3.0

  import base
  from blocks import Blocks
  import re
  
  class Sentences(base.Base):
      _type = 'sentences'
      child_class = Blocks
  
      @staticmethod
      def parse(text):
-         #re.split('\!|\?|\. | \.',text)
+         _len = len(text)
          result = []
          prev = 0
  
          # we allow .09 as not end of sentences
-         #for m in re.finditer('[\!\?]+|\.+(?:\s+|$|\?|\!)', text):
+         for m in re.finditer('[\!\?\.]+', text):
-         for m in re.finditer('\.+(?:\s+|$)|(\.*)[\!\?]+(\.+(?:\s+|$))*', text):
              curr, _next = m.start(), m.end()
+             items = list( text[curr: _next].strip() )
+ 
+             if (_len > _next) and not (text[_next] == ' '):
+                 # delete ending '.' if they not before space or end of string
+                 while (len(items) > 0) and (items[-1] == '.'):
+                     items.pop()
+                     _next = _next - 1
+ 
+             if len(items) > 0:
-             # if prev position of delimiter < current - between exists text
+                 # if prev position of delimiter < current - between exists text
-             # at least 1 symbol.
+                 # at least 1 symbol.
-             if prev < curr:
+                 if prev < curr:
-                 node = text[prev:curr].strip()
+                     node = text[prev:curr].strip()
-                 if node != '':
+                     if node != '':
-                     result.append(node)
+                         result.append(node)
-             result.append(list( text[curr:_next].strip() ))
+                 result.append( items )
-             prev = _next
+                 prev = _next
-         if len(text) > prev:
+         if _len > prev:
              result.append(text[prev:].strip())
          return result
Optimize from 5-6s to 2.9-3.0
## Code Before:
import base
from blocks import Blocks
import re

class Sentences(base.Base):
    _type = 'sentences'
    child_class = Blocks

    @staticmethod
    def parse(text):
        #re.split('\!|\?|\. | \.',text)
        result = []
        prev = 0

        # we allow .09 as not end of sentences
        #for m in re.finditer('[\!\?]+|\.+(?:\s+|$|\?|\!)', text):
        for m in re.finditer('\.+(?:\s+|$)|(\.*)[\!\?]+(\.+(?:\s+|$))*', text):
            curr, _next = m.start(), m.end()
            # if prev position of delimiter < current - between exists text
            # at least 1 symbol.
            if prev < curr:
                node = text[prev:curr].strip()
                if node != '':
                    result.append(node)
            result.append(list( text[curr:_next].strip() ))
            prev = _next
        if len(text) > prev:
            result.append(text[prev:].strip())
        return result
## Instruction:
Optimize from 5-6s to 2.9-3.0
## Code After:
import base
from blocks import Blocks
import re

class Sentences(base.Base):
    _type = 'sentences'
    child_class = Blocks

    @staticmethod
    def parse(text):
        _len = len(text)
        result = []
        prev = 0

        # we allow .09 as not end of sentences
        for m in re.finditer('[\!\?\.]+', text):
            curr, _next = m.start(), m.end()
            items = list( text[curr: _next].strip() )

            if (_len > _next) and not (text[_next] == ' '):
                # delete ending '.' if they not before space or end of string
                while (len(items) > 0) and (items[-1] == '.'):
                    items.pop()
                    _next = _next - 1

            if len(items) > 0:
                # if prev position of delimiter < current - between exists text
                # at least 1 symbol.
                if prev < curr:
                    node = text[prev:curr].strip()
                    if node != '':
                        result.append(node)
                result.append( items )
                prev = _next
        if _len > prev:
            result.append(text[prev:].strip())
        return result