-
Notifications
You must be signed in to change notification settings - Fork 5
/
relations.py
52 lines (45 loc) · 1.82 KB
/
relations.py
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
from smart_open import smart_open
import sys
import csv
class Relations(object):
"""Class to stream relations from a tsv-like file."""
def __init__(self, file_path, reverse, encoding='utf8', delimiter='\t'):
"""Initialize instance from file containing a pair of nodes (a relation) per line.
Parameters
----------
file_path : str
Path to file containing a pair of nodes (a relation) per line, separated by `delimiter`.
reverse: bool
Whether input csv file has edges (u,v) swapped or not.
encoding : str, optional
Character encoding of the input file.
delimiter : str, optional
Delimiter character for each relation.
"""
self.file_path = file_path
self.reverse = reverse
self.encoding = encoding
self.delimiter = delimiter
def __iter__(self):
"""Streams relations from self.file_path decoded into unicode strings.
Yields
-------
2-tuple (unicode, unicode)
Relation from input file.
"""
with smart_open(self.file_path) as file_obj:
if sys.version_info[0] < 3:
lines = file_obj
else:
lines = (l.decode(self.encoding) for l in file_obj)
# csv.reader requires bytestring input in python2, unicode input in python3
reader = csv.reader(lines, delimiter=self.delimiter)
for row in reader:
if sys.version_info[0] < 3:
row = [value.decode(self.encoding) for value in row]
(u,v) = tuple(row) # Swap line in the csv file because we want the correct edge direction.
assert u != v
if self.reverse:
yield (v,u)
else:
yield (u,v)