Skip to content

Commit

Permalink
Merge pull request #127 from commonsense/orderedset
Browse files Browse the repository at this point in the history
Added orderedset to vectors/formats.py
  • Loading branch information
Rob Speer authored Dec 4, 2017
2 parents 9930974 + b5d90c4 commit acce22a
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions conceptnet5/vectors/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import gzip
import struct
import pickle
from ordered_set import OrderedSet
from .transforms import l1_normalize_columns, l2_normalize_rows, standardize_row_labels


Expand Down Expand Up @@ -251,3 +252,24 @@ def save_index_as_labels(index, label_filename):
with open(label_filename, 'w', encoding='utf-8') as out:
for label in index:
print(label, file=out)


def save_ordered_set(oset, filename):
"""
Save an OrderedSet object as a text file of words.
"""
with open(filename, 'w', encoding='utf-8') as out:
for word in oset:
print(word, file=out)


def load_ordered_set(filename):
"""
Load a set of words from a text file, and
represent them in an OrderedSet object.
"""
oset = OrderedSet()
for line in open(filename, encoding='utf-8'):
oset.append(line.rstrip('\n'))
return oset

0 comments on commit acce22a

Please sign in to comment.