Interactive Parsing

version 3 by Jon Ingold

  • Home page
  • Beginning
  • Previous
  • Next



  • Section - Compare Input Buffer against Dictionary Word and return a score

    Include (-

    Constant MATCH_RANGE = 1;

    Constant BASIC_UNIT_SCORE = 10;
    Constant FIRST_LETTER_BOOST = 2;
    Constant PRIORITY_BOOST = 1;

    Constant WORD_LENGTH_MAX_BOOST = 10;

    Constant BASE_LINE_MATCH_SCORE = 30;
    Constant PRIORITISE_THRESHOLD = 40;



    [ CompareWithIndexedDictionaryWord
        input_buffer dict_word_index
        dict_mult
        sc i j
        ;
        
        dict_mult = dictionary_word_pointer --> dict_word_index;
        if (dictionary_word_multiples-->(dict_mult) == 1) return -2;

        ! initialise the score
        sc = FIRST_LETTER_BOOST * BASIC_UNIT_SCORE * (dictionary_word_first_letter->dict_word_index == input_buffer->WORDSIZE);
        
        for (i = 0 : i < input_buffer-->0 : i++)
        {
            for (j = -MATCH_RANGE: j <= MATCH_RANGE : j++)
            {
                if (i + j >=0 && i + j < DictionaryIndexedWordLength(dict_word_index))
                {
                    if (dictionary_word_multiples-->(dict_mult + i + j) % PrimeFromCharacter(input_buffer->(i + WORDSIZE)) == 0)
                    {
                        ! this character features in this dictionary word multiple
                        sc = sc + BASIC_UNIT_SCORE;
                    }
                }
            }
        }
        
        
        ! boost for words we want to prioritise
        if (sc > PRIORITISE_THRESHOLD)
        {
            for (i = 0 : i < wds_boosted: i++)
                if (WordCompare(boost_wds-->i, WordFromIndex(dict_word_index)))
                    sc = sc + PRIORITY_BOOST * BASIC_UNIT_SCORE ;
        
        }

        ! boost for length of word correct
            sc = sc + WORD_LENGTH_MAX_BOOST - Abs(input_buffer-->0 - DictionaryIndexedWordLength(dict_word_index));
        
            
        #ifdef DEBUG_ADVANCE_PARSING;
            if (sc > 4)
            {
                glk_set_window((+main-window+).ref_number);
                print "Word ", (address) WordFromIndex(dict_word_index), " scored ", sc, ".^";
            }
        #endif;
        
        return sc;
    ];

    [ CompareWithDictionaryWord
        input_buffer
        dict_word;
        
        return CompareWithIndexedDictionaryWord(input_buffer, IndexFromWord(dict_word));

    ];

    [ MaxPossibleScore length;
        ! based on length of typed buffer
        ! each typed character can score in 3 multiples
        ! except the first and last character, which lose 1 group each
    ! Note the "-2" is specific to a MATCH_RANGE of 1. So sue me.

        return BASIC_UNIT_SCORE * (length * (2* MATCH_RANGE + 1) - 2 + FIRST_LETTER_BOOST + PRIORITY_BOOST) + WORD_LENGTH_MAX_BOOST;
        
    ];

    [ Abs x; if (x > 0) return x; return -x; ];

    -).