Commit 55b440f3 authored by cclauss's avatar cclauss Committed by Chris Waterson
Browse files

Swivel: print(), basestring(), exception changes for Python 3 (#2249)

* from six.moves import xrange for Python 3

* from __future__ import print_function for Python 3

Also old style exception --> new style exception

* Define basestring for Python 3

Also remove unused import.

* from six import string_types for Python 3

* from __future__ import print_function

* print() --> print('')
parent 70097a35
...@@ -45,6 +45,8 @@ import sys ...@@ -45,6 +45,8 @@ import sys
import tensorflow as tf import tensorflow as tf
from six.moves import xrange
flags = tf.app.flags flags = tf.app.flags
flags.DEFINE_string('input', 'coocurrences.bin', 'Vocabulary file') flags.DEFINE_string('input', 'coocurrences.bin', 'Vocabulary file')
...@@ -131,6 +133,7 @@ def make_shard_files(coocs, nshards, vocab_sz): ...@@ -131,6 +133,7 @@ def make_shard_files(coocs, nshards, vocab_sz):
return (shard_files, row_sums) return (shard_files, row_sums)
def main(_): def main(_):
with open(FLAGS.vocab, 'r') as lines: with open(FLAGS.vocab, 'r') as lines:
orig_vocab_sz = sum(1 for _ in lines) orig_vocab_sz = sum(1 for _ in lines)
...@@ -193,5 +196,6 @@ def main(_): ...@@ -193,5 +196,6 @@ def main(_):
print('done!') print('done!')
if __name__ == '__main__': if __name__ == '__main__':
tf.app.run() tf.app.run()
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
"""Simple tool for inspecting nearest neighbors and analogies.""" """Simple tool for inspecting nearest neighbors and analogies."""
from __future__ import print_function
import re import re
import sys import sys
from getopt import GetoptError, getopt from getopt import GetoptError, getopt
...@@ -24,8 +25,8 @@ from vecs import Vecs ...@@ -24,8 +25,8 @@ from vecs import Vecs
try: try:
opts, args = getopt(sys.argv[1:], 'v:e:', ['vocab=', 'embeddings=']) opts, args = getopt(sys.argv[1:], 'v:e:', ['vocab=', 'embeddings='])
except GetoptError, e: except GetoptError as e:
print >> sys.stderr, e print(e, file=sys.stderr)
sys.exit(2) sys.exit(2)
opt_vocab = 'vocab.txt' opt_vocab = 'vocab.txt'
...@@ -55,21 +56,21 @@ while True: ...@@ -55,21 +56,21 @@ while True:
elif len(parts) == 3: elif len(parts) == 3:
vs = [vecs.lookup(w) for w in parts] vs = [vecs.lookup(w) for w in parts]
if any(v is None for v in vs): if any(v is None for v in vs):
print 'not in vocabulary: %s' % ( print('not in vocabulary: %s' % (
', '.join(tok for tok, v in zip(parts, vs) if v is None)) ', '.join(tok for tok, v in zip(parts, vs) if v is None)))
continue continue
res = vecs.neighbors(vs[2] - vs[0] + vs[1]) res = vecs.neighbors(vs[2] - vs[0] + vs[1])
else: else:
print 'use a single word to query neighbors, or three words for analogy' print('use a single word to query neighbors, or three words for analogy')
continue continue
if not res: if not res:
continue continue
for word, sim in res[:20]: for word, sim in res[:20]:
print '%0.4f: %s' % (sim, word) print('%0.4f: %s' % (sim, word))
print print()
...@@ -15,7 +15,9 @@ ...@@ -15,7 +15,9 @@
import mmap import mmap
import numpy as np import numpy as np
import os import os
import struct
from six import string_types
class Vecs(object): class Vecs(object):
def __init__(self, vocab_filename, rows_filename, cols_filename=None): def __init__(self, vocab_filename, rows_filename, cols_filename=None):
...@@ -41,8 +43,8 @@ class Vecs(object): ...@@ -41,8 +43,8 @@ class Vecs(object):
rows = np.matrix( rows = np.matrix(
np.frombuffer(rows_mm, dtype=np.float32).reshape(n, dim)) np.frombuffer(rows_mm, dtype=np.float32).reshape(n, dim))
# If column vectors were specified, then open them and add them to the row # If column vectors were specified, then open them and add them to the
# vectors. # row vectors.
if cols_filename: if cols_filename:
with open(cols_filename, 'r') as cols_fh: with open(cols_filename, 'r') as cols_fh:
cols_mm = mmap.mmap(cols_fh.fileno(), 0, prot=mmap.PROT_READ) cols_mm = mmap.mmap(cols_fh.fileno(), 0, prot=mmap.PROT_READ)
...@@ -71,7 +73,7 @@ class Vecs(object): ...@@ -71,7 +73,7 @@ class Vecs(object):
def neighbors(self, query): def neighbors(self, query):
"""Returns the nearest neighbors to the query (a word or vector).""" """Returns the nearest neighbors to the query (a word or vector)."""
if isinstance(query, basestring): if isinstance(query, string_types):
idx = self.word_to_idx.get(query) idx = self.word_to_idx.get(query)
if idx is None: if idx is None:
return None return None
......
...@@ -34,6 +34,7 @@ the scored human judgement. ...@@ -34,6 +34,7 @@ the scored human judgement.
""" """
from __future__ import print_function
import scipy.stats import scipy.stats
import sys import sys
from getopt import GetoptError, getopt from getopt import GetoptError, getopt
...@@ -42,8 +43,8 @@ from vecs import Vecs ...@@ -42,8 +43,8 @@ from vecs import Vecs
try: try:
opts, args = getopt(sys.argv[1:], '', ['embeddings=', 'vocab=']) opts, args = getopt(sys.argv[1:], '', ['embeddings=', 'vocab='])
except GetoptError, e: except GetoptError as e:
print >> sys.stderr, e print(e, file=sys.stderr)
sys.exit(2) sys.exit(2)
opt_embeddings = None opt_embeddings = None
...@@ -56,19 +57,20 @@ for o, a in opts: ...@@ -56,19 +57,20 @@ for o, a in opts:
opt_vocab = a opt_vocab = a
if not opt_vocab: if not opt_vocab:
print >> sys.stderr, 'please specify a vocabulary file with "--vocab"' print('please specify a vocabulary file with "--vocab"', file=sys.stderr)
sys.exit(2) sys.exit(2)
if not opt_embeddings: if not opt_embeddings:
print >> sys.stderr, 'please specify the embeddings with "--embeddings"' print('please specify the embeddings with "--embeddings"', file=sys.stderr)
sys.exit(2) sys.exit(2)
try: try:
vecs = Vecs(opt_vocab, opt_embeddings) vecs = Vecs(opt_vocab, opt_embeddings)
except IOError, e: except IOError as e:
print >> sys.stderr, e print(e, file=sys.stderr)
sys.exit(1) sys.exit(1)
def evaluate(lines): def evaluate(lines):
acts, preds = [], [] acts, preds = [], []
...@@ -85,6 +87,7 @@ def evaluate(lines): ...@@ -85,6 +87,7 @@ def evaluate(lines):
rho, _ = scipy.stats.spearmanr(acts, preds) rho, _ = scipy.stats.spearmanr(acts, preds)
return rho return rho
for filename in args: for filename in args:
with open(filename, 'r') as lines: with open(filename, 'r') as lines:
print '%0.3f %s' % (evaluate(lines), filename) print('%0.3f %s' % (evaluate(lines), filename))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment