<jupyter_start><jupyter_text>Chapter 2<jupyter_code>import spacy

nlp = spacy.load('en_core_web_sm')

coffee_hash = nlp.vocab.strings['coffee']
print('Coffee in hash:', coffee_hash)

coffee_string = nlp.vocab.strings[coffee_hash]
print('Coffee in string:', coffee_string)
lexeme = nlp.vocab['coffee']

print(lexeme.text, lexeme.orth, lexeme.is_alpha)
from spacy.lang.en import English
from spacy.tokens import Doc, Span
import spacy

words = ['Hello', 'world', '!']
spaces = [True, False, False]

doc = Doc(nlp.vocab, words = words, spaces = spaces)
print('DOC content:', doc.text)

span = Span(doc, 0, 2)
print('Span:', span)
from spacy.lang.en import English
import spacy

# nlp = English()
nlp = spacy.load('en_core_web_sm')

# Import the Doc and Span classes
from spacy.tokens import Doc, Span

words = ["I", "like", "David", "Bowie"]
spaces = [True, True, True, False]

# Create a doc from the words and spaces
doc = Doc(nlp.vocab, words=words, spaces=spaces)
print(doc.text)

# Create a span for "<NAME>" from the doc and assign it the label "PERSON"
span = Span(doc, 2, 4, label="PERSON")
print(span.text, span.label_)

# Add the span to the doc's entities
doc.ents = [span]

# Print entities' text and labels
print([(ent.text, ent.label_) for ent in doc.ents])
import spacy

nlp = spacy.load("en_core_web_sm")
doc = nlp("Berlin looks like a nice city")


token_texts = [token.text for token in doc]
pos_tags = [token.pos_ for token in doc]

for index, pos in enumerate(pos_tags):
    if pos == 'PROPN':
        if (pos_tags[index + 1]) == 'VERB':
            print('Proper Noun:', token_texts[index])
import spacy

nlp = spacy.load("en_core_web_sm")
doc = nlp("Berlin looks like a nice city")

for token in doc:
    if token.pos_ == 'PROPN':
        if doc[token.i + 1].pos_ == 'VERB':
            print('Proper Noun:', token.text)
import spacy

nlp = spacy.load('en_core_web_md')

doc_1 = nlp('I like fast food')
doc_2 = nlp('I like pizza')

print('Simularity:', doc_1.similarity(doc_2))
# Compare two tokens
doc = nlp("I like pizza and pasta")

token1 = doc[2]
token2 = doc[4]

print(token1.similarity(token2))
doc = nlp('I like pizza')
token = nlp('soap')[0]

print(doc.similarity(token))
import spacy

nlp = spacy.load('en_core_web_md')

doc = nlp('I have a banana')

print(doc[3].vector)
# Initialize with the shared vocab
from spacy.matcher import Matcher
matcher = Matcher(nlp.vocab)

# Patterns are lists of dictionaries describing the tokens
pattern = [{"LEMMA": "love", "POS": "VERB"}, {"LOWER": "cats"}]
matcher.add("LOVE_CATS", [pattern])

# Operators can specify how often a token should be matched
pattern = [{"TEXT": "very", "OP": "+"}, {"TEXT": "happy"}]
matcher.add("VERY_HAPPY", [pattern])

# Calling matcher on doc returns list of (match_id, start, end) tuples
doc = nlp("I love cats and I'm very very happy")
matches = matcher(doc)

for match_id, start, end in matches:
    print(doc[start:end], end = '\t')
matcher = Matcher(nlp.vocab)
matcher.add("DOG", [[{"LOWER": "golden"}, {"LOWER": "retriever"}]])
doc = nlp("I have a Golden Retriever")

for match_id, start, end in matcher(doc):
    span = doc[start:end]
    print("Matched span:", span.text)
    # Get the span's root token and root head token
    print("Root token:", span.root.text)
    print("Root head token:", span.root.head.text)
    # Get the previous token and its POS tag
    print("Previous token:", doc[start - 1].text, doc[start - 1].pos_)
from spacy.matcher import PhraseMatcher

matcher = PhraseMatcher(nlp.vocab)

pattern = nlp("Golden Retriever")
matcher.add("DOG", None, pattern)
doc = nlp("I have a Golden Retriever")

# Iterate over the matches
for match_id, start, end in matcher(doc):
    # Get the matched span
    span = doc[start:end]
    print("Matched span:", span.text)
import spacy
from spacy.matcher import Matcher

nlp = spacy.load("en_core_web_sm")
doc = nlp(
    "Twitch Prime, the perks program for Amazon Prime members offering free "
    "loot, games and other benefits, is ditching one of its best features: "
    "ad-free viewing. According to an email sent out to Amazon Prime members "
    "today, ad-free viewing will no longer be included as a part of Twitch "
    "Prime for new members, beginning on September 14. However, members with "
    "existing annual subscriptions will be able to continue to enjoy ad-free "
    "viewing until their subscription comes up for renewal. Those with "
    "monthly subscriptions will have access to ad-free viewing until October 15."
)

# Create the match patterns
pattern1 = [{"LOWER": "amazon"}, {"IS_TITLE": True, "POS": "PROPN"}]
pattern2 = [{"LOWER": "ad"}, {'TEXT': '-'}, {'LOWER': 'free'}, {"POS": "NOUN"}]

# Initialize the Matcher and add the patterns
matcher = Matcher(nlp.vocab)
matcher.add("PATTERN1", [pattern1])
matcher.add("PATTERN2", [pattern2])

# Iterate over the matches
for match_id, start, end in matcher(doc):
    # Print pattern string name and text of matched span
    print(doc.vocab.strings[match_id], doc[start:end].text)<jupyter_output>PATTERN1 Amazon Prime
PATTERN2 ad-free viewing
PATTERN1 Amazon Prime
PATTERN2 ad-free viewing
PATTERN2 ad-free viewing
PATTERN2 ad-free viewing
{'fraction_non_alphanumeric': 0.1366183574879227, 'fraction_numerical': 0.00714975845410628, 'mean_word_length': 4.553648068669528, 'pattern_counts': {'":': 13, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '365876', 'license': 'Apache-2.0', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1796, 'n_words': 588, 'path': 'Courses/spaCy/Chapter 2.ipynb', 'repo_name': 'keivanipchihagh/Intro_to_DS_and_ML', 'size': '5180'}