NLP Folder

Overview

The NLP folder contains scripts for natural language processing tasks using spaCy.

File: 2nd.py

Description: Tokenizes and lemmatizes text using spaCy.

Dependencies: spacy (with model en_core_web_sm)

Code:

                
                import spacy

nlp = spacy.load('en_core_web_sm')
text = "The quick brown foxes are running."
doc = nlp(text)
for token in doc:
    print(f"{token.text}: {token.lemma_}")
            

File: 3rd.py

Description: Performs part-of-speech tagging on a sample sentence.

Dependencies: spacy (with model en_core_web_sm)

Code:

                
                import spacy

nlp = spacy.load('en_core_web_sm')
text = "Apple is launching a new product."
doc = nlp(text)
for token in doc:
    print(f"{token.text}: {token.pos_}")
            

File: 4th.py

Description: Extracts named entities from a news article snippet.

Dependencies: spacy (with model en_core_web_sm)

Code:

                
                import spacy

nlp = spacy.load('en_core_web_sm')
text = "Tesla opened a new factory in Shanghai."
doc = nlp(text)
for ent in doc.ents:
    print(f"{ent.text}: {ent.label_}")
            

File: 4thalternative.py

Description: Alternative NER approach using a different spaCy model.

Dependencies: spacy (with model en_core_web_lg)

Code:

                
                import spacy

nlp = spacy.load('en_core_web_lg')
text = "Tesla opened a new factory in Shanghai."
doc = nlp(text)
for ent in doc.ents:
    print(f"{ent.text}: {ent.label_}")
            

File: 5th.py

Description: Dependency parsing for sentence structure analysis.

Dependencies: spacy (with model en_core_web_sm)

Code:

                
                import spacy

nlp = spacy.load('en_core_web_sm')
text = "The cat chased the mouse."
doc = nlp(text)
for token in doc:
    print(f"{token.text}: {token.dep_} -> {token.head.text}")
            

File: 6th.py

Description: Evaluates NER performance by comparing predicted and true entities.

Dependencies: spacy (with model en_core_web_sm)

Code:

                
                import spacy
from sklearn.metrics import precision_recall_fscore_support

nlp = spacy.load('en_core_web_sm')
text = "Apple Inc. is based in Cupertino."
doc = nlp(text)
predicted = [(ent.text, ent.label_) for ent in doc.ents]
true = [("Apple Inc.", "ORG"), ("Cupertino", "GPE")]
y_true = [label for _, label in true]
y_pred = [label for _, label in predicted]
precision, recall, f1, _ = precision_recall_fscore_support(y_true, y_pred, average='weighted')
print(f"Precision: {precision}, Recall: {recall}, F1: {f1}")
            

File: 7th.py

Description: Named entity recognition on a sample sentence.

Dependencies: spacy (with model en_core_web_sm)

Code:

                
                import spacy

nlp = spacy.load('en_core_web_sm')
text = "Apple Inc. is looking at buying U.K. startup for $1 billion"
doc = nlp(text)
print('Named Entities')
for ent in doc.ents:
    print(f"{ent.text} ({ent.label_})")
            

File: new.py

Description: Empty file, possibly a placeholder.

Dependencies: None

Code:

                
                # Placeholder file