cryptexcode commited on
Commit
7f553d0
1 Parent(s): fb75482

Update multiconer_v2.py

Browse files
Files changed (1) hide show
  1. multiconer_v2.py +32 -46
multiconer_v2.py CHANGED
@@ -287,52 +287,38 @@ class MultiCoNER2(datasets.GeneratorBasedBuilder):
287
  logger.info("⏳ Generating examples from = %s", filepath)
288
 
289
 
290
- content = open(filepath, 'r', encoding="utf-8").read().strip()
291
- lines = content.split('\n')
292
-
293
- items = [(4, {'id': '5fcb44ad-c1ef-4925-be27-008a76667b3b', 'tokens': ['রাস্তায়', 'রঙিন', 'লাইট', 'এবং', 'বাদশাহী', 'মসজিদ', 'এর', 'দৃশ্য', 'রয়েছে', 'এবং', 'এটি', 'রোশনাই', 'গেট', 'এর', 'নিকটে', 'রয়েছে'], 'ner_tags': ['O', 'O', 'O', 'O', 'B-Facility', 'I-Facility', 'O', 'O', 'O', 'O', 'O', 'B-OtherPROD', 'I-OtherPROD', 'O', 'O', 'O']}),
294
- (5, {'id': '44334ba4-0ebb-4001-83a7-525473eba921', 'tokens': ['ফাইল', ':', 'একে', '৭৪', 'বোল্ট।', 'জেপিজি', '|', 'একে-৭৪', 'বোল্ট', 'এবং', 'ফায়ারিং', 'পিন।'], 'ner_tags': ['O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-OtherPROD', 'O', 'O', 'O', 'O']}),
295
- (6, {'id': '8c87e2f6-cacc-43a8-bb0f-40aa3817af48', 'tokens': ['যখন', 'নিমজ্জিত', 'হয়', 'প্রতিটি', 'প্রোপেলার', 'একটি', 'বৈদ্যুতিক', 'মোটর', 'দ্বারা', 'চালিত', 'হয়েছিল।'], 'ner_tags': ['O', 'O', 'O', 'O', 'O', 'O', 'B-OtherPROD', 'I-OtherPROD', 'O', 'O', 'O']}),
296
- (7, {'id': '624ef8af-c8d6-4f28-b946-53344d721b6e', 'tokens': ['বেশ', 'কয়েকটি', 'অ্যাসকোমাইসেট', 'ছত্রাকের', 'বংশাণুসমগ্র', 'পুরোপুরি', 'ক্রমযুক্ত', 'হয়েছে।'], 'ner_tags': ['O', 'O', 'O', 'O', 'B-OtherPROD', 'O', 'O', 'O']})]
297
-
298
- items.append((6, {'id': 'asdas624ef8af-c8d6-4f28-b946-53344d721b6e', 'tokens': lines[:5], 'ner_tags': lines[5:10]} ))
299
- for it in items:
300
- yield it
301
-
302
- guid = -1
303
- s_id = None
304
- tokens = []
305
- ner_tags = []
306
-
307
- for line in lines:
308
- if line.startswith("# id"):
309
- s_id = line.split('\t')[0].split(' ')[-1].strip()
310
- guid += 1
311
- tokens = []
312
- ner_tags = []
313
- elif len(line.strip()) < 3:
314
- if s_id and len(tokens) >= 1 and len(tokens) == len(ner_tags):
315
- yield (guid, {
316
- "id": str(guid),
317
- "sample_id": str(s_id),
318
- "tokens": tokens,
319
- "ner_tags": ner_tags,
320
- })
321
- s_id = None
322
  tokens = []
323
  ner_tags = []
324
- else:
325
- continue
326
- else:
327
- # Separator is " _ _ "
328
- splits = line.split()
329
- if len(splits) > 2:
330
  tokens.append(splits[0].strip())
331
- ner_tags.append(splits[-1].strip())
332
- if s_id:
333
- yield (guid, {
334
- "id": str(guid),
335
- "sample_id": str(s_id),
336
- "tokens": tokens,
337
- "ner_tags": ner_tags,
338
- })
 
 
 
 
 
 
 
 
 
 
 
 
287
  logger.info("⏳ Generating examples from = %s", filepath)
288
 
289
 
290
+ with open(filepath) as f:
291
+ guid = -1
292
+ s_id = None
293
+ tokens = []
294
+ ner_tags = []
295
+
296
+ for line in f:
297
+ if line.strip().startswith("# id"):
298
+ s_id = line.split('\t')[0].split(' ')[-1].strip()
299
+ guid += 1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
300
  tokens = []
301
  ner_tags = []
302
+ elif '_ _' in line:
303
+ # Separator is " _ _ "
304
+ splits = line.split("_ _")
 
 
 
305
  tokens.append(splits[0].strip())
306
+ ner_tags.append(splits[1].strip())
307
+ elif len(line.strip()) == 0:
308
+ if s_id and len(tokens) >= 1 and len(tokens) == len(ner_tags):
309
+ yield guid, {
310
+ "id": s_id,
311
+ "tokens": tokens,
312
+ "ner_tags": ner_tags,
313
+ }
314
+ s_id = None
315
+ tokens = []
316
+ ner_tags = []
317
+ else:
318
+ continue
319
+ if s_id and len(tokens) >= 1 and len(tokens) == len(ner_tags):
320
+ yield guid, {
321
+ "id": s_id,
322
+ "tokens": tokens,
323
+ "ner_tags": ner_tags,
324
+ }