|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "id": "e79ee373", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "from SNgramExtractor import SNgramExtractor\n", |
| 11 | + "import spacy\n", |
| 12 | + "from spacy import displacy\n", |
| 13 | + "en_nlp = spacy.load('en_core_web_sm')" |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "markdown", |
| 18 | + "id": "ecd66da8", |
| 19 | + "metadata": {}, |
| 20 | + "source": [ |
| 21 | + "### Extracting syntactic N-grams" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": 2, |
| 27 | + "id": "9165ecf9", |
| 28 | + "metadata": {}, |
| 29 | + "outputs": [ |
| 30 | + { |
| 31 | + "name": "stdout", |
| 32 | + "output_type": "stream", |
| 33 | + "text": [ |
| 34 | + "Original Text: You should not run any more\n", |
| 35 | + "SNGram bigram: run_You run_should run_not more_any run_more\n", |
| 36 | + "SNGram trigram: run_more_any\n" |
| 37 | + ] |
| 38 | + } |
| 39 | + ], |
| 40 | + "source": [ |
| 41 | + "text = 'You should not run any more'\n", |
| 42 | + "\n", |
| 43 | + "SNgram_obj=SNgramExtractor(text,\n", |
| 44 | + " meta_tag='original',\n", |
| 45 | + " trigram_flag='yes',\n", |
| 46 | + " nlp_model=None)\n", |
| 47 | + "output=SNgram_obj.get_SNgram()\n", |
| 48 | + "print('Original Text:',text)\n", |
| 49 | + "print('SNGram bigram:',output['SNBigram'])\n", |
| 50 | + "print('SNGram trigram:',output['SNTrigram'])" |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "code", |
| 55 | + "execution_count": null, |
| 56 | + "id": "73882fa4", |
| 57 | + "metadata": {}, |
| 58 | + "outputs": [], |
| 59 | + "source": [] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "markdown", |
| 63 | + "id": "60010f05", |
| 64 | + "metadata": {}, |
| 65 | + "source": [ |
| 66 | + "### Dependency Parse tree" |
| 67 | + ] |
| 68 | + }, |
| 69 | + { |
| 70 | + "cell_type": "code", |
| 71 | + "execution_count": 3, |
| 72 | + "id": "99e2c90b", |
| 73 | + "metadata": {}, |
| 74 | + "outputs": [ |
| 75 | + { |
| 76 | + "data": { |
| 77 | + "text/html": [ |
| 78 | + "<span class=\"tex2jax_ignore\"><svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xml:lang=\"en\" id=\"79c4ca11417542d090928c022c84c842-0\" class=\"displacy\" width=\"950\" height=\"362.0\" direction=\"ltr\" style=\"max-width: none; height: 362.0px; color: #000000; background: #ffffff; font-family: Arial; direction: ltr\">\n", |
| 79 | + "<text class=\"displacy-token\" fill=\"currentColor\" text-anchor=\"middle\" y=\"272.0\">\n", |
| 80 | + " <tspan class=\"displacy-word\" fill=\"currentColor\" x=\"50\">You</tspan>\n", |
| 81 | + " <tspan class=\"displacy-tag\" dy=\"2em\" fill=\"currentColor\" x=\"50\">PRON</tspan>\n", |
| 82 | + "</text>\n", |
| 83 | + "\n", |
| 84 | + "<text class=\"displacy-token\" fill=\"currentColor\" text-anchor=\"middle\" y=\"272.0\">\n", |
| 85 | + " <tspan class=\"displacy-word\" fill=\"currentColor\" x=\"200\">should</tspan>\n", |
| 86 | + " <tspan class=\"displacy-tag\" dy=\"2em\" fill=\"currentColor\" x=\"200\">AUX</tspan>\n", |
| 87 | + "</text>\n", |
| 88 | + "\n", |
| 89 | + "<text class=\"displacy-token\" fill=\"currentColor\" text-anchor=\"middle\" y=\"272.0\">\n", |
| 90 | + " <tspan class=\"displacy-word\" fill=\"currentColor\" x=\"350\">not</tspan>\n", |
| 91 | + " <tspan class=\"displacy-tag\" dy=\"2em\" fill=\"currentColor\" x=\"350\">PART</tspan>\n", |
| 92 | + "</text>\n", |
| 93 | + "\n", |
| 94 | + "<text class=\"displacy-token\" fill=\"currentColor\" text-anchor=\"middle\" y=\"272.0\">\n", |
| 95 | + " <tspan class=\"displacy-word\" fill=\"currentColor\" x=\"500\">run</tspan>\n", |
| 96 | + " <tspan class=\"displacy-tag\" dy=\"2em\" fill=\"currentColor\" x=\"500\">VERB</tspan>\n", |
| 97 | + "</text>\n", |
| 98 | + "\n", |
| 99 | + "<text class=\"displacy-token\" fill=\"currentColor\" text-anchor=\"middle\" y=\"272.0\">\n", |
| 100 | + " <tspan class=\"displacy-word\" fill=\"currentColor\" x=\"650\">any</tspan>\n", |
| 101 | + " <tspan class=\"displacy-tag\" dy=\"2em\" fill=\"currentColor\" x=\"650\">PRON</tspan>\n", |
| 102 | + "</text>\n", |
| 103 | + "\n", |
| 104 | + "<text class=\"displacy-token\" fill=\"currentColor\" text-anchor=\"middle\" y=\"272.0\">\n", |
| 105 | + " <tspan class=\"displacy-word\" fill=\"currentColor\" x=\"800\">more</tspan>\n", |
| 106 | + " <tspan class=\"displacy-tag\" dy=\"2em\" fill=\"currentColor\" x=\"800\">ADJ</tspan>\n", |
| 107 | + "</text>\n", |
| 108 | + "\n", |
| 109 | + "<g class=\"displacy-arrow\">\n", |
| 110 | + " <path class=\"displacy-arc\" id=\"arrow-79c4ca11417542d090928c022c84c842-0-0\" stroke-width=\"2px\" d=\"M62,227.0 62,152.0 500.0,152.0 500.0,227.0\" fill=\"none\" stroke=\"currentColor\"/>\n", |
| 111 | + " <text dy=\"1.25em\" style=\"font-size: 0.8em; letter-spacing: 1px\">\n", |
| 112 | + " <textPath xlink:href=\"#arrow-79c4ca11417542d090928c022c84c842-0-0\" class=\"displacy-label\" startOffset=\"50%\" side=\"left\" fill=\"currentColor\" text-anchor=\"middle\">nsubj</textPath>\n", |
| 113 | + " </text>\n", |
| 114 | + " <path class=\"displacy-arrowhead\" d=\"M62,229.0 L58,221.0 66,221.0\" fill=\"currentColor\"/>\n", |
| 115 | + "</g>\n", |
| 116 | + "\n", |
| 117 | + "<g class=\"displacy-arrow\">\n", |
| 118 | + " <path class=\"displacy-arc\" id=\"arrow-79c4ca11417542d090928c022c84c842-0-1\" stroke-width=\"2px\" d=\"M212,227.0 212,177.0 497.0,177.0 497.0,227.0\" fill=\"none\" stroke=\"currentColor\"/>\n", |
| 119 | + " <text dy=\"1.25em\" style=\"font-size: 0.8em; letter-spacing: 1px\">\n", |
| 120 | + " <textPath xlink:href=\"#arrow-79c4ca11417542d090928c022c84c842-0-1\" class=\"displacy-label\" startOffset=\"50%\" side=\"left\" fill=\"currentColor\" text-anchor=\"middle\">aux</textPath>\n", |
| 121 | + " </text>\n", |
| 122 | + " <path class=\"displacy-arrowhead\" d=\"M212,229.0 L208,221.0 216,221.0\" fill=\"currentColor\"/>\n", |
| 123 | + "</g>\n", |
| 124 | + "\n", |
| 125 | + "<g class=\"displacy-arrow\">\n", |
| 126 | + " <path class=\"displacy-arc\" id=\"arrow-79c4ca11417542d090928c022c84c842-0-2\" stroke-width=\"2px\" d=\"M362,227.0 362,202.0 494.0,202.0 494.0,227.0\" fill=\"none\" stroke=\"currentColor\"/>\n", |
| 127 | + " <text dy=\"1.25em\" style=\"font-size: 0.8em; letter-spacing: 1px\">\n", |
| 128 | + " <textPath xlink:href=\"#arrow-79c4ca11417542d090928c022c84c842-0-2\" class=\"displacy-label\" startOffset=\"50%\" side=\"left\" fill=\"currentColor\" text-anchor=\"middle\">neg</textPath>\n", |
| 129 | + " </text>\n", |
| 130 | + " <path class=\"displacy-arrowhead\" d=\"M362,229.0 L358,221.0 366,221.0\" fill=\"currentColor\"/>\n", |
| 131 | + "</g>\n", |
| 132 | + "\n", |
| 133 | + "<g class=\"displacy-arrow\">\n", |
| 134 | + " <path class=\"displacy-arc\" id=\"arrow-79c4ca11417542d090928c022c84c842-0-3\" stroke-width=\"2px\" d=\"M662,227.0 662,202.0 794.0,202.0 794.0,227.0\" fill=\"none\" stroke=\"currentColor\"/>\n", |
| 135 | + " <text dy=\"1.25em\" style=\"font-size: 0.8em; letter-spacing: 1px\">\n", |
| 136 | + " <textPath xlink:href=\"#arrow-79c4ca11417542d090928c022c84c842-0-3\" class=\"displacy-label\" startOffset=\"50%\" side=\"left\" fill=\"currentColor\" text-anchor=\"middle\">advmod</textPath>\n", |
| 137 | + " </text>\n", |
| 138 | + " <path class=\"displacy-arrowhead\" d=\"M662,229.0 L658,221.0 666,221.0\" fill=\"currentColor\"/>\n", |
| 139 | + "</g>\n", |
| 140 | + "\n", |
| 141 | + "<g class=\"displacy-arrow\">\n", |
| 142 | + " <path class=\"displacy-arc\" id=\"arrow-79c4ca11417542d090928c022c84c842-0-4\" stroke-width=\"2px\" d=\"M512,227.0 512,177.0 797.0,177.0 797.0,227.0\" fill=\"none\" stroke=\"currentColor\"/>\n", |
| 143 | + " <text dy=\"1.25em\" style=\"font-size: 0.8em; letter-spacing: 1px\">\n", |
| 144 | + " <textPath xlink:href=\"#arrow-79c4ca11417542d090928c022c84c842-0-4\" class=\"displacy-label\" startOffset=\"50%\" side=\"left\" fill=\"currentColor\" text-anchor=\"middle\">advmod</textPath>\n", |
| 145 | + " </text>\n", |
| 146 | + " <path class=\"displacy-arrowhead\" d=\"M797.0,229.0 L801.0,221.0 793.0,221.0\" fill=\"currentColor\"/>\n", |
| 147 | + "</g>\n", |
| 148 | + "</svg></span>" |
| 149 | + ], |
| 150 | + "text/plain": [ |
| 151 | + "<IPython.core.display.HTML object>" |
| 152 | + ] |
| 153 | + }, |
| 154 | + "metadata": {}, |
| 155 | + "output_type": "display_data" |
| 156 | + } |
| 157 | + ], |
| 158 | + "source": [ |
| 159 | + "doc = en_nlp(text)\n", |
| 160 | + "displacy.render(doc,options={'compact':True})" |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "code", |
| 165 | + "execution_count": null, |
| 166 | + "id": "6ac52371", |
| 167 | + "metadata": {}, |
| 168 | + "outputs": [], |
| 169 | + "source": [] |
| 170 | + } |
| 171 | + ], |
| 172 | + "metadata": { |
| 173 | + "kernelspec": { |
| 174 | + "display_name": "Python 3 (ipykernel)", |
| 175 | + "language": "python", |
| 176 | + "name": "python3" |
| 177 | + }, |
| 178 | + "language_info": { |
| 179 | + "codemirror_mode": { |
| 180 | + "name": "ipython", |
| 181 | + "version": 3 |
| 182 | + }, |
| 183 | + "file_extension": ".py", |
| 184 | + "mimetype": "text/x-python", |
| 185 | + "name": "python", |
| 186 | + "nbconvert_exporter": "python", |
| 187 | + "pygments_lexer": "ipython3", |
| 188 | + "version": "3.9.13" |
| 189 | + } |
| 190 | + }, |
| 191 | + "nbformat": 4, |
| 192 | + "nbformat_minor": 5 |
| 193 | +} |
0 commit comments