codebyzeb commited on
Commit
e2e17f0
1 Parent(s): b2b3b5d

Upload 3 files

Browse files
Files changed (3) hide show
  1. BabyLM.py +6 -3
  2. clean_data.py +2 -0
  3. tag_data.py +4 -1
BabyLM.py CHANGED
@@ -122,13 +122,16 @@ class BabyLM(datasets.GeneratorBasedBuilder):
122
 
123
  for filepath in filepaths:
124
  with open(filepath, encoding="utf-8") as f:
125
- filename = str(filepath.split("/")[-1])
126
  is_tags = False
127
  text = ""
128
- # Every other row contains POS tags
 
129
  for row in f:
 
 
 
130
  if is_tags:
131
- yield global_idx, {"text": text, "tagged_text": row, "filename": filepath}
132
  global_idx += 1
133
  is_tags = False
134
  else:
 
122
 
123
  for filepath in filepaths:
124
  with open(filepath, encoding="utf-8") as f:
 
125
  is_tags = False
126
  text = ""
127
+ filename = ""
128
+ # Every other row contains POS tags. First row is the filename (we can't use filepath since the file path changes upon caching)
129
  for row in f:
130
+ if filename == "":
131
+ filename = row
132
+ continue
133
  if is_tags:
134
+ yield global_idx, {"text": text, "tagged_text": row, "filename": filename}
135
  global_idx += 1
136
  is_tags = False
137
  else:
clean_data.py CHANGED
@@ -256,4 +256,6 @@ if __name__ == "__main__":
256
  new_file = file.replace('original', 'clean')
257
  os.makedirs(os.path.dirname(new_file), exist_ok=True)
258
  with open(new_file, 'w') as f:
 
 
259
  f.writelines(lines)
 
256
  new_file = file.replace('original', 'clean')
257
  os.makedirs(os.path.dirname(new_file), exist_ok=True)
258
  with open(new_file, 'w') as f:
259
+ # Save file name to file, so we can later recover the original file names
260
+ f.write(new_file.split('/')[-1] + '\n')
261
  f.writelines(lines)
tag_data.py CHANGED
@@ -68,7 +68,8 @@ def tokenize_lines(text, tokenizer):
68
 
69
  def get_tags_from_file(file):
70
  with open(file, 'r') as f:
71
- lines = f.read().splitlines()
 
72
 
73
  gold_tagged_lines = []
74
  pred_tagged_lines = []
@@ -96,6 +97,8 @@ def get_tags_from_file(file):
96
 
97
  def write_tagged_lines(filename, text, tagged_lines):
98
  with open(filename, 'w') as f:
 
 
99
  for line, tagged in zip(text, tagged_lines):
100
  f.write(line)
101
  f.write(' '.join([f'{token}__<label>__{tag}' for token, tag in tagged]) + '\n')
 
68
 
69
  def get_tags_from_file(file):
70
  with open(file, 'r') as f:
71
+ # Skip the first line, the file name
72
+ lines = f.read().splitlines()[1:]
73
 
74
  gold_tagged_lines = []
75
  pred_tagged_lines = []
 
97
 
98
  def write_tagged_lines(filename, text, tagged_lines):
99
  with open(filename, 'w') as f:
100
+ # Write the filename as the first line
101
+ f.write(filename.split('/')[-1] + '\n')
102
  for line, tagged in zip(text, tagged_lines):
103
  f.write(line)
104
  f.write(' '.join([f'{token}__<label>__{tag}' for token, tag in tagged]) + '\n')