<commit_before>import math
import sys
import types


class Point(object):
    def __init__(self, x=None, y=None):
        if x is not None:
            self.x = float(x)
        else:
            self.x = None
        if y is not None:
            self.y = float(y)
        else:
            self.y = None

    def __str__(self):
        return '(' + str(self.x) + ', ' + str(self.y) + ')'

    def __nonzero__(self):
        """ True if both x and y are defined """
        if self.x is not None and self.y is not None:
            return True
        else:
            return False

    def __eq__(self, p):
        if self.x == p.x and self.y == p.y:
            return True
        else:
            return False

    def __ne__(self, p):
        if self.x != p.x or self.y != p.y:
            return True
        else:
            return False

    def __sub__(self, q):
        return Point(self.x - q.x, self.y - q.y)

    def __add__(self, q):
        return Point(self.x + q.x, self.y + q.y)

    def dist(self, q):
        return math.sqrt((self.x - q.x) ** 2 + (self.y - q.y) ** 2)

    def signed_dist_to_line(self, p, q):
        """ Calculate signed distance from self to the line defined by
            p and q. Note that the function does not allow correct
            comparison of signs between lines parallel to either axis
            and lines oriented otherwise.
        """
        if p.y == q.y:
            return self.y - p.y
        elif p.x == q.x:
            return self.x - p.x
        else:
            a = 1 / (q.x - p.x)
            b = -1 / (q.y - p.y)
            c = p.y / (q.y - p.y) - p.x / (q.x - p.x)
        return (a * self.x + b * self.y + c) / math.sqrt(a ** 2 + b ** 2)

    def is_within_polygon(self, pol):
        """  Determine whether point p is inside polygon;
             Uses the crossing number method => works only with simple
             polygons.
        """
        if not pol:
            return None
        cn = 0
        for n in range(-1, len(pol) - 1):
            if ((pol[n].y <= self.y < pol[n + 1].y)
                 or ((pol[n].y > self.y) and pol[n + 1].y <= self.y)):
                if (line_intersection(pol[n], pol[n + 1], self,
                                      Point(self.x - 1, self.y)).x > self.x):
                    cn += 1
        if cn % 2 == 1:
            return True
        else:
            return False

    def project_on_path(self, path):
        """ Determine the orthogonal projection of a point on a segmented path;
            Return projection point and first node of the path segment on which
            the point projects. If no projection is possible, return
            Point(None, None), None.
        """
        mindist = float(sys.maxint)
        project = Point(None, None)
        seg0 = None
        for n in range(0, len(path) - 1):
            u = Vec(self.x - path[n].x, self.y - path[n].y)
            v = Vec(path[n + 1].x - path[n].x, path[n + 1].y - path[n].y)
            d = abs(self.signed_dist_to_line(path[n], path[n + 1]))
            if ((u.project(v).dot(v) >= 0) and (u.project(v).dist(Point(0, 0))
                                                <= v.dist(Point(0, 0)))
                    and d < mindist):
                mindist = d
                project = u.project(v) + path[n]
                seg0 = n
        if project:
            for n in range(1, len(path) - 1):
                d = self.dist(path[n])
                if d < mindist:
                    mindist = d
                    project = path[n]
                    seg0 = n
        return project, seg0

    def project_on_path_or_endnode(self, path):
        """ Determine the orthogonal projection of a point on a segmented path;
            Return projection point and first node of the path segment on which
            the point projects. If no projection is possible, choose nearest
            endpoint as projection.
        """
        mindist = float(sys.maxint)
        project = Point(None, None)
        seg0 = None
        for n in range(0, len(path) - 1):
            u = Vec(self.x - path[n].x, self.y - path[n].y)
            v = Vec(path[n + 1].x - path[n].x, path[n + 1].y - path[n].y)
            d = abs(self.signed_dist_to_line(path[n], path[n + 1]))
            if ((u.project(v).dot(v) >= 0) and (u.project(v).dist(Point(0, 0))
                                                    <= v.dist(Point(0, 0)))
                 and d < mindist):
                mindist = d
                project = u.project(v) + path[n]
                seg0 = n
        for n in range(0, len(path)):
            d = self.dist(path[n])
            if d < mindist:
                mindist = d
                project = path[n]
                seg0 = n
        if seg0 == len(path):
            seg0 -= 1
        return project, seg0

    def project_on_closed_path(self, path):
        """ Determine the orthogonal projection of a point on a closed path;
            Return projection point and first node of the path segment on which
            the point projects.
        """
        mindist = float(sys.maxint)
        project = Point(None, None)
        seg0 = None
        for n in range(-1, len(path) - 1):
            u = Vec(self.x - path[n].x, self.y - path[n].y)
            v = Vec(path[n + 1].x - path[n].x, path[n + 1].y - path[n].y)
            d = abs(self.signed_dist_to_line(path[n], path[n + 1]))
            if ((u.project(v).dot(v) >= 0) and (u.project(v).dist(Point(0, 0))
                                                    <= v.dist(Point(0, 0)))
                 and d < mindist):
                mindist = d
                project = u.project(v) + path[n]
                seg0 = n
        if project:
            for n in range(0, len(path)):
                d = self.dist(path[n])
                if d < mindist:
                    mindist = d
                    project = path[n]
                    seg0 = n
        for n in range(0, len(path)):
            d = self.dist(path[n])
            if d < mindist:
                mindist = d
                project = path[n]
                seg0 = n
        return project, seg0

    def lateral_dist(self, path):
        """ Determine lateral distance to center of path. If
            distance > 1, the projection of the point is on the
            extension of path.
        """
        subpath = SegmentedPath()
        # need node only
        foo, seg_path_center = \
            path.center_point().project_on_path_or_endnode(path)
        project, seg_project = self.project_on_path_or_endnode(path)
        subpath.extend([project, path.center_point()])
        if seg_path_center < seg_project:
            subpath.reverse()
        for n in range(min(seg_path_center, seg_project) + 1,
                       max(seg_path_center, seg_project)):
            subpath.insert(len(subpath) - 1, path[n])
        return subpath.length()

    def segment_crossing_number(self, path, refp):
        """ Return the number of times the line between a point p and a
            reference point refp crosses a segmented path (path)
        """
        cn = 0
        for n in range(0, len(path) - 1):
            d, t, u = line_intersection_with_params(self, refp, path[n],
                                                    path[n + 1])
            # is intersection between self and refp?
            if d and (0 <= t <= 1):
                # is intersection within path segment?
                if 0 <= u < 1:
                    cn += 1
                # if intersecting last segment node, count only if last
                # path segment; else it would be counted twice
                elif abs(u - 1) < sys.float_info.epsilon:
                    if n == len(path) - 2:
                        cn += 1
                    # if the line tangents the node between two
                    # segments, i e does not cross the path, regard it
                    # as no intersection; thus, decrement cn by 1 now
                    # (net change will be 0)
                    elif (path[n].signed_dist_to_line(path[n + 1], refp) *
                          path[n + 2].
                                  signed_dist_to_line(path[n + 1],
                                                          refp)) > 0:
                        cn -= 1
                elif (u < 0 and n == 0) or (u > 1 and n == len(path) - 2):
                    pass
        return cn

    def dist_to_segment(self, path, n):
        """Calculate distance from the point to segment n in path;
           First, determine if the orthogonal projection of the point
           on the path segment is between the nodes of ("on") the
           segment - if not, return distance to the closest node.
           Return distance and a flag which is set to 0 if "off" the
           first or last node of the path, otherwise to 1
        """
        u = Vec(self.x - path[n].x, self.y - path[n].y)
        v = Vec(path[n + 1].x - path[n].x, path[n + 1].y - path[n].y)
        if (u.project(v).dot(v) >= 0) and (u.project(v).dist(Point(0, 0)) <=
                                               v.dist(Point(0, 0))):
            return True, abs(self.signed_dist_to_line(path[n], path[n + 1]))
        else:  # So, not on segment.
            d0, d1 = abs(self.dist(path[n])), abs(self.dist(path[n + 1]))
            if n == 0 and d0 < d1:
                return False, d0
            elif n == len(path) - 2 and d1 < d0:
                return False, d1
            else:
                return True, min(d0, d1)

    def perpend_dist_closed_path(self, m, dont_care_if_on_or_off_seg=True):
        """" Calculate distance from the point to a closed path m
        """
        mindist = float(sys.maxint)
        on_m = False
        for n in range(-1, len(m) - 1):
            if (m[n].x != -1) and (m[n + 1].x != -1):
                on_this_seg, d = self.dist_to_segment(m, n)
                # smallest distance so far...
                if d <= mindist:
                    mindist = d
                    if on_this_seg or dont_care_if_on_or_off_seg:
                        # least distance and "on" segment (not
                        # completely true; see dist_to_segment())
                        on_m = True
                    else:
                        # least distance but "off" segment
                        on_m = False
        if not on_m:
            # shouldn't happen because m is closed
            return None
        return mindist

    def perpend_dist(self, m, negloc=None, posloc=None,
                     dont_care_if_on_or_off_seg=False):
        """" Calculate distance from the point to a path m; the
             polarity can be defined by negloc or posloc, which are
             points defined to have a negative and a positive distance
             to the path, respectively. If neither negloc nor posloc is
             defined, absolute distance is returned.
        """
        mindist = float(sys.maxint)
        on_m = False
        for n in range(0, len(m) - 1):
            if (m[n].x != -1) and (m[n + 1].x != -1):
                on_this_seg, d = self.dist_to_segment(m, n)
                if d <= mindist:
                    # smallest distance so far...
                    mindist = d
                    if on_this_seg or dont_care_if_on_or_off_seg:
                        # least distance and "on" segment (not
                        # completely true; see dist_to_segment())
                        on_m = True
                    else:
                        # least distance but "off" segment
                        on_m = False
        if not on_m:
            return None
        # If polarity (posloc or negloc) is defined, we say that points
        # on the positive side of the path have positive distances to
        # the path, while other points have negative distances. To
        # determine this, we count the number of path segments
        # dissected by the line between the particle and negloc
        # (posloc). Even (odd) number => the particle and negloc
        # (posloc) are on the same side of the path; odd number =>
        # different side.
        if (negloc is not None and
                        self.segment_crossing_number(m, negloc) % 2 == 0):
            mindist = -mindist
        elif (posloc is not None and
              self.segment_crossing_number(m, posloc) % 2 != 0):
            mindist = -mindist
        return mindist


