





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






import requests import spacy import pandas as pd from spacy import displacy
nlp = spacy.load("en_core_web_sm")
dataset_url = "https://raw.githubusercontent.com/tidyverse/stringr/refs/heads/main/data-raw/harvard-sentences.txt" response = requests.get(dataset_url) sentences = response.text.split("\n") sentences = [s.strip() for s in sentences if len(s.strip()) > 0] print("=" * 60) print("Total Sentences :", len(sentences)) print("=" * 60)
for i, sentence in enumerate(sentences[:10]): print("\n") print("=" * 70) print("Sentence", i + 1) print("=" * 70) print(sentence) doc = nlp(sentence) print("\nToken\tPOS\tDependency\tHead") for token in doc: print(f"{token.text:15}{token.pos_:10}{token.dep_:15}{token.head.text}") print("\nSubjects") for token in doc: if "subj" in token.dep_: print(token.text) print("\nObjects") for token in doc: if "obj" in token.dep_: print(token.text) print("\nModifiers") for token in doc: if token.dep_ in ["amod", "advmod", "nummod", "compound"]: print(token.text, "->", token.dep_)
print("\nGenerating dependency graphs...") for i, sentence in enumerate(sentences[:3]): doc = nlp(sentence) svg = displacy.render(doc, style="dep", jupyter=False) filename = f"dependency_graph_{i+1}.html" with open(filename, "w", encoding="utf-8") as file: file.write(svg) print(filename, "saved")
print("\n") print("=" * 70) print("Dependency Parsing vs Constituency Parsing") print("=" * 70) comparison = pd.DataFrame({ "Feature":[ "Representation", "Focus", "Root", "Relations", "Complexity", "Used In", "Example" ], "Dependency Parsing":[ "Word-to-word", "Grammatical dependency", "Verb", "Subject, object, modifiers",
"Simple", "NLP, Information Extraction", "John - > eats - > apple" ], "Constituency Parsing":[ "Phrase structure", "Noun Phrase, Verb Phrase", "Sentence", "Phrase hierarchy", "More complex", "Grammar Analysis", "[S [NP John] [VP eats apple]]" ] }) print(comparison)
print("\n") print("=" * 70) print("Parsing Limitations") print("=" * 70) limitations = [ "Long sentences reduce parsing accuracy.", "Ambiguous words may receive incorrect dependency labels.", "Named entities with uncommon spellings may be misclassified.", "Complex grammatical structures can confuse the parser.", "Dependency parser performance depends on training data.", "spaCy parser is statistical and may not always produce perfect results." ] for i, item in enumerate(limitations): print(i + 1, ".", item)
print("\n") print("=" * 70) print("Detailed Dependency Analysis") print("=" * 70) sample = "The young boy quickly kicked the red ball into the garden." doc = nlp(sample) print("Sentence :") print(sample) print("\nDependency Details") for token in doc: print( "Word:", token.text, "| POS:", token.pos_, "| Dependency:", token.dep_, "| Head:", token.head.text ) print("\nSubjects") for token in doc: if "subj" in token.dep_: print(token.text) print("\nObjects") for token in doc: if "obj" in token.dep_: print(token.text) print("\nModifiers") for token in doc: if token.dep_ in ["amod", "advmod", "compound", "nummod"]: print(token.text, "->", token.dep_) svg = displacy.render(doc, style="dep", jupyter=False) with open("sample_dependency_graph.html", "w", encoding="utf-8") as file: file.write(svg) print("\nSample dependency graph saved as sample_dependency_graph.html") print("\n") print("Program Completed Successfully")