#-*- coding:utf-8 -*-
"""mark_lsunknown.py
"""
import sys,re,codecs

class Unknown(object):
 def __init__(self,line):
  line = line.rstrip('\r\n')
  parts = line.split('\t')  # tab-delimited
  self.ilinestr,self.meta,self.ls = parts
  self.iline = int(self.ilinestr)
  
  
def init_unks(filein):
 with codecs.open(filein,"r","utf-8") as f:
  recs = [Unknown(x) for x in f]
 print(len(recs),'unknown ls read from',filein)
 # git dictionary based on iline. Take into account 'duplicates'
 d = {}
 for rec in recs:
  iline = rec.iline
  if iline not in d:
   d[iline] = []
  d[iline].append(rec)
 return recs,d

def mark_unks(lines,unks): 
 newlines = []
 for line in lines:
  newlines.append(line)
 # revise newlines based on unks
 for rec in unks:
  iline = rec.iline
  ls = rec.ls
  old = newlines[iline]
  new = old.replace(ls,'**'+ls+'**')
  newlines[iline] = new
  if False: # dbg
   print('iline=%s, ls=%s' % (iline,ls))
   print('old:%s' % old)
   print()
   print('new:%s' % new)
   exit(1)
 return newlines

if __name__=="__main__":
 filein = sys.argv[1] #  xxx.txt (path to digitization of xxx)
 fileunk = sys.argv[2] # lsunknown file generated by lsextract_all
 fileout = sys.argv[3] # marked revision of xxx.txt
 
 unks,dunks = init_unks(fileunk)
 with codecs.open(filein,"r","utf-8") as f:
  lines = [x.rstrip('\r\n') for x in f]
 newlines = mark_unks(lines,unks)
 # write newlines
 with codecs.open(fileout,"w","utf-8") as f:
  for out in newlines:
   f.write(out+'\n')
 print(len(newlines),"lines written to",fileout)

 
