Skip to content

Commit

Permalink
Fix for Python 2.7 under Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
UKP committed Jul 11, 2017
1 parent 2ced10a commit b94c7f6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
11 changes: 9 additions & 2 deletions 2017-07_Seminar/Session 1 - SENNA/code for NER/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,15 @@ def getCasing(word, caseLookup):
else:
print(embeddingsPath, "does not exist. Please provide pre-trained embeddings")
exit()

fEmbeddings = gzip.open(embeddingsPath, 'rt') if embeddingsPath.endswith('.gz') else open(embeddingsPath)

if embeddingsPath.endswith('.gz'):
try:
fEmbeddings = gzip.open(embeddingsPath, "rt")
except ValueError:
# Workaround for Python 2.7 under Windows
fEmbeddings = gzip.open(embeddingsPath, "r")
else:
fEmbeddings = open(embeddingsPath)

for line in fEmbeddings:
split = line.strip().split(" ")
Expand Down
11 changes: 9 additions & 2 deletions 2017-07_Seminar/Session 1 - SENNA/code for POS/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,15 @@ def getCasing(word, caseLookup):
print(embeddingsPath, "does not exist. Please provide pre-trained embeddings")
exit()

fEmbeddings = gzip.open(embeddingsPath, 'rt') if embeddingsPath.endswith('.gz') else open(embeddingsPath)

if embeddingsPath.endswith('.gz'):
try:
fEmbeddings = gzip.open(embeddingsPath, "rt")
except ValueError:
# Workaround for Python 2.7 under Windows
fEmbeddings = gzip.open(embeddingsPath, "r")
else:
fEmbeddings = open(embeddingsPath)

for line in fEmbeddings:
split = line.strip().split(" ")
word = split[0]
Expand Down
11 changes: 9 additions & 2 deletions 2017-07_Seminar/Session 2 - Sentence CNN/code/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,15 @@ def readFile(filepath):
exit()

# :: Load the pre-trained embeddings file ::
fEmbeddings = gzip.open(embeddingsPath, 'rt') if embeddingsPath.endswith('.gz') else open(embeddingsPath)

if embeddingsPath.endswith('.gz'):
try:
fEmbeddings = gzip.open(embeddingsPath, "rt")
except ValueError:
# Workaround for Python 2.7 under Windows
fEmbeddings = gzip.open(embeddingsPath, "r")
else:
fEmbeddings = open(embeddingsPath)

print("Load pre-trained embeddings file")
for line in fEmbeddings:
split = line.strip().split(" ")
Expand Down
11 changes: 9 additions & 2 deletions 2017-07_Seminar/Session 3 - Relation CNN/code/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,15 @@ def getWordIdx(token, word2Idx):
exit()

# :: Load the pre-trained embeddings file ::
fEmbeddings = gzip.open(embeddingsPath, 'rt') if embeddingsPath.endswith('.gz') else open(embeddingsPath)

if embeddingsPath.endswith('.gz'):
try:
fEmbeddings = gzip.open(embeddingsPath, "rt")
except ValueError:
# Workaround for Python 2.7 under Windows
fEmbeddings = gzip.open(embeddingsPath, "r")
else:
fEmbeddings = open(embeddingsPath)

print("Load pre-trained embeddings file")
for line in fEmbeddings:
split = line.strip().split(" ")
Expand Down

0 comments on commit b94c7f6

Please sign in to comment.