<commit_before>import re
from fractions import Fraction


class VideoRegionUtils(object):
  @staticmethod
  def validate_region_set(vid_duration_ms, regions):
    '''a set of regions are valid if and only if for each region, x, in
    the set, x is within bounds [0,vid_duration] and x does not
    intersect any other region and x is not equal to any other region'''
    # this is an n^2 algo that can be improved, but the number of
    # of regions to be processed is so small, its not really worth it
    in_bounds = lambda(x): x.time_a >= 0 and x.time_b <= vid_duration_ms
    for x in regions:
      if not in_bounds(x):
        raise RuntimeError(
            'region [{},{}] not in bounds [0,{}]'.
            format(x.time_a, x.time_b, vid_duration_ms))
      for y in regions:
        if x is not y:
          if x.intersects(y):
            raise RuntimeError(
                'region {} interesects existing region {}'.format(x, y))
          if x == y:
            raise RuntimeError(
                'region {} is the same as region {}'.format(x, y))
    return True

  @staticmethod
  def time_string_to_ms(time):
    '''valid time syntax:
    [hrs:mins:secs.xxx] OR [mins:secs.xxx] OR [secs.xxx]
    '''
    value_error = ValueError('invalid time syntax: {}'.format(time))
    h, m, s = 0, 0, 0
    valid_char_set = '0123456789:.'
    if time == '':
      raise value_error
    if time.count(':') > 2:
      raise value_error
    for c in time:
      if c not in valid_char_set:
        raise value_error
    v = time.split(':')
    if len(v) >= 1:
      if v[-1] != '':
        s = v[-1]
    if len(v) >= 2:
      if v[-2] != '':
        m = v[-2]
    if len(v) == 3:
      if v[-3] != '':
        h = v[-3]
    return (float(h) * 3600 + float(m) * 60 + float(s)) * 1000.0


class VideoSubRegion(object):
  '''describes a subregion in a video'''
  def __init__(self, ta, tb, fa=None, fb=None):
    self._time_a = ta
    self._time_b = tb
    self.validate(ta, tb)
    self.frame_a = fa
    self.frame_b = fb
    self.relative_pos_a = None
    self.relative_pos_b = None

  def validate(self, a, b):
    if a < 0 or a > b:
      raise ValueError('a ({}) must be nonzero and <= b ({})'.format(a, b))
    if b < a:
      raise ValueError('b ({}) must be >= than a ({})'.format(a, b))

  @property
  def time_a(self):
    return self._time_a

  @time_a.setter
  def time_a(self, val):
    self.validate(val, self.time_b)
    self._time_a = val

  @property
  def time_b(self):
    return self._time_b

  @time_b.setter
  def time_b(self, val):
    self.validate(self.time_a, val)
    self._time_b = val

  def intersects(self, other):
    '''returns if regions intersect each other, that is if either ends
    fall within each others ranges.
    '''
    if self == other:
      return True
    if self.time_a > other.time_a and self.time_a < other.time_b:
      return True
    if self.time_b > other.time_a and self.time_b < other.time_b:
      return True
    if self.frame_a > other.frame_a and self.frame_a < other.frame_a:
      return True
    if self.frame_b > other.frame_a and self.frame_b < other.frame_b:
      return True
    return False

  def __lt__(self, other):
    return self.time_a < other.time_a and self.time_b <= other.time_a

  def __gt__(self, other):
    return self.time_a >= other.time_b and self.time_b > other.time_b

  def __eq__(self, other):
    return self.time_a == other.time_a and self.time_b == other.time_b

  def __le__(self, other):
    return NotImplemented

  def __ge__(self, other):
    return NotImplemented

  def __ne__(self, other):
      return self.time_a != other.time_a and self.time_b != other.time_b
<commit_msg>cleanup region intersects code<commit_after>import re
from fractions import Fraction


class VideoRegionUtils(object):
  @staticmethod
  def validate_region_set(vid_duration_ms, regions):
    '''a set of regions are valid if and only if for each region, x, in
    the set, x is within bounds [0,vid_duration] and x does not
    intersect any other region and x is not equal to any other region'''
    # this is an n^2 algo that can be improved, but the number of
    # of regions to be processed is so small, its not really worth it
    in_bounds = lambda(x): x.time_a >= 0 and x.time_b <= vid_duration_ms
    for x in regions:
      if not in_bounds(x):
        raise RuntimeError(
            'region [{},{}] not in bounds [0,{}]'.
            format(x.time_a, x.time_b, vid_duration_ms))
      for y in regions:
        if x is not y:
          if x.intersects(y):
            raise RuntimeError(
                'region {} interesects existing region {}'.format(x, y))
          if x == y:
            raise RuntimeError(
                'region {} is the same as region {}'.format(x, y))
    return True

  @staticmethod
  def time_string_to_ms(time):
    '''valid time syntax:
    [hrs:mins:secs.xxx] OR [mins:secs.xxx] OR [secs.xxx]
    '''
    value_error = ValueError('invalid time syntax: {}'.format(time))
    h, m, s = 0, 0, 0
    valid_char_set = '0123456789:.'
    if time == '':
      raise value_error
    if time.count(':') > 2:
      raise value_error
    for c in time:
      if c not in valid_char_set:
        raise value_error
    v = time.split(':')
    if len(v) >= 1:
      if v[-1] != '':
        s = v[-1]
    if len(v) >= 2:
      if v[-2] != '':
        m = v[-2]
    if len(v) == 3:
      if v[-3] != '':
        h = v[-3]
    return (float(h) * 3600 + float(m) * 60 + float(s)) * 1000.0


class VideoSubRegion(object):
  '''describes a subregion in a video'''
  def __init__(self, ta, tb, fa=None, fb=None):
    self._time_a = ta
    self._time_b = tb
    self.validate(ta, tb)
    self.frame_a = fa
    self.frame_b = fb
    self.relative_pos_a = None
    self.relative_pos_b = None

  def validate(self, a, b):
    if a < 0 or a > b:
      raise ValueError('a ({}) must be nonzero and <= b ({})'.format(a, b))
    if b < a:
      raise ValueError('b ({}) must be >= than a ({})'.format(a, b))

  @property
  def time_a(self):
    return self._time_a

  @time_a.setter
  def time_a(self, val):
    self.validate(val, self.time_b)
    self._time_a = val

  @property
  def time_b(self):
    return self._time_b

  @time_b.setter
  def time_b(self, val):
    self.validate(self.time_a, val)
    self._time_b = val

  def intersects(self, other):
    '''returns if regions intersect each other, that is if either ends
    fall within each others ranges.
    '''
    if (self is other or
       (self.time_a == other.time_a and self.time_b == other.time_b) or
       (self.time_a > other.time_a and self.time_a < other.time_b) or
       (self.time_b > other.time_a and self.time_b < other.time_b) or
       (self.frame_a > other.frame_a and self.frame_a < other.frame_a) or
       (self.frame_b > other.frame_a and self.frame_b < other.frame_b)):
      return True
    return False

  def __lt__(self, other):
    return self.time_a < other.time_a and self.time_b <= other.time_a
  def __gt__(self, other):
    return self.time_a >= other.time_b and self.time_b > other.time_b
  def __eq__(self, other):
    return self.time_a == other.time_a and self.time_b == other.time_b
  def __le__(self, other):
    return NotImplemented
  def __ge__(self, other):
    return NotImplemented
  def __ne__(self, other):
      return self.time_a != other.time_a and self.time_b != other.time_b

{'fraction_non_alphanumeric': 0.12154845695722793, 'fraction_numerical': 0.010557661072008662, 'mean_word_length': 2.5320267686424476, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '768033', 'n_tokens_mistral': 2637, 'n_tokens_neox': 2548, 'n_words': 796}