Exemple #1
0
def safe_add(key, val, dict, label=''):
    ''' Adds a value to a Python dictionary, or fails and prints a message to the screen if it already exists.
    '''
    if key in dict:
        s = "{} Already has an item for key {}.".format(label, S.ascii(key))
        print(s)
    else:
        dict[key] = val
Exemple #2
0
def safe_add(key, val, dict, label=''):
    ''' Adds a value to a Python dictionary, or fails and prints a message to the screen if it already exists.
    '''
    if key in dict:
        s = "{} Already has an item for key {}.".format(label, S.ascii(key))
        print(s)
    else:
        dict[key] = val
Exemple #3
0
def push_ps_down(record):
    ''' If non-empty ps occurs above one or more sn fields within scope, copy it down and then delete it.
    
    If no sn lines are found below the ps line, insert an sn immediately above it. 
    (Formerly, when finished, it would delete ps if it's empty. That would help simplify messy data, 
    but it's not a good idea yet due to this FLEx issue: LT-10739) 
    '''

    rec = record.as_lists()
    offset = 0
    lxval = rec[0][1].strip()
    lxval = sfm.ascii(lxval)  #so non-unicode consoles won't crash

    # find each occurrence of ps and deal with it
    pss = record.find(PS, 1)
    for _m, ps in pss:
        i = ps + offset
        if not rec[i][1].strip():
            rec[i][1] = EMPTY_PS
        mrk, val = rec[i]
        val = val.strip()

        if not mrk == PS:
            raise Exception(
                "Programming error! Lost track of where the {} fields are.".
                format(PS))

        sns = record.find(SN, i + 1, EDGES)
        if sns and val:
            debug(
                'Push: For word {} near line {}, pushing {} (at {}) down to {} sense(s)'
                .format(lxval, record.location, PS, i, len(sns)))
            offset2 = 0
            for _m, j in sns:
                rec.insert(j + offset2 + 1, rec[i])
                offset2 += 1
            offset += offset2
            val = ''  # mark the original ps for deletion
        elif val:
            debug(
                'Insert: For word {} near line {}. No {} fields found. Inserting a new {} above {} instead.'
                .format(lxval, record.location, SN, SN, PS))
            rec.insert(i, [SN, EMPTY_SN])
            offset += 1

        if not val:
            # delete original ps
            #            print( 'Delete: for word {} near line {}, deleting ps (at {})'.format(lxval, record.location, i) )
            rec.pop(i)
            offset -= 1

    return record.as_string()
Exemple #4
0
def push_ps_down (record):
    ''' If non-empty ps occurs above one or more sn fields within scope, copy it down and then delete it.
    
    If no sn lines are found below the ps line, insert an sn immediately above it. 
    (Formerly, when finished, it would delete ps if it's empty. That would help simplify messy data, 
    but it's not a good idea yet due to this FLEx issue: LT-10739) 
    '''

    rec = record.as_lists()
    offset = 0
    lxval = rec[0][1].strip()
    lxval = sfm.ascii(lxval) #so non-unicode consoles won't crash
    
    # find each occurrence of ps and deal with it
    pss = record.find(PS, 1)
    for _m, ps in pss:
        i = ps + offset
        if not rec[i][1].strip():
            rec[i][1] = EMPTY_PS
        mrk, val = rec[i]
        val = val.strip()
        
        if not mrk == PS:
            raise Exception("Programming error! Lost track of where the {} fields are.".format(PS))
            
        sns = record.find(SN, i+1, EDGES)
        if sns and val:
            debug( 'Push: For word {} near line {}, pushing {} (at {}) down to {} sense(s)'.format(lxval, record.location, PS, i, len(sns)) )
            offset2 = 0
            for _m, j in sns:
                rec.insert(j + offset2 + 1, rec[i])
                offset2 += 1
            offset += offset2
            val = '' # mark the original ps for deletion
        elif val:
            debug('Insert: For word {} near line {}. No {} fields found. Inserting a new {} above {} instead.'.format(lxval, record.location, SN, SN, PS))
            rec.insert(i, [SN, EMPTY_SN])
            offset += 1

        if not val: 
            # delete original ps
#            print( 'Delete: for word {} near line {}, deleting ps (at {})'.format(lxval, record.location, i) )
            rec.pop(i)
            offset -= 1
    
    return record.as_string()