NLP lab Manual (3)
NLP lab Manual (3)
def generate(symbol):
"""
Recursively generates a sentence fragment from the given symbol
using the PCFG grammar.
Parameters:
symbol (str): The non-terminal or terminal symbol to expand.
Returns:
str: The generated string from the grammar.
"""
# If the symbol is not in the grammar, it's assumed to be a
terminal.
if symbol not in grammar:
return symbol
productions = grammar[symbol]
# Unzip the production rules and their corresponding weights.
rules, weights = zip(*productions)
# Debug log: show the chosen production rule for the current non-
terminal.
print(f"Expanding '{symbol}' using rule: {chosen_rule}")
# Create trigrams
tri_grams = list(trigrams(words))
Args:
w1 (str): The first word.
w2 (str): The second word.
Returns:
str: The predicted next word.
"""
next_word = model[w1, w2]
if next_word:
predicted_word = max(next_word, key=next_word.get) # Choose
the most likely next word
return predicted_word
else:
return "No prediction available"
# Example usage
print("Next Word:", predict_next_word('the', 'stock'))
[nltk_data] Downloading package reuters to /root/nltk_data...
[nltk_data] Package reuters is already up-to-date!
[nltk_data] Downloading package punkt to /root/nltk_data...
[nltk_data] Package punkt is already up-to-date!
[nltk_data] Downloading package punkt_tab to /root/nltk_data...
[nltk_data] Package punkt_tab is already up-to-date!
Next Word: of
m = len(s1)
n = len(s2)
return dp[m][n]
# Example usage
string1 = "intention"
string2 = "execution"
distance1 = weighted_edit_distance_no_numpy(string1, string2,
ins_cost=1, del_cost=1, sub_cost=1)
print(f"Weighted edit distance between '{string1}' and '{string2}':
{distance1}")