Skip to content

Commit

Permalink
Swivel: print(), basestring(), exception changes for Python 3 (tensor…
Browse files Browse the repository at this point in the history
…flow#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('')
  • Loading branch information
cclauss authored and waterson committed Aug 19, 2017
1 parent 70097a3 commit 55b440f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
4 changes: 4 additions & 0 deletions swivel/glove_to_shards.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@

import tensorflow as tf

from six.moves import xrange

flags = tf.app.flags

flags.DEFINE_string('input', 'coocurrences.bin', 'Vocabulary file')
Expand Down Expand Up @@ -131,6 +133,7 @@ def make_shard_files(coocs, nshards, vocab_sz):

return (shard_files, row_sums)


def main(_):
with open(FLAGS.vocab, 'r') as lines:
orig_vocab_sz = sum(1 for _ in lines)
Expand Down Expand Up @@ -193,5 +196,6 @@ def _floats(xs):

print('done!')


if __name__ == '__main__':
tf.app.run()
15 changes: 8 additions & 7 deletions swivel/nearest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

"""Simple tool for inspecting nearest neighbors and analogies."""

from __future__ import print_function
import re
import sys
from getopt import GetoptError, getopt
Expand All @@ -24,8 +25,8 @@

try:
opts, args = getopt(sys.argv[1:], 'v:e:', ['vocab=', 'embeddings='])
except GetoptError, e:
print >> sys.stderr, e
except GetoptError as e:
print(e, file=sys.stderr)
sys.exit(2)

opt_vocab = 'vocab.txt'
Expand Down Expand Up @@ -55,21 +56,21 @@
elif len(parts) == 3:
vs = [vecs.lookup(w) for w in parts]
if any(v is None for v in vs):
print 'not in vocabulary: %s' % (
', '.join(tok for tok, v in zip(parts, vs) if v is None))
print('not in vocabulary: %s' % (
', '.join(tok for tok, v in zip(parts, vs) if v is None)))

continue

res = vecs.neighbors(vs[2] - vs[0] + vs[1])

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

if not res:
continue

for word, sim in res[:20]:
print '%0.4f: %s' % (sim, word)
print('%0.4f: %s' % (sim, word))

print
print()
10 changes: 6 additions & 4 deletions swivel/vecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import mmap
import numpy as np
import os
import struct

from six import string_types


class Vecs(object):
def __init__(self, vocab_filename, rows_filename, cols_filename=None):
Expand All @@ -41,8 +43,8 @@ def __init__(self, vocab_filename, rows_filename, cols_filename=None):
rows = np.matrix(
np.frombuffer(rows_mm, dtype=np.float32).reshape(n, dim))

# If column vectors were specified, then open them and add them to the row
# vectors.
# If column vectors were specified, then open them and add them to the
# row vectors.
if cols_filename:
with open(cols_filename, 'r') as cols_fh:
cols_mm = mmap.mmap(cols_fh.fileno(), 0, prot=mmap.PROT_READ)
Expand Down Expand Up @@ -71,7 +73,7 @@ def similarity(self, word1, word2):

def neighbors(self, query):
"""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)
if idx is None:
return None
Expand Down
17 changes: 10 additions & 7 deletions swivel/wordsim.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"""

from __future__ import print_function
import scipy.stats
import sys
from getopt import GetoptError, getopt
Expand All @@ -42,8 +43,8 @@

try:
opts, args = getopt(sys.argv[1:], '', ['embeddings=', 'vocab='])
except GetoptError, e:
print >> sys.stderr, e
except GetoptError as e:
print(e, file=sys.stderr)
sys.exit(2)

opt_embeddings = None
Expand All @@ -56,19 +57,20 @@
opt_vocab = a

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)

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)

try:
vecs = Vecs(opt_vocab, opt_embeddings)
except IOError, e:
print >> sys.stderr, e
except IOError as e:
print(e, file=sys.stderr)
sys.exit(1)


def evaluate(lines):
acts, preds = [], []

Expand All @@ -85,6 +87,7 @@ def evaluate(lines):
rho, _ = scipy.stats.spearmanr(acts, preds)
return rho


for filename in args:
with open(filename, 'r') as lines:
print '%0.3f %s' % (evaluate(lines), filename)
print('%0.3f %s' % (evaluate(lines), filename))

0 comments on commit 55b440f

Please sign in to comment.