# end of class Point


class Vec(Point):
    def __rmul__(self, l):
        """ Multiplication with scalar """
        if isinstance(l, types.IntType) or isinstance(l, types.FloatType):
            return Vec(l * self.x, l * self.y)
        else:
            raise TypeError('First operand not a scalar')

    def dot(self, v):
        """" Dot product """
        return self.x * v.x + self.y * v.y
<commit_msg>Move lateral_dist_to_point from core.Particle<commit_after>import math
import sys
import types


class Point(object):
    def __init__(self, x=None, y=None):
        if x is not None:
            self.x = float(x)
        else:
            self.x = None
        if y is not None:
            self.y = float(y)
        else:
            self.y = None

    def __str__(self):
        return '(' + str(self.x) + ', ' + str(self.y) + ')'

    def __nonzero__(self):
        """ True if both x and y are defined """
        if self.x is not None and self.y is not None:
            return True
        else:
            return False

    def __eq__(self, p):
        if self.x == p.x and self.y == p.y:
            return True
        else:
            return False

    def __ne__(self, p):
        if self.x != p.x or self.y != p.y:
            return True
        else:
            return False

    def __sub__(self, q):
        return Point(self.x - q.x, self.y - q.y)

    def __add__(self, q):
        return Point(self.x + q.x, self.y + q.y)

    def dist(self, q):
        return math.sqrt((self.x - q.x) ** 2 + (self.y - q.y) ** 2)

    def signed_dist_to_line(self, p, q):
        """ Calculate signed distance from self to the line defined by
            p and q. Note that the function does not allow correct
            comparison of signs between lines parallel to either axis
            and lines oriented otherwise.
        """
        if p.y == q.y:
            return self.y - p.y
        elif p.x == q.x:
            return self.x - p.x
        else:
            a = 1 / (q.x - p.x)
            b = -1 / (q.y - p.y)
            c = p.y / (q.y - p.y) - p.x / (q.x - p.x)
        return (a * self.x + b * self.y + c) / math.sqrt(a ** 2 + b ** 2)

    def is_within_polygon(self, pol):
        """  Determine whether point p is inside polygon;
             Uses the crossing number method => works only with simple
             polygons.
        """
        if not pol:
            return None
        cn = 0
        for n in range(-1, len(pol) - 1):
            if ((pol[n].y <= self.y < pol[n + 1].y)
                 or ((pol[n].y > self.y) and pol[n + 1].y <= self.y)):
                if (line_intersection(pol[n], pol[n + 1], self,
                                      Point(self.x - 1, self.y)).x > self.x):
                    cn += 1
        if cn % 2 == 1:
            return True
        else:
            return False

    def project_on_path(self, path):
        """ Determine the orthogonal projection of a point on a segmented path;
            Return projection point and first node of the path segment on which
            the point projects. If no projection is possible, return
            Point(None, None), None.
        """
        mindist = float(sys.maxint)
        project = Point(None, None)
        seg0 = None
        for n in range(0, len(path) - 1):
            u = Vec(self.x - path[n].x, self.y - path[n].y)
            v = Vec(path[n + 1].x - path[n].x, path[n + 1].y - path[n].y)
            d = abs(self.signed_dist_to_line(path[n], path[n + 1]))
            if ((u.project(v).dot(v) >= 0) and (u.project(v).dist(Point(0, 0))
                                                <= v.dist(Point(0, 0)))
                    and d < mindist):
                mindist = d
                project = u.project(v) + path[n]
                seg0 = n
        if project:
            for n in range(1, len(path) - 1):
                d = self.dist(path[n])
                if d < mindist:
                    mindist = d
                    project = path[n]
                    seg0 = n
        return project, seg0

    def project_on_path_or_endnode(self, path):
        """ Determine the orthogonal projection of a point on a segmented path;
            Return projection point and first node of the path segment on which
            the point projects. If no projection is possible, choose nearest
            endpoint as projection.
        """
        mindist = float(sys.maxint)
        project = Point(None, None)
        seg0 = None
        for n in range(0, len(path) - 1):
            u = Vec(self.x - path[n].x, self.y - path[n].y)
            v = Vec(path[n + 1].x - path[n].x, path[n + 1].y - path[n].y)
            d = abs(self.signed_dist_to_line(path[n], path[n + 1]))
            if ((u.project(v).dot(v) >= 0) and (u.project(v).dist(Point(0, 0))
                                                    <= v.dist(Point(0, 0)))
                 and d < mindist):
                mindist = d
                project = u.project(v) + path[n]
                seg0 = n
        for n in range(0, len(path)):
            d = self.dist(path[n])
            if d < mindist:
                mindist = d
                project = path[n]
                seg0 = n
        if seg0 == len(path):
            seg0 -= 1
        return project, seg0

    def project_on_closed_path(self, path):
        """ Determine the orthogonal projection of a point on a closed path;
            Return projection point and first node of the path segment on which
            the point projects.
        """
        mindist = float(sys.maxint)
        project = Point(None, None)
        seg0 = None
        for n in range(-1, len(path) - 1):
            u = Vec(self.x - path[n].x, self.y - path[n].y)
            v = Vec(path[n + 1].x - path[n].x, path[n + 1].y - path[n].y)
            d = abs(self.signed_dist_to_line(path[n], path[n + 1]))
            if ((u.project(v).dot(v) >= 0) and (u.project(v).dist(Point(0, 0))
                                                    <= v.dist(Point(0, 0)))
                 and d < mindist):
                mindist = d
                project = u.project(v) + path[n]
                seg0 = n
        if project:
            for n in range(0, len(path)):
                d = self.dist(path[n])
                if d < mindist:
                    mindist = d
                    project = path[n]
                    seg0 = n
        for n in range(0, len(path)):
            d = self.dist(path[n])
            if d < mindist:
                mindist = d
                project = path[n]
                seg0 = n
        return project, seg0

    def lateral_dist(self, path):
        """ Determine lateral distance to center of path. If
            distance > 1, the projection of the point is on the
            extension of path.
        """
        subpath = SegmentedPath()
        # need node only
        foo, seg_path_center = \
            path.center_point().project_on_path_or_endnode(path)
        project, seg_project = self.project_on_path_or_endnode(path)
        subpath.extend([project, path.center_point()])
        if seg_path_center < seg_project:
            subpath.reverse()
        for n in range(min(seg_path_center, seg_project) + 1,
                       max(seg_path_center, seg_project)):
            subpath.insert(len(subpath) - 1, path[n])
        return subpath.length()

    def segment_crossing_number(self, path, refp):
        """ Return the number of times the line between a point p and a
            reference point refp crosses a segmented path (path)
        """
        cn = 0
        for n in range(0, len(path) - 1):
            d, t, u = line_intersection_with_params(self, refp, path[n],
                                                    path[n + 1])
            # is intersection between self and refp?
            if d and (0 <= t <= 1):
                # is intersection within path segment?
                if 0 <= u < 1:
                    cn += 1
                # if intersecting last segment node, count only if last
                # path segment; else it would be counted twice
                elif abs(u - 1) < sys.float_info.epsilon:
                    if n == len(path) - 2:
                        cn += 1
                    # if the line tangents the node between two
                    # segments, i e does not cross the path, regard it
                    # as no intersection; thus, decrement cn by 1 now
                    # (net change will be 0)
                    elif (path[n].signed_dist_to_line(path[n + 1], refp) *
                          path[n + 2].
                                  signed_dist_to_line(path[n + 1],
                                                          refp)) > 0:
                        cn -= 1
                elif (u < 0 and n == 0) or (u > 1 and n == len(path) - 2):
                    pass
        return cn

    def dist_to_segment(self, path, n):
        """Calculate distance from the point to segment n in path;
           First, determine if the orthogonal projection of the point
           on the path segment is between the nodes of ("on") the
           segment - if not, return distance to the closest node.
           Return distance and a flag which is set to 0 if "off" the
           first or last node of the path, otherwise to 1
        """
        u = Vec(self.x - path[n].x, self.y - path[n].y)
        v = Vec(path[n + 1].x - path[n].x, path[n + 1].y - path[n].y)
        if (u.project(v).dot(v) >= 0) and (u.project(v).dist(Point(0, 0)) <=
                                               v.dist(Point(0, 0))):
            return True, abs(self.signed_dist_to_line(path[n], path[n + 1]))
        else:  # So, not on segment.
            d0, d1 = abs(self.dist(path[n])), abs(self.dist(path[n + 1]))
            if n == 0 and d0 < d1:
                return False, d0
            elif n == len(path) - 2 and d1 < d0:
                return False, d1
            else:
                return True, min(d0, d1)

    def perpend_dist_closed_path(self, m, dont_care_if_on_or_off_seg=True):
        """" Calculate distance from the point to a closed path m
        """
        mindist = float(sys.maxint)
        on_m = False
        for n in range(-1, len(m) - 1):
            if (m[n].x != -1) and (m[n + 1].x != -1):
                on_this_seg, d = self.dist_to_segment(m, n)
                # smallest distance so far...
                if d <= mindist:
                    mindist = d
                    if on_this_seg or dont_care_if_on_or_off_seg:
                        # least distance and "on" segment (not
                        # completely true; see dist_to_segment())
                        on_m = True
                    else:
                        # least distance but "off" segment
                        on_m = False
        if not on_m:
            # shouldn't happen because m is closed
            return None
        return mindist

    def perpend_dist(self, m, negloc=None, posloc=None,
                     dont_care_if_on_or_off_seg=False):
        """" Calculate distance from the point to a path m; the
             polarity can be defined by negloc or posloc, which are
             points defined to have a negative and a positive distance
             to the path, respectively. If neither negloc nor posloc is
             defined, absolute distance is returned.
        """
        mindist = float(sys.maxint)
        on_m = False
        for n in range(0, len(m) - 1):
            if (m[n].x != -1) and (m[n + 1].x != -1):
                on_this_seg, d = self.dist_to_segment(m, n)
                if d <= mindist:
                    # smallest distance so far...
                    mindist = d
                    if on_this_seg or dont_care_if_on_or_off_seg:
                        # least distance and "on" segment (not
                        # completely true; see dist_to_segment())
                        on_m = True
                    else:
                        # least distance but "off" segment
                        on_m = False
        if not on_m:
            return None
        # If polarity (posloc or negloc) is defined, we say that points
        # on the positive side of the path have positive distances to
        # the path, while other points have negative distances. To
        # determine this, we count the number of path segments
        # dissected by the line between the particle and negloc
        # (posloc). Even (odd) number => the particle and negloc
        # (posloc) are on the same side of the path; odd number =>
        # different side.
        if (negloc is not None and
                        self.segment_crossing_number(m, negloc) % 2 == 0):
            mindist = -mindist
        elif (posloc is not None and
              self.segment_crossing_number(m, posloc) % 2 != 0):
            mindist = -mindist
        return mindist

    def lateral_dist_to_point(self, p2, border):
        """ Determine lateral distance to a point p2 along profile
            border. Assume profile border is a closed path.
        """
        path = geometry.SegmentedPath()
        p2_project, p2_seg_project = p2.project_on_closed_path(border)
        project, seg_project = self.project_on_closed_path(border)
        path.extend([project, p2_project])
        if p2_seg_project < seg_project:
            path.reverse()
        for n in range(min(p2_seg_project, seg_project) + 1,
                       max(p2_seg_project, seg_project)):
            path.insert(len(path) - 1, border[n])
        length = path.length()
        return min(length, border.perimeter() - length)

# end of class Point


class Vec(Point):
    def __rmul__(self, l):
        """ Multiplication with scalar """
        if isinstance(l, types.IntType) or isinstance(l, types.FloatType):
            return Vec(l * self.x, l * self.y)
        else:
            raise TypeError('First operand not a scalar')

    def dot(self, v):
        """" Dot product """
        return self.x * v.x + self.y * v.y

{'fraction_non_alphanumeric': 0.1037327206297789, 'fraction_numerical': 0.010207540773663792, 'mean_word_length': 1.3478952953661125, 'pattern_counts': {'":': 0, '<': 54, '<?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': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4375891', 'n_tokens_mistral': 7956, 'n_tokens_neox': 7522, 'n_words': 2543}