diff --git a/part11.ipynb b/part11.ipynb new file mode 100644 index 0000000..55eb005 --- /dev/null +++ b/part11.ipynb @@ -0,0 +1,115 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "import spacy" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "#spacy pipeline\n", + "nlp = spacy.load('en_core_web_md')" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Python PROPN\n", + "is AUX\n", + "n't PART\n", + "just ADV\n", + "a DET\n", + "language NOUN\n", + ". PUNCT\n", + "It PRON\n", + "'s AUX\n", + "a DET\n", + "framework NOUN\n", + "! PUNCT\n" + ] + } + ], + "source": [ + "doc = nlp(\"Python isn't just a language. It's a framework!\")\n", + "for token in doc:\n", + " print(token, token.pos_)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "text lemma pos ent shape punct morph \n", + "------ ------ ----- ------ ----- ----- ------------------------------\n", + "Hi hi INTJ Xx False \n", + ", , PUNCT , True PunctType=Comm \n", + "my my PRON xx False Number=Sing|Person=1|Poss=Yes|PronType=Prs\n", + "name name NOUN xxxx False Number=Sing \n", + "is be AUX xx False Mood=Ind|Number=Sing|Person=3|Tense=Pres|VerbForm=Fin\n", + "Kevin Kevin PROPN PERSON Xxxxx False Number=Sing \n", + ". . PUNCT . True PunctType=Peri \n", + "I I PRON X False Case=Nom|Number=Sing|Person=1|PronType=Prs\n", + "like like VERB xxxx False Tense=Pres|VerbForm=Fin \n", + "to to PART xx False \n", + "write write VERB xxxx False VerbForm=Inf \n", + "Python Python PROPN Xxxxx False Number=Sing \n", + "\n" + ] + } + ], + "source": [ + "from wasabi import table\n", + "\n", + "def text_to_doctable(txt):\n", + " doc = nlp(txt)\n", + " header = (\"text\", \"lemma\", \"pos\", \"ent\", \"shape\", \"punct\", \"morph\")\n", + " data = [(tok.text, tok.lemma_, tok.pos_, tok.ent_type_, tok.shape_, tok.is_punct, tok.morph) for tok in doc]\n", + " formatted = table(data, header=header, divider=True)\n", + " print(formatted)\n", + "\n", + "text_to_doctable(\"Hi, my name is Kevin. I like to write Python\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "torch", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.9" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}