# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import spacy
from spacy.matcher import Matcher

# ## Load model and create a document

# Define a document
string = 'My new iPhone 6 is white. Xiaiomi smartphones are pretty. I like to spend afternoons in Old Yafo and \
take pictures with my iPhone 7.'

# Load a language model
nlp = spacy.load("en_core_web_sm")

# Create a spacy document
doc = nlp(string)

doc.text

# ## POS tagging

for token in doc:
    if token.pos_ == 'PROPN':
        print(token, token.pos_)

# ## NER

# Get entities
for ent in doc.ents:
    print(ent, ent.label_)

# ## Rule-based matching

# Initiate matcher
matcher = Matcher(nlp.vocab)

# Define a pattern
pattern = [
    {'LOWER': 'iphone'},
    {'IS_DIGIT': True}
]

# Add pattern to matcher
matcher.add('IPHONE_PATTERN', None, pattern)   # `None` because we don't use any callbacks now 

# Get matches - it returns a tuple of 3 vals
matches = matcher(doc)
matches

for match_id, start, end in matches:
    matched_span = doc[start:end]
    print(f'Your match: {matched_span.text}')

# ## More complex matching

# +
matcher2 = Matcher(nlp.vocab)

doc2 = nlp("I installed WinZip in 8th grade. Nevertheless, it didn\'t work. I tried to install GZip, \
I tried to install XZip and others. Nothing worked. I am gonna install Ubuntu now.")

pattern2 = [{'LEMMA': 'install'}, {'POS': 'PROPN'}]

# Add the pattern to the matcher and apply the matcher to the doc
matcher2.add('DOWNLOAD_THINGS_PATTERN', None, pattern2)
matches2 = matcher2(doc2)
print('Total matches found:', len(matches2))

# Iterate over the matches and print the span text
for match_id, start, end in matches2:
    print('Your match:', doc2[start:end].text)
# -

# No WinZip though
for token in doc2:
    if token.pos_ == 'PROPN':
        print(token)



StarcoderdataJupyterScriptsDedupFiltered