Dependency parsing 4. Exercises
Dependency parsing with multiple texts
In this exercise, you will apply the dependency parsing and CoNLL-U procedures from the previous modules to multiple texts. Use the same five sample texts from the English POS tagging exercises. Follow the same steps from that activity to load the .txt files and process them with nlp_en.pipe() so that you have:
text_files
docs
You do not need to download the sample corpus again, if you already saved it in your local computer.
Inspect dependency annotations across texts
Choose one of the sample texts and inspect its dependency annotations:
doc = docs[0]
for token in doc:
if not token.is_space:
print(
token.text,
token.pos_,
token.head.text,
token.dep_,
sep="\t"
)
Try this with different sample texts. Pay particular attention to the syntactic head and dependency relation assigned to each token.
Create CoNLL-U files
Use the doc_to_conllu() function from the CoNLL-U format module to convert each sample text into CoNLL-U format.
First, create a folder for the output files:
from pathlib import Path
output_folder = Path("dependency_output")
output_folder.mkdir(parents=True, exist_ok=True)
Then convert and save all five texts:
for text_file, doc in zip(text_files, docs):
conllu_text = doc_to_conllu(
doc,
text_label=text_file.stem
)
output_path = (
output_folder /
f"{text_file.stem}.conllu"
)
output_path.write_text(
conllu_text,
encoding="utf-8"
)
print(f"Saved: {output_path.name}")
Your output folder should contain:
sample1.conllu
sample2.conllu
sample3.conllu
sample4.conllu
sample5.conllu
Check your output
A .conllu file is a plain-text file, so you can open it with any text editor. For example:
- VSCode: right-click the file and select Open With → Text Editor
- Excel: open or import the
.conllufile as a tab-delimited text file. This is useful for viewing the ten CoNLL-U fields as separate columns. - macOS: open it with TextEdit
- Windows: open it with Notepad?
You can also inspect the file directly in Python:
conllu_file = output_folder / "sample1.conllu"
print(
conllu_file.read_text(
encoding="utf-8"
)
)
Open at least one .conllu file and check that:
- each sentence includes
# sent_idand# text; - each token is represented using the ten CoNLL-U columns;
HEADcontains the syntactic head ID;DEPRELcontains the dependency relation;- sentences are separated by a blank line.
Submission
Submit the five .conllu files in a single compressed folder.
Use the following filename:
lastname_firstname_dependency_analysis.zip