File size: 1,833 Bytes
cb0ad2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Copyright (c) 2019-present, Zewen Chi
# All rights reserved.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import sys

def normalize(s:str, rule=0):

  if rule == 0:
    s = s.replace("\r", "")
    s = s.replace("\n", "")
    s = s.replace(" ", "")
    s = s.replace("\t", "")
    return s.upper()
  else:
    raise NotImplementedError


class Relation(object):

  def __init__(self, from_text, to_text, direction, from_id=0, to_id=0, no_blanks=0):
    self.from_text = from_text
    self.to_text = to_text
    self.direction = direction
    self.no_blanks = no_blanks
    self.from_id = from_id
    self.to_id = to_id
  
  def __eq__(self, rl):
    this_ft = normalize(self.from_text)
    this_tt = normalize(self.to_text)
    rl_ft = normalize(rl.from_text)
    rl_tt = normalize(rl.to_text)
    if len(this_ft) == 0 or len(this_tt) == 0 or \
       len(rl_ft) == 0 or len(rl_tt) == 0:
      print("Warning: Text comparison of 0-length strings after normalization",
        file=sys.stderr)
  
    return this_ft == rl_ft and this_tt == rl_tt and \
      self.direction == rl.direction and self.no_blanks == rl.no_blanks
  
  def equal(self, rl, cmp_blank=True):
    this_ft = normalize(self.from_text)
    this_tt = normalize(self.to_text)
    rl_ft = normalize(rl.from_text)
    rl_tt = normalize(rl.to_text)
    if len(this_ft) == 0 or len(this_tt) == 0 or \
       len(rl_ft) == 0 or len(rl_tt) == 0:
      print("Warning: Text comparison of 0-length strings after normalization",
        file=sys.stderr)

    return this_ft == rl_ft and this_tt == rl_tt and \
      self.direction == rl.direction and \
      (self.no_blanks == rl.no_blanks if cmp_blank else True)
  
  def __str__(self):
    return "%d:%d" % (self.direction, self.no_blanks)