<jupyter_start><jupyter_text>First things first. Make sure that Spacy is intalled in your system, If not use:```pip install spacy```Next, download a small sized english model trained on web.<jupyter_code>!python -m spacy download en_core_web_sm
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("I like this weather")
from spacy import displacy
# For using Displacy in a local port we can use:
# displacy.server(doc, style="dep")
# For using Displacy from spacy in google colab use:
displacy.render(doc, style='dep', jupyter=True, options={'distance': 90})<jupyter_output><empty_output><jupyter_text>Custom Style<jupyter_code>doc = nlp("This is a sentence in compact mode with custom styles.")
options = {"compact": True, "bg": "#09d5d4", "color": "orange", "font": "verdana"}
displacy.render(doc, style='dep', jupyter=True, options=options)<jupyter_output><empty_output><jupyter_text>Saving as a file<jupyter_code>from pathlib import Path

doc = nlp("I'm a butterfly.")
svg = displacy.render(doc, style="dep", jupyter=False)

filename = "butterfly.svg"
output_path = Path(filename)

with open (output_path, "w", encoding="utf-8") as file:
  file.write(svg)<jupyter_output><empty_output><jupyter_text>Saving as HTML<jupyter_code>doc1 = nlp("I own a ginger cat.")
doc2 = nlp("He is very pretty.")

html = displacy.render([doc1, doc2], style="dep", page=True)
print(html)
displacy.render([doc1, doc2], style="dep", jupyter=True)<jupyter_output><empty_output><jupyter_text>Displaying Entities<jupyter_code>doc = nlp("<NAME> is the CEO of Microsoft.")
displacy.render(doc, style="ent", jupyter=True)<jupyter_output><empty_output><jupyter_text>Custom coloring Entities<jupyter_code>sentence = "Sony was leading the consumer music devices sector not so long ago before he lost it to Apple. By birth of music platforms such SoundCloud and Spotify, Sony lost the music battle completely. Over the last quarter, Apple sold 20.000 iPods for a profit of $5 million. Whereas Sony was able to sell only 5.000 Walkman music players."

doc = nlp(sentence)
colors = {"ORG": "linear-gradient(326deg, #a4508b, #5f0a87)", "PRODUCT": "radial-gradient(yellow, green)"}
options = {"ents": ["ORG", "PRODUCT"], "colors": colors}
displacy.render(doc, style="ent", options=options, jupyter=True)<jupyter_output><empty_output><jupyter_text>Tokenization<jupyter_code>doc1 = nlp("I own a Ferrari")
doc2 = nlp("It's been a crazy week!!!") # with punctuations
print([token.text for token in doc1])
print([token.text for token in doc2])<jupyter_output>['It', "'s", 'been', 'a', 'crazy', 'week', '!', '!', '!']<jupyter_text>Customizing Tokens<jupyter_code>from spacy.symbols import ORTH

doc = nlp("lemme that")
print([w.text for w in doc])
special_case = [{ORTH: "lem"}, {ORTH: "me"}]
nlp.tokenizer.add_special_case("lemme", special_case)
print([w.text for w in nlp("lemme that")])
print([w.text for w in nlp("lemme!!")])
nlp.tokenizer.add_special_case("lemme!!", [{ORTH:"lemme!!"}])
# Takes precedence over the punctuation splitting
print([w.text for w in nlp("lemme!!")])<jupyter_output>['lemme!!']<jupyter_text>Debugging Tokenizer<jupyter_code>text = "Let's go!"
token_exp = nlp.tokenizer.explain(text)
for t in token_exp:
  print(t[1], "\t", t[0])<jupyter_output>Let 	 SPECIAL-1
's 	 SPECIAL-2
go 	 TOKEN
! 	 SUFFIX<jupyter_text>Lemmatization<jupyter_code>import pandas as pd
doc = nlp("I went for working and worked for 3 years.")
tokens, lemma = [], []
for token in doc:
    tokens.append(token.text)
    lemma.append(token.lemma_)
pd = pd.DataFrame({"Token":tokens, "Lemma":lemma})
pd
from spacy.symbols import ORTH, LEMMA

special_case = [{ORTH: "Angeltown", LEMMA:"Los Angels"}]
nlp.tokenizer.add_special_case("Angeltown", special_case)
doc = nlp("I'm flying to Angeltown")
for token in doc:
  print(token.text, "\t", token.lemma_)<jupyter_output>I 	 -PRON-
'm 	 be
flying 	 fly
to 	 to
Angeltown 	 Los Angels<jupyter_text>Spacy Container Objects Doc<jupyter_code>doc = nlp("I like cats")
doc.text
for token in doc:
  print(token.text)
len(doc)
print(doc[1])
# gets text's sentences
doc2 = nlp("This is a sentence. This is the second sentence.")
doc2.sents
sentences = list(doc2.sents)
print(sentences)
# gets named entities
doc3 = nlp("I flied to New York with Ashley")
print(doc3.ents)
doc4 = nlp("Sweet brown fox jumped over the fence.")
print(list(doc4.noun_chunks))
print(doc4.lang_)
# serialization
doc = nlp("Hi")
json_doc = doc.to_json()
json_doc<jupyter_output><empty_output><jupyter_text>**NOTE**:  `doc.lang_` returns unicode string of the language where as `doc.lang` returns the language ID. Similar conventions for `token.lemma_ , token.tag_ , token.pos_`.   Token<jupyter_code>doc = nlp("Hi Madam!")

for token in doc:
  print(token.text, token.text_with_ws, len(token), token.idx, token.doc, token.sent)
doc = nlp("He entered the room. Then he nodded.")
print(list(doc.sents))
doc[0].is_sent_start
doc[5].is_sent_start
doc[6].is_sent_start
doc = nlp("I went there.")
doc[1].lemma_
doc = nlp("President Trump visited Mexico City")
doc.ents
for i in range(len(doc)):
  print(doc[i], "\t", doc[i].ent_type_)
# All features and methods
dir(doc)<jupyter_output><empty_output><jupyter_text>Span<jupyter_code>doc = nlp("I know that you have been to USA.")
print(doc[2:4])
doc2 =  nlp("President Trump visited Mexico City.")
print(doc2[4:])
print(doc2[3:-1]) # 3 to rest of the sentence
doc3 = nlp("You love Atlanta since you're 20.")
print(doc3.char_span(4, 16))
doc4 = nlp("You went there after you saw me.")
span = doc4[2:4]
print(span)
for token in span:
    print(token)

print(len(span))
print(span.sent)
print(span.doc)
print("Start:",span.start)
print("End:",span.end)
print("StartChar:",span.start_char)
print("EndChar:",span.end_char)<jupyter_output>there after
there
after
2
You went there after you saw me.
You went there after you saw me.
Start: 2
End: 4
StartChar: 9
EndChar: 20<jupyter_text>More Spacy features<jupyter_code>doc = nlp("Hello, hi!")
print(doc[0].lower_)
doc1 = nlp("HELLO, Hello, hello, hEllO")
print(doc1[0].is_upper)
print(doc1[0].is_lower)
print(doc1[1].is_upper)
print(doc1[1].is_lower)
doc2 = nlp("Cat! and Cat123")
print(doc2[0].is_alpha)
print(doc2[1].is_alpha)
print(doc2[2].is_alpha)
print(doc2[3].is_alpha)
doc3 = nlp("Hamburg and Göttingen")
print(doc3[0].is_ascii)
print(doc3[2].is_ascii)
doc4 = nlp("Cat Cat123 123")
print(doc4[0].is_digit)
print(doc4[1].is_digit)
print(doc4[2].is_digit)
doc5 = nlp("You, him and Sally")
print(doc5[1])
print(doc5[1].is_punct)
doc6 = nlp(" ")
print(doc6[0])
print(len(doc6[0]))
print(doc6[0].is_space)
doc7 = nlp("I paid 12$ for the t-shirt.")
print(doc7[3])
print(doc7[3].is_currency)
doc8 = nlp("I emailed you at least 100 times")
print(doc8[-2])
print(doc8[-2].like_num)
doc9 = nlp("I emailed you at least hundred times")
print(doc9[-2])
print(doc9[-2].like_num)

doc10 = nlp("My email is <EMAIL> and you can visit me under https://duygua.github.io any time you want.")
print(doc10[3])
print(doc10[3].like_email)
print(doc10[10])
print(doc10[10].like_url)

for token in doc10:
  if token.like_email or token.like_url:
    print(token.text)
doc11 = nlp("Girl called Kathy has a nickname Cat123.")
for token in doc11:
    print(token.text, token.shape_)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.152191894127378, 'fraction_numerical': 0.024676040805073064, 'mean_word_length': 6.320887991927346, 'pattern_counts': {'":': 10, '<': 54, '<?xml version=': 0, '>': 54, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '70369', 'license': 'MIT', 'n_tokens_mistral': 2842, 'n_tokens_neox': 2719, 'n_words': 707, 'path': 'Exploring Spacy3/Getting_Started_with_Spacy.ipynb', 'repo_name': 'luckyrabbit85/NLP', 'size': '7266'}