def createfrequencytables(document, n): """ Constructs a list of n frequency tables for…
Rate quality
Private research annotation
Sign in to add a private research label or note.
- User
def create_frequency_tables(document, n): """ Constructs a list of
nfrequency tables for an n-gram model. Each table captures character frequencies with increasing conditional dependencies.code- **Parameters**: - `document`: The text document used to train the model. - `n`: The maximum order of the n-gram model. - **Returns**: - A list of n frequency tables. """ - Assistant
- User
I am getting this error: Traceback (most recent call last): File "/Users//Desktop/CS 383/assignment-6-n-gram-complete-/main.py", line 2, in from NgramAutocomplete import create_frequency_tables, calculate_probability, predict_next_char File "/Users//Desktop/CS 383/assignment-6-n-gram-complete-/NgramAutocomplete.py", line 43, in tables = create_frequency_tables(document, n) File "/Users//Desktop/CS 383/assignment-6-n-gram-complete-/NgramAutocomplete.py", line 34, in create_frequency_tables
- Assistant
- User
this is the full error: Traceback (most recent call last): File "/Users//Desktop/CS 383/assignment-6-n-gram-complete-/main.py", line 2, in from NgramAutocomplete import create_frequency_tables, calculate_probability, predict_next_char File "/Users//Desktop/CS 383/assignment-6-n-gram-complete-/NgramAutocomplete.py", line 42, in tables = create_frequency_tables(document, n) File "/Users//Desktop/CS 383/assignment-6-n-gram-complete-/NgramAutocomplete.py", line 34, in create_frequency_tables total_count = sum(table[prefix].values()) ^^^^^^^^^^^^^^^^^^^^ AttributeError: 'int' object has no attribute 'values
- Assistant
- User
Is this correct: def calculate_probability(sequence, char, tables): """ Calculates the probability of observing a given character after a sequence of characters using the frequency tables.
code- **Parameters**: - `sequence`: The sequence of characters whose probability we want to compute. - `char`: The character whose probability of occurrence after the sequence is to be calculated. - `tables`: The list of frequency tables created by `create_frequency_tables()`. - **Returns**: - A probability value for the sequence and character. """ n = len(tables) seq_length = len(sequence) # Limit sequence to the last (n-1) characters if too long if seq_length > n - 1: sequence = sequence[-(n-1):] # Determine the right table to use based on the length of the sequence context = sequence current_table = tables[len(context) - 1] if context in current_table: # Get the probability of char given the context return ((current_table[context].get(char, 0))/len(context)) return 0 # Return 0 probability if context does not exist - Assistant