예제 #1
0
def process_expression(expression, max_syll, singletons, verbose, tally=None):
    """Report cases of more than the number of allowed syllable combinations.

    Examples are:

    1) more than `max_syll` consecutive syllables without spaces or
    2) more than `singletons` consecutive single syllables.

    Return `tally` in any case:

        [0]: more than `max_syll` consecutive syllables without spaces
        [1]: more than `singletons` consecutive single syllables

    All results are printed to STDOUT."""
    if tally == None:
        tallly = [0, 0]
    divided = expression.split()
    consec_sing_count = 0
    for item in divided:
        try:
            length = len(S.separate(item, 'r'))
        except TypeError as e:
            if verbose:
                print('    Skipping {}.'.format(item))
            continue
        if length > 1:
            # Report any case of excessive singletons before resetting counter.
            if consec_sing_count > singletons:
                if verbose:
                    print('{}:    {} consecutive single syllables.'.
                            format(expression, consec_sing_count))
                tally[1] += 1
            consec_sing_count = 0
            # Check for excessive number of syllables.
            if length > max_syll:
                if verbose:
                    print('{}:    {} has syllables numbering {} > {}.'.
                            format(expression, item, length, max_syll))
                tally[0] += 1
        else:
            consec_sing_count += 1
    # Once more, report any case of excessive singletons.
    if consec_sing_count > singletons:
        if verbose:
            print('{}:    {} consecutive single syllables.'.
                    format(expression, consec_sing_count))
        tally[1] += 1
    return tally
def main(filename='pinyin_syllables_20130728.txt', output=False):
    '''Runs never-ending test of separate_pinyin.py.
    
    Use main() for standard syllables; use main('r') for all syllables,
    including those with epenthetic -r.
    
    Randomly generates a string of Pīnyīn up to six syllables long,
    separates it into component syllables using separate_pinyin.py,
    checks whether returned components are the same as the originals or not.

    Repeats this process until interrupted with control-c, and then reports the
    number of trials done. The process also terminates in the case of a failed
    trial.'''

    print(
        '''Tests of separate_pinyin.py run indefinitely until '''
        '''\n\n 1) an error is encountered or'''
        '''\n 2) control-c is pressed. '''
        '''\n\nIf the latter, statistics are then output.'''
        '''\n\nInventory of possible syllables includes''',
        end=' ')
    if filename == 'r':
        print('all those with -r.')
        filename = 'pinyin_syllables_with_r_20130728.txt'
    else:
        print('only those without added -r.')
    all_syllables = S.get_py_syllables(filename)
    all_vowels, _, _, _, _ = S.get_all_letters()
    counter = 0
    start_time = time.time()
    try:
        while True:
            counter += 1
            string_in_midprocess, original_list = get_random_string(
                all_syllables, all_vowels)
            finished_list = S.separate(string_in_midprocess, filename)
            if finished_list != original_list:
                print('Bad!', finished_list, '\n vs.', original_list)
            elif output:
                print('From {}\n    produced {}.'.format(
                    string_in_midprocess, finished_list))
    except KeyboardInterrupt:
        total_time = time.time() - start_time
        print('\n{:.0f} tests/second'.format(counter / total_time))
        print('Total tests run:', counter)
def main(filename='pinyin_syllables_20130728.txt', output=False):
    '''Runs never-ending test of separate_pinyin.py.
    
    Use main() for standard syllables; use main('r') for all syllables,
    including those with epenthetic -r.
    
    Randomly generates a string of Pīnyīn up to six syllables long,
    separates it into component syllables using separate_pinyin.py,
    checks whether returned components are the same as the originals or not.

    Repeats this process until interrupted with control-c, and then reports the
    number of trials done. The process also terminates in the case of a failed
    trial.'''
    
    print('''Tests of separate_pinyin.py run indefinitely until '''
            '''\n\n 1) an error is encountered or'''
            '''\n 2) control-c is pressed. '''
            '''\n\nIf the latter, statistics are then output.'''
            '''\n\nInventory of possible syllables includes''', end=' ')
    if filename == 'r':
        print('all those with -r.')
        filename = 'pinyin_syllables_with_r_20130728.txt'
    else:
        print('only those without added -r.')
    all_syllables = S.get_py_syllables(filename)
    all_vowels, _, _, _, _ = S.get_all_letters()
    counter = 0
    start_time = time.time()
    try:
        while True:
            counter += 1
            string_in_midprocess, original_list = get_random_string(
                    all_syllables, all_vowels)
            finished_list = S.separate(string_in_midprocess, filename)
            if finished_list != original_list:
                print('Bad!', finished_list, '\n vs.', original_list)
            elif output:
                print('From {}\n    produced {}.'.
                        format(string_in_midprocess, finished_list))
    except KeyboardInterrupt:
        total_time = time.time() - start_time
        print('\n{:.0f} tests/second'.
                format(counter/total_time))
        print('Total tests run:', counter)