{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: spacy in /home/shoney/dev/.venv/lib/python3.9/site-packages (3.5.3)\n", "Requirement already satisfied: typer<0.8.0,>=0.3.0 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy) (0.7.0)\n", "Requirement already satisfied: jinja2 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy) (3.1.2)\n", "Requirement already satisfied: setuptools in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy) (57.4.0)\n", "Requirement already satisfied: numpy>=1.15.0 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy) (1.24.3)\n", "Requirement already satisfied: spacy-legacy<3.1.0,>=3.0.11 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy) (3.0.12)\n", "Requirement already satisfied: packaging>=20.0 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy) (23.1)\n", "Requirement already satisfied: requests<3.0.0,>=2.13.0 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy) (2.31.0)\n", "Requirement already satisfied: pydantic!=1.8,!=1.8.1,<1.11.0,>=1.7.4 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy) (1.10.8)\n", "Requirement already satisfied: tqdm<5.0.0,>=4.38.0 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy) (4.65.0)\n", "Requirement already satisfied: thinc<8.2.0,>=8.1.8 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy) (8.1.10)\n", "Requirement already satisfied: spacy-loggers<2.0.0,>=1.0.0 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy) (1.0.4)\n", "Requirement already satisfied: murmurhash<1.1.0,>=0.28.0 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy) (1.0.9)\n", "Requirement already satisfied: preshed<3.1.0,>=3.0.2 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy) (3.0.8)\n", "Requirement already satisfied: catalogue<2.1.0,>=2.0.6 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy) (2.0.8)\n", "Requirement already satisfied: langcodes<4.0.0,>=3.2.0 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy) (3.3.0)\n", "Requirement already satisfied: pathy>=0.10.0 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy) (0.10.1)\n", "Requirement already satisfied: wasabi<1.2.0,>=0.9.1 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy) (1.1.1)\n", "Requirement already satisfied: cymem<2.1.0,>=2.0.2 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy) (2.0.7)\n", "Requirement already satisfied: smart-open<7.0.0,>=5.2.1 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy) (6.3.0)\n", "Requirement already satisfied: srsly<3.0.0,>=2.4.3 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy) (2.4.6)\n", "Requirement already satisfied: typing-extensions>=4.2.0 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from pydantic!=1.8,!=1.8.1,<1.11.0,>=1.7.4->spacy) (4.6.1)\n", "Requirement already satisfied: idna<4,>=2.5 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from requests<3.0.0,>=2.13.0->spacy) (3.4)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from requests<3.0.0,>=2.13.0->spacy) (2.0.2)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from requests<3.0.0,>=2.13.0->spacy) (3.1.0)\n", "Requirement already satisfied: certifi>=2017.4.17 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from requests<3.0.0,>=2.13.0->spacy) (2023.5.7)\n", "Requirement already satisfied: blis<0.8.0,>=0.7.8 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from thinc<8.2.0,>=8.1.8->spacy) (0.7.9)\n", "Requirement already satisfied: confection<1.0.0,>=0.0.1 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from thinc<8.2.0,>=8.1.8->spacy) (0.0.4)\n", "Requirement already satisfied: click<9.0.0,>=7.1.1 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from typer<0.8.0,>=0.3.0->spacy) (8.1.3)\n", "Requirement already satisfied: MarkupSafe>=2.0 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from jinja2->spacy) (2.1.2)\n", "\u001b[33mWARNING: You are using pip version 21.2.3; however, version 23.1.2 is available.\n", "You should consider upgrading via the '/home/shoney/dev/.venv/bin/python -m pip install --upgrade pip' command.\u001b[0m\n" ] } ], "source": [ "!python -m pip install spacy" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Collecting en-core-web-sm==3.5.0\n", " Downloading https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.5.0/en_core_web_sm-3.5.0-py3-none-any.whl (12.8 MB)\n", "\u001b[K |████████████████████████████████| 12.8 MB 625 kB/s eta 0:00:01\n", "\u001b[?25hRequirement already satisfied: spacy<3.6.0,>=3.5.0 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from en-core-web-sm==3.5.0) (3.5.3)\n", "Requirement already satisfied: numpy>=1.15.0 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (1.24.3)\n", "Requirement already satisfied: preshed<3.1.0,>=3.0.2 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (3.0.8)\n", "Requirement already satisfied: packaging>=20.0 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (23.1)\n", "Requirement already satisfied: pathy>=0.10.0 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (0.10.1)\n", "Requirement already satisfied: smart-open<7.0.0,>=5.2.1 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (6.3.0)\n", "Requirement already satisfied: spacy-loggers<2.0.0,>=1.0.0 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (1.0.4)\n", "Requirement already satisfied: spacy-legacy<3.1.0,>=3.0.11 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (3.0.12)\n", "Requirement already satisfied: typer<0.8.0,>=0.3.0 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (0.7.0)\n", "Requirement already satisfied: srsly<3.0.0,>=2.4.3 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (2.4.6)\n", "Requirement already satisfied: langcodes<4.0.0,>=3.2.0 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (3.3.0)\n", "Requirement already satisfied: requests<3.0.0,>=2.13.0 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (2.31.0)\n", "Requirement already satisfied: catalogue<2.1.0,>=2.0.6 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (2.0.8)\n", "Requirement already satisfied: setuptools in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (57.4.0)\n", "Requirement already satisfied: pydantic!=1.8,!=1.8.1,<1.11.0,>=1.7.4 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (1.10.8)\n", "Requirement already satisfied: murmurhash<1.1.0,>=0.28.0 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (1.0.9)\n", "Requirement already satisfied: jinja2 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (3.1.2)\n", "Requirement already satisfied: cymem<2.1.0,>=2.0.2 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (2.0.7)\n", "Requirement already satisfied: thinc<8.2.0,>=8.1.8 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (8.1.10)\n", "Requirement already satisfied: tqdm<5.0.0,>=4.38.0 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (4.65.0)\n", "Requirement already satisfied: wasabi<1.2.0,>=0.9.1 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (1.1.1)\n", "Requirement already satisfied: typing-extensions>=4.2.0 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from pydantic!=1.8,!=1.8.1,<1.11.0,>=1.7.4->spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (4.6.1)\n", "Requirement already satisfied: certifi>=2017.4.17 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from requests<3.0.0,>=2.13.0->spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (2023.5.7)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from requests<3.0.0,>=2.13.0->spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (3.1.0)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from requests<3.0.0,>=2.13.0->spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (2.0.2)\n", "Requirement already satisfied: idna<4,>=2.5 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from requests<3.0.0,>=2.13.0->spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (3.4)\n", "Requirement already satisfied: blis<0.8.0,>=0.7.8 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from thinc<8.2.0,>=8.1.8->spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (0.7.9)\n", "Requirement already satisfied: confection<1.0.0,>=0.0.1 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from thinc<8.2.0,>=8.1.8->spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (0.0.4)\n", "Requirement already satisfied: click<9.0.0,>=7.1.1 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from typer<0.8.0,>=0.3.0->spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (8.1.3)\n", "Requirement already satisfied: MarkupSafe>=2.0 in /home/shoney/dev/.venv/lib/python3.9/site-packages (from jinja2->spacy<3.6.0,>=3.5.0->en-core-web-sm==3.5.0) (2.1.2)\n", "\u001b[33mWARNING: You are using pip version 21.2.3; however, version 23.1.2 is available.\n", "You should consider upgrading via the '/home/shoney/dev/.venv/bin/python -m pip install --upgrade pip' command.\u001b[0m\n", "\u001b[38;5;2m✔ Download and installation successful\u001b[0m\n", "You can now load the package via spacy.load('en_core_web_sm')\n" ] } ], "source": [ "!python -m spacy download en_core_web_sm" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "import spacy\n", "from spacy import displacy\n", "from pathlib import Path\n", "nlp = spacy.load(\"en_core_web_sm\")\n", "\n", "options = {\"compact\": True, \"bg\": \"white\",\n", " \"color\": \"black\", \"font\": \"Source Sans Pro\"}" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "text = (\"Motor oil, engine oil, or engine lubricant is any one of various substances used for the lubrication of internal combustion engines\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "doc = nlp(text)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Noun phrases: ['Motor oil', 'engine oil', 'engine lubricant', 'various substances', 'the lubrication', 'internal combustion engines']\n", "Verbs: ['use']\n" ] } ], "source": [ "# Analyze syntax\n", "print(\"Noun phrases:\", [chunk.text for chunk in doc.noun_chunks])\n", "print(\"Verbs:\", [token.lemma_ for token in doc if token.pos_ == \"VERB\"])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", " Motor\n", " NOUN\n", "\n", "\n", "\n", " oil,\n", " NOUN\n", "\n", "\n", "\n", " engine\n", " NOUN\n", "\n", "\n", "\n", " oil,\n", " NOUN\n", "\n", "\n", "\n", " or\n", " CCONJ\n", "\n", "\n", "\n", " engine\n", " NOUN\n", "\n", "\n", "\n", " lubricant\n", " NOUN\n", "\n", "\n", "\n", " is\n", " AUX\n", "\n", "\n", "\n", " any\n", " DET\n", "\n", "\n", "\n", " one\n", " NUM\n", "\n", "\n", "\n", " of\n", " ADP\n", "\n", "\n", "\n", " various\n", " ADJ\n", "\n", "\n", "\n", " substances\n", " NOUN\n", "\n", "\n", "\n", " used\n", " VERB\n", "\n", "\n", "\n", " for\n", " ADP\n", "\n", "\n", "\n", " the\n", " DET\n", "\n", "\n", "\n", " lubrication\n", " NOUN\n", "\n", "\n", "\n", " of\n", " ADP\n", "\n", "\n", "\n", " internal\n", " ADJ\n", "\n", "\n", "\n", " combustion\n", " NOUN\n", "\n", "\n", "\n", " engines\n", " NOUN\n", "\n", "\n", "\n", " \n", " \n", " compound\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " nsubj\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " compound\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " conj\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " cc\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " compound\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " conj\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " det\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " attr\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " prep\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " amod\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " pobj\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " acl\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " prep\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " det\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " pobj\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " prep\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " amod\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " compound\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " pobj\n", " \n", " \n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "displacy.render(doc, style=\"dep\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "text = (\"Ford does not recomend Fx01\")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "doc = nlp(text)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", " Ford\n", " PROPN\n", "\n", "\n", "\n", " does\n", " AUX\n", "\n", "\n", "\n", " not\n", " PART\n", "\n", "\n", "\n", " recomend\n", " VERB\n", "\n", "\n", "\n", " Fx01\n", " PROPN\n", "\n", "\n", "\n", " \n", " \n", " nsubj\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " aux\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " neg\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " dobj\n", " \n", " \n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "displacy.render(doc, style=\"dep\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5776" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "text = (\"Ford does not recomend Fx01. It recommends Fx05\")\n", "doc = nlp(text)\n", "svg = displacy.render(doc, style=\"dep\",options=options,jupyter=False)\n", "\n", "output_path = Path(\"../doc/images/pronoun.svg\") # you can keep there only \"dependency_plot.svg\" if you want to save it in the same folder where you run the script \n", "output_path.open(\"w\", encoding=\"utf-8\").write(svg)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", " What\n", " PRON\n", "\n", "\n", "\n", " is\n", " AUX\n", "\n", "\n", "\n", " used\n", " VERB\n", "\n", "\n", "\n", " for\n", " ADP\n", "\n", "\n", "\n", " the\n", " DET\n", "\n", "\n", "\n", " lubrication\n", " NOUN\n", "\n", "\n", "\n", " of\n", " ADP\n", "\n", "\n", "\n", " internal\n", " ADJ\n", "\n", "\n", "\n", " combustion\n", " NOUN\n", "\n", "\n", "\n", " engines\n", " NOUN\n", "\n", "\n", "\n", " \n", " \n", " nsubjpass\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " auxpass\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " prep\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " det\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " pobj\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " prep\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " amod\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " compound\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", " pobj\n", " \n", " \n", "\n", "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "text = (\"What is used for the lubrication of internal combustion engines\")\n", "doc = nlp(text)\n", "displacy.render(doc, style=\"dep\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.7" } }, "nbformat": 4, "nbformat_minor": 2 }