Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardizing reading input files with utf-8 encoding using io.open for both python 2 and 3. #184

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions utils/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# @Last Modified time: 2019-01-25 20:25:59
from __future__ import print_function
from __future__ import absolute_import
import io
import os
import sys
from .alphabet import Alphabet
from .functions import *
Expand Down Expand Up @@ -212,15 +214,14 @@ def initial_feature_alphabets(self):


def build_alphabet(self, input_file):
in_lines = open(input_file,'r').readlines()
in_lines = io.open(input_file, mode='r', encoding='utf-8').readlines()
for line in in_lines:
line = line.strip()
if len(line) > 2:
## if sentence classification data format, splited by \t
if self.sentence_classification:
pairs = line.strip().split(self.split_token)
pairs = line.split(self.split_token)
sent = pairs[0]
if sys.version_info[0] < 3:
sent = sent.decode('utf-8')
words = sent.split()
for word in words:
if self.number_normalized:
Expand All @@ -237,10 +238,8 @@ def build_alphabet(self, input_file):

## if sequence labeling data format i.e. CoNLL 2003
else:
pairs = line.strip().split()
pairs = line.split()
word = pairs[0]
if sys.version_info[0] < 3:
word = word.decode('utf-8')
if self.number_normalized:
word = normalize_word(word)
label = pairs[-1]
Expand Down Expand Up @@ -313,7 +312,7 @@ def write_decoded_results(self, predict_results, name):
sent_num = len(predict_results)
content_list = []
if name == 'raw':
content_list = self.raw_texts
content_list = self.raw_texts
elif name == 'test':
content_list = self.test_texts
elif name == 'dev':
Expand Down Expand Up @@ -344,6 +343,9 @@ def load(self,data_file):
self.__dict__.update(tmp_dict)

def save(self,save_file):
save_dir = os.path.dirname(save_file)
if not os.path.exists(save_dir):
os.makedirs(save_dir)
f = open(save_file, 'wb')
pickle.dump(self.__dict__, f, 2)
f.close()
Expand All @@ -353,7 +355,9 @@ def save(self,save_file):
def write_nbest_decoded_results(self, predict_results, pred_scores, name):
## predict_results : [whole_sent_num, nbest, each_sent_length]
## pred_scores: [whole_sent_num, nbest]
fout = open(self.decode_dir,'w')
if not os.path.exists(os.path.dirname(self.decode_dir)):
os.makedirs(os.path.dirname(self.decode_dir))
fout = open(self.decode_dir, 'w', encoding="utf-8")
sent_num = len(predict_results)
content_list = []
if name == 'raw':
Expand Down
20 changes: 8 additions & 12 deletions utils/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# @Last Modified time: 2019-02-14 12:23:52
from __future__ import print_function
from __future__ import absolute_import
import io
import sys
import numpy as np

Expand All @@ -20,7 +21,7 @@ def normalize_word(word):

def read_instance(input_file, word_alphabet, char_alphabet, feature_alphabets, label_alphabet, number_normalized, max_sent_length, sentence_classification=False, split_token='\t', char_padding_size=-1, char_padding_symbol = '</pad>'):
feature_num = len(feature_alphabets)
in_lines = open(input_file,'r', encoding="utf8").readlines()
in_lines = io.open(input_file, 'r', encoding="utf8").readlines()
instence_texts = []
instence_Ids = []
words = []
Expand All @@ -35,11 +36,10 @@ def read_instance(input_file, word_alphabet, char_alphabet, feature_alphabets, l
## if sentence classification data format, splited by \t
if sentence_classification:
for line in in_lines:
line = line.strip()
if len(line) > 2:
pairs = line.strip().split(split_token)
pairs = line.split(split_token)
sent = pairs[0]
if sys.version_info[0] < 3:
sent = sent.decode('utf-8')
original_words = sent.split()
for word in original_words:
words.append(word)
Expand Down Expand Up @@ -95,11 +95,10 @@ def read_instance(input_file, word_alphabet, char_alphabet, feature_alphabets, l
else:
### for sequence labeling data format i.e. CoNLL 2003
for line in in_lines:
line = line.strip()
if len(line) > 2:
pairs = line.strip().split()
pairs = line.split()
word = pairs[0]
if sys.version_info[0] < 3:
word = word.decode('utf-8')
words.append(word)
if number_normalized:
word = normalize_word(word)
Expand Down Expand Up @@ -196,7 +195,7 @@ def norm2one(vec):
def load_pretrain_emb(embedding_path):
embedd_dim = -1
embedd_dict = dict()
with open(embedding_path, 'r', encoding="utf8") as file:
with io.open(embedding_path, 'r', encoding="utf8") as file:
for line in file:
line = line.strip()
if len(line) == 0:
Expand All @@ -210,10 +209,7 @@ def load_pretrain_emb(embedding_path):
# assert (embedd_dim + 1 == len(tokens))
embedd = np.empty([1, embedd_dim])
embedd[:] = tokens[1:]
if sys.version_info[0] < 3:
first_col = tokens[0].decode('utf-8')
else:
first_col = tokens[0]
first_col = tokens[0]
embedd_dict[first_col] = embedd
return embedd_dict, embedd_dim

Expand Down