When I enter this code
results = []
import spacy
from tqdm import tqdm
Load the spaCy model
nlp = spacy.load("en_core_web_lg")
Define the lists of lexical terms, verbs, modal verbs, adverbial phrases, and phrasal verbs
lexical_terms = ["Proposal", "Plan"]
verbs = ["Propose", "Plan""]
modal_verbs = ["Can", "Could"]
adverbial_phrases = ["In the long run", "As a temporary measure"
phrasal_verbs = ["Come up with", "Look into"]
Iterate through the sentences in your dataframe
for sentence in tqdm(df['sentence']):
# Process the sentence with spaCy
doc = nlp(sentence)
code
# Extract entities and check if they match any of the lexical terms in your list
for ent in doc.ents:
if ent.text in lexical_terms:
results.append(ent.text)
# Extract verbs and check
for token in doc:
if token.text in verbs:
# Extract the subject of the verb
subj = [tok for tok in token.subtree if tok.dep_ == "nsubj"]
if subj:
results.append(subj[0].text, token.text)
# Extract modal verbs and check if they match any of the modal verbs in your list
for token in doc:
if token.text in modal_verbs:
# Extract the subject of the modal verb
subj = [tok for tok in token.subtree if tok.dep_ == "nsubj"]
if subj:
results.append(subj[0].text, token.text)
# Extract adverbial phrases and check if they match any of the adverbial phrases in your list
for token in doc:
if token.text in adverbial_phrases:
# Extract the subject of the adverbial phrase
subj = [tok for tok in token.subtree if tok.dep_ == "nsubj"]
if subj:
results.append(subj[0].text, token.text)
# Extract phrasal verbs and check if they match any of the phrasal verbs in your list
for token in doc:
if token.text in phrasal_verbs:
# Extract the subject of the phrasal verb
subj = [tok for tok in token.subtree if tok.dep_ == "nsubj"]
if subj:
results.append(subj[0].text, token.text)
Assistant
The error is occurring because the append() method only takes one argument, but you are trying to give it two arguments subj[0].text and token.text. If you want to add multiple items to the list, you should use the extend() method or add them as a tuple.
Here's how you can fix it:
Option 1: Use extend() method
python
results.extend([subj[0].text, token.text])
Option 2: Add them as a tuple
python
results.append((subj[0].text, token.text))
Either of these options will resolve the TypeError you're encountering.
User
Can you give the correct code starting from '# Iterate through the sentences in your dataframe'
Assistant
Sure, here's the corrected code starting from '# Iterate through the sentences in your dataframe':
python
User
A few changes: 1) I want the 'lemmatized_sentence' column to be used for the extraction instead of the 'sentence'-colum; 2) I want the results to be printed (and then written out to 'policy options.json' in two pretty columns - a) the first column with the results extracted from the 'lemmatized_sentence'; the second with those same (non-lemmatized) results highlighted in a column that contains the full 'sentence' columnn of that row, displayed wrapped and left-aligned.
Assistant
Here's the modified code that uses the 'lemmatized_sentence' column for extraction and prints the results in the desired format:
python
User
The progress bar now shows '2610it [00:19, 133.47it/s]'. I want it to also show %s and time lapsed, time remaining and ETA
Assistant
To display the progress bar with percentage, time lapsed, time remaining, and ETA, you can use the tqdm module's tqdm function with the total parameter set to the number of iterations. Here's the modified code:
python
User
I don't see the results printed on the screen in the way I want them. For instance the first extracted policy option is "Plan The Zelenskyy Plan must account for and encourage these regional differences." These are not in tabular form in two columns. And also I would have expected that the lemmatized word 'plan' that was extracted from the 'lemmatized_sentence' would also be highlighted in a color in the second column which would display the full sentence from the 'sentence' column with the non-lemmatized word plan in a color. Maybe in html format?
Assistant
I apologize for the confusion. To display the results in the desired format, you can use the tabulate module to create a table with two columns. To highlight the extracted words in the second column, you can use the colored module to color the words. Here's the modified code:
python
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
with open("results.json", "w") as f:
json.dump(results, f)
I get this error
TypeError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_57916\72252947.py in
36 subj = [tok for tok in token.subtree if tok.dep_ == "nsubj"]
37 if subj:
---> 38 results.append(subj[0].text, token.text)
39 # Extract adverbial phrases and check if they match any of the adverbial phrases in your list
40 for token in doc:
TypeError: list.append() takes exactly one argument (2 given)
# Iterate through the sentences in your dataframe
for sentence in tqdm(df['sentence']):
# Process the sentence with spaCy
doc = nlp(sentence)
# Extract entities and check if they match any of the lexical terms in your list
for ent in doc.ents:
if ent.text in lexical_terms:
results.append(ent.text)
# Extract verbs and check
for token in doc:
if token.text in verbs:
# Extract the subject of the verb
subj = [tok for tok in token.subtree if tok.dep_ == "nsubj"]
if subj:
results.extend([subj[0].text, token.text])
# Extract modal verbs and check if they match any of the modal verbs in your list
for token in doc:
if token.text in modal_verbs:
# Extract the subject of the modal verb
subj = [tok for tok in token.subtree if tok.dep_ == "nsubj"]
if subj:
results.extend([subj[0].text, token.text])
# Extract adverbial phrases and check if they match any of the adverbial phrases in your list
for token in doc:
if token.text in adverbial_phrases:
# Extract the subject of the adverbial phrase
subj = [tok for tok in token.subtree if tok.dep_ == "nsubj"]
if subj:
results.extend([subj[0].text, token.text])
# Extract phrasal verbs and check if they match any of the phrasal verbs in your list
for token in doc:
if token.text in phrasal_verbs:
# Extract the subject of the phrasal verb
subj = [tok for tok in token.subtree if tok.dep_ == "nsubj"]
if subj:
results.extend([subj[0].text, token.text])
This code should work without raising the TypeError you encountered earlier.
# Define the lists of lexical terms, verbs, modal verbs, adverbial phrases, and phrasal verbs
lexical_terms = ["Proposal", "Plan"]
verbs = ["Propose", "Plan"]
modal_verbs = ["Can", "Could"]
adverbial_phrases = ["In the long run", "As a temporary measure"]
phrasal_verbs = ["Come up with", "Look into"]
# Iterate through the lemmatized sentences in your dataframe
for lemmatized_sentence in tqdm(df['lemmatized_sentence']):
# Process the sentence with spaCy
doc = nlp(lemmatized_sentence)
# Extract entities and check if they match any of the lexical terms in your list
for ent in doc.ents:
if ent.text in lexical_terms:
results.append(ent.text)
# Extract verbs and check
for token in doc:
if token.text in verbs:
# Extract the subject of the verb
subj = [tok for tok in token.subtree if tok.dep_ == "nsubj"]
if subj:
results.append((subj[0].text, token.text))
# Print the results in two columns
for result in results:
print(result)
# Write the results to 'policy_options.json'
with open("policy_options.json", "w") as f:
json.dump(results, f)
This code will extract the desired information from the 'lemmatized_sentence' column and print the results in the desired format. The results will also be written to 'policy_options.json'.
from tqdm import tqdm
# Define the lists of lexical terms, verbs, modal verbs, adverbial phrases, and phrasal verbs
lexical_terms = ["Proposal", "Plan"]
verbs = ["Propose", "Plan"]
modal_verbs = ["Can", "Could"]
adverbial_phrases = ["In the long run", "As a temporary measure"]
phrasal_verbs = ["Come up with", "Look into"]
# Iterate through the lemmatized sentences in your dataframe
for lemmatized_sentence in tqdm(df['lemmatized_sentence'], total=len(df['lemmatized_sentence'])):
# Process the sentence with spaCy
doc = nlp(lemmatized_sentence)
# Extract entities and check if they match any of the lexical terms in your list
for ent in doc.ents:
if ent.text in lexical_terms:
results.append(ent.text)
# Extract verbs and check
for token in doc:
if token.text in verbs:
# Extract the subject of the verb
subj = [tok for tok in token.subtree if tok.dep_ == "nsubj"]
if subj:
results.append((subj[0].text, token.text))
# Print the results in two columns
for result in results:
print(result)
# Write the results to 'policy_options.json'
with open("policy_options.json", "w") as f:
json.dump(results, f)
This code will display thetqdm` progress bar with percentage, time lapsed, time remaining, and ETA.
from tabulate import tabulate
from colored import fg, bg, attr
# Define the lists of lexical terms, verbs, modal verbs, adverbial phrases, and phrasal verbs
lexical_terms = ["Proposal", "Plan"]
verbs = ["Propose", "Plan"]
modal_verbs = ["Can", "Could"]
adverbial_phrases = ["In the long run", "As a temporary measure"]
phrasal_verbs = ["Come up with", "Look into"]
# Iterate through the lemmatized sentences in your dataframe
for lemmatized_sentence in tqdm(df['lemmatized_sentence'], total=len(df['lemmatized_sentence'])):
# Process the sentence with spaCy
doc = nlp(lemmatized_sentence)
# Extract entities and check if they match any of the lexical terms in your list
for ent in doc.ents:
if ent.text in lexical_terms:
results.append((ent.text, lemmatized_sentence))
# Extract verbs and check
for token in doc:
if token.text in verbs:
# Extract the subject of the verb
subj = [tok for tok in token.subtree if tok.dep_ == "nsubj"]
if subj:
results.append((subj[0].text, lemmatized_sentence))
# Print the results in a table with two columns
table = []
for result in results:
table.append([result[0], f"{result[1]} {fg('red')}{result[0]}{attr('reset')}"])
print(tabulate(table, headers=["Extracted Policy Option", "Full Sentence"], tablefmt="html"))
# Write the results to 'policy_options.json'
with open("policy_options.json", "w") as f:
json.dump(results, f)
This code will display the results in a table with two columns, with the extracted words highlighted in red in the second column. The results will also be written to 'policy_options.json'.