{ "cells": [ { "cell_type": "markdown", "id": "297e0da6", "metadata": {}, "source": [ "# 1. Requred library: install, import\n", "# 2. Req data: import" ] }, { "cell_type": "code", "execution_count": null, "id": "e9809af4", "metadata": {}, "outputs": [], "source": [ "# conda install -c anaconda gensim" ] }, { "cell_type": "raw", "id": "46eea9c9", "metadata": {}, "source": [ "Collecting package metadata (current_repodata.json): ...working... done\n", "Solving environment: ...working... failed with initial frozen solve. Retrying with flexible solve.\n", "Solving environment: ...working... failed with repodata from current_repodata.json, will retry with next repodata source.\n", "Collecting package metadata (repodata.json): ...working... done\n", "Solving environment: ...working... done\n", "\n", "Note: you may need to restart the kernel to use updated packages.\n", "## Package Plan ##\n", "\n", " environment location: C:\\Users\\sayed\\anaconda3\n", "\n", "\n", " added / updated specs:\n", " - gensim\n", "\n", "\n", "The following packages will be downloaded:\n", "\n", " package | build\n", " ---------------------------|-----------------\n", " boto3-1.20.24 | pyhd3eb1b0_0 71 KB\n", " botocore-1.23.24 | pyhd3eb1b0_0 4.2 MB\n", " bz2file-0.98 | py39haa95532_1 247 KB\n", " conda-4.12.0 | py39haa95532_0 14.5 MB\n", " gensim-3.8.3 | py39hd77b12b_2 18.4 MB\n", " jmespath-0.10.0 | py_0 22 KB anaconda\n", " s3transfer-0.5.0 | pyhd3eb1b0_0 57 KB\n", " smart_open-1.9.0 | py_0 59 KB anaconda\n", " ------------------------------------------------------------\n", " Total: 37.4 MB\n", "\n", "The following NEW packages will be INSTALLED:\n", "\n", " boto3 pkgs/main/noarch::boto3-1.20.24-pyhd3eb1b0_0\n", " botocore pkgs/main/noarch::botocore-1.23.24-pyhd3eb1b0_0\n", " bz2file pkgs/main/win-64::bz2file-0.98-py39haa95532_1\n", " gensim pkgs/main/win-64::gensim-3.8.3-py39hd77b12b_2\n", " jmespath anaconda/noarch::jmespath-0.10.0-py_0\n", " s3transfer pkgs/main/noarch::s3transfer-0.5.0-pyhd3eb1b0_0\n", " smart_open anaconda/noarch::smart_open-1.9.0-py_0\n", "\n", "The following packages will be UPDATED:\n", "\n", " conda 4.10.3-py39haa95532_0 --> 4.12.0-py39haa95532_0\n", "\n", "\n", "\n", "Downloading and Extracting Packages\n", "\n", "jmespath-0.10.0 | 22 KB | | 0% \n", "jmespath-0.10.0 | 22 KB | #######1 | 72% \n", "jmespath-0.10.0 | 22 KB | ########## | 100% \n", "\n", "s3transfer-0.5.0 | 57 KB | | 0% \n", "s3transfer-0.5.0 | 57 KB | ##8 | 28% \n", "s3transfer-0.5.0 | 57 KB | ########## | 100% \n", "\n", "conda-4.12.0 | 14.5 MB | | 0% \n", "conda-4.12.0 | 14.5 MB | 2 | 3% \n", "conda-4.12.0 | 14.5 MB | 7 | 8% \n", "conda-4.12.0 | 14.5 MB | #4 | 14% \n", "conda-4.12.0 | 14.5 MB | ## | 20% \n", "conda-4.12.0 | 14.5 MB | ##5 | 26% \n", "conda-4.12.0 | 14.5 MB | ### | 31% \n", "conda-4.12.0 | 14.5 MB | ###6 | 36% \n", "conda-4.12.0 | 14.5 MB | #### | 41% \n", "conda-4.12.0 | 14.5 MB | ####5 | 46% \n", "conda-4.12.0 | 14.5 MB | ##### | 50% \n", "conda-4.12.0 | 14.5 MB | #####4 | 54% \n", "conda-4.12.0 | 14.5 MB | #####8 | 59% \n", "conda-4.12.0 | 14.5 MB | ######3 | 63% \n", "conda-4.12.0 | 14.5 MB | ######7 | 67% \n", "conda-4.12.0 | 14.5 MB | #######2 | 72% \n", "conda-4.12.0 | 14.5 MB | #######6 | 77% \n", "conda-4.12.0 | 14.5 MB | ########2 | 83% \n", "conda-4.12.0 | 14.5 MB | ########8 | 88% \n", "conda-4.12.0 | 14.5 MB | #########5 | 96% \n", "conda-4.12.0 | 14.5 MB | ########## | 100% \n", "\n", "botocore-1.23.24 | 4.2 MB | | 0% \n", "botocore-1.23.24 | 4.2 MB | #1 | 12% \n", "botocore-1.23.24 | 4.2 MB | ##5 | 25% \n", "botocore-1.23.24 | 4.2 MB | ####1 | 41% \n", "botocore-1.23.24 | 4.2 MB | #####6 | 57% \n", "botocore-1.23.24 | 4.2 MB | #######4 | 75% \n", "botocore-1.23.24 | 4.2 MB | ######### | 91% \n", "botocore-1.23.24 | 4.2 MB | ########## | 100% \n", "\n", "boto3-1.20.24 | 71 KB | | 0% \n", "boto3-1.20.24 | 71 KB | ##2 | 23% \n", "boto3-1.20.24 | 71 KB | ########## | 100% \n", "boto3-1.20.24 | 71 KB | ########## | 100% \n", "\n", "smart_open-1.9.0 | 59 KB | | 0% \n", "smart_open-1.9.0 | 59 KB | ########## | 100% \n", "smart_open-1.9.0 | 59 KB | ########## | 100% \n", "\n", "bz2file-0.98 | 247 KB | | 0% \n", "bz2file-0.98 | 247 KB | ########## | 100% \n", "bz2file-0.98 | 247 KB | ########## | 100% \n", "\n", "gensim-3.8.3 | 18.4 MB | | 0% \n", "gensim-3.8.3 | 18.4 MB | | 1% \n", "gensim-3.8.3 | 18.4 MB | 3 | 3% \n", "gensim-3.8.3 | 18.4 MB | 6 | 7% \n", "gensim-3.8.3 | 18.4 MB | # | 10% \n", "gensim-3.8.3 | 18.4 MB | #3 | 14% \n", "gensim-3.8.3 | 18.4 MB | #6 | 17% \n", "gensim-3.8.3 | 18.4 MB | #9 | 20% \n", "gensim-3.8.3 | 18.4 MB | ##3 | 23% \n", "gensim-3.8.3 | 18.4 MB | ##6 | 26% \n", "gensim-3.8.3 | 18.4 MB | ### | 30% \n", "gensim-3.8.3 | 18.4 MB | ###3 | 34% \n", "gensim-3.8.3 | 18.4 MB | ###7 | 38% \n", "gensim-3.8.3 | 18.4 MB | #### | 41% \n", "gensim-3.8.3 | 18.4 MB | ####4 | 45% \n", "gensim-3.8.3 | 18.4 MB | ####9 | 49% \n", "gensim-3.8.3 | 18.4 MB | #####3 | 54% \n", "gensim-3.8.3 | 18.4 MB | #####7 | 58% \n", "gensim-3.8.3 | 18.4 MB | ######1 | 62% \n", "gensim-3.8.3 | 18.4 MB | ######5 | 66% \n", "gensim-3.8.3 | 18.4 MB | ####### | 70% \n", "gensim-3.8.3 | 18.4 MB | #######4 | 74% \n", "gensim-3.8.3 | 18.4 MB | #######8 | 79% \n", "gensim-3.8.3 | 18.4 MB | ########3 | 83% \n", "gensim-3.8.3 | 18.4 MB | ########7 | 87% \n", "gensim-3.8.3 | 18.4 MB | #########1 | 91% \n", "gensim-3.8.3 | 18.4 MB | #########7 | 97% \n", "gensim-3.8.3 | 18.4 MB | ########## | 100% \n", "Preparing transaction: ...working... done\n", "Verifying transaction: ...working... done\n", "Executing transaction: ...working... done\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "26795353", "metadata": {}, "outputs": [], "source": [ "# conda install -c anaconda nltk" ] }, { "cell_type": "raw", "id": "0bfcf5ac", "metadata": {}, "source": [ "Collecting package metadata (current_repodata.json): ...working... done\n", "Solving environment: ...working... done\n", "\n", "# All requested packages already installed.\n", "\n", "Note: you may need to restart the kernel to use updated packages." ] }, { "cell_type": "code", "execution_count": 3, "id": "577d479f", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[nltk_data] Downloading package gutenberg to\n", "[nltk_data] C:\\Users\\sayed\\AppData\\Roaming\\nltk_data...\n", "[nltk_data] Package gutenberg is already up-to-date!\n" ] } ], "source": [ "import nltk\n", "nltk.download('gutenberg');" ] }, { "cell_type": "code", "execution_count": 4, "id": "8731768a", "metadata": {}, "outputs": [], "source": [ "data = nltk.corpus.gutenberg.raw('shakespeare-hamlet.txt');" ] }, { "cell_type": "code", "execution_count": 5, "id": "b96bce4e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'[The Tragedie of Hamlet by William Shakespeare 1599]\\n\\n\\nActus Primus. Scoena Prima.\\n\\nEnter Barnardo a'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data[:100]" ] }, { "cell_type": "code", "execution_count": 6, "id": "865c8ed0", "metadata": {}, "outputs": [], "source": [ "from gensim.summarization import summarize" ] }, { "cell_type": "code", "execution_count": 7, "id": "80dda0cd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "And let thine eye looke like a Friend on Denmarke.\n", "Ham. I am very glad to see you: good euen Sir. But what in faith make you from Wittemberge?\n", "Ham. I pray thee doe not mock me (fellow Student)\n", "Ham. For Heauens loue let me heare\n", "Hor. As I doe liue my honourd Lord 'tis true;\n", "Ham. It wafts me still: goe on, Ile follow thee\n", "Ham. Neuer make known what you haue seen to night\n", "Ham. Neuer to speake of this that you haue seene.\n", "I know the good King & Queene haue sent for you\n", "Ham. Nay then I haue an eye of you: if you loue me\n", "Ham. My Lord, I haue Newes to tell you.\n", "'Tis well, Ile haue thee speake out the rest,\n", "Ham. If thou doest Marry, Ile giue thee this Plague\n", "Ham. We shall know by these Fellowes: the Players\n", "Ham. Oh good Horatio, Ile take the Ghosts word for\n", "Ham. We shall obey, were she ten times our Mother.\n", "Ham. Mother, you haue my Father much offended\n", "Ham. Come show me what thou'lt doe.\n", "Ham. As th'art a man, giue me the Cup. Let go, by Heauen Ile haue't.\n" ] } ], "source": [ "summary = summarize(data, word_count = 200)\n", "print (summary);" ] }, { "cell_type": "code", "execution_count": 8, "id": "40793e34", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"And let vs heare Barnardo speake of this\\nIn the same figure, like the King that's dead\\nMar. Thou art a Scholler; speake to it Horatio\\nLookes it not like the King?\\nHor. What art thou that vsurp'st this time of night,\\nDid sometimes march: By Heauen I charge thee speake\\nHor. As thou art to thy selfe,\\nHor. In what particular thought to work, I know not:\\nMar. Good now sit downe, & tell me he that knowes\\nOr, if thou hast vp-hoorded in thy life\\nHor. And then it started, like a guilty thing\\nHor. So haue I heard, and do in part beleeue it.\\nLet vs impart what we haue seene to night\\nMar. Let do't I pray, and I this morning know\\nEnter Claudius King of Denmarke, Gertrude the Queene, Hamlet,\\nLaertes, and his Sister Ophelia, Lords Attendant.\\nWhat would'st thou haue Laertes?\\nHaue you your Fathers leaue?\\nPol. He hath my Lord:\\nTake thy faire houre Laertes, time be thine,\\nHam. A little more then kin, and lesse then kinde\\nHam. Not so my Lord, I am too much i'th' Sun\\nGood Hamlet cast thy nightly colour off\"" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "summary = summarize(data, ratio=0.10)\n", "summary[:1000]" ] }, { "cell_type": "code", "execution_count": 9, "id": "7183c94e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'A'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "summary[:1]" ] }, { "cell_type": "code", "execution_count": 15, "id": "63bc3ab8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'A mother and daughter from Ukraine thought they had secured a place to live in Canada to escape the war in their home country. But they ended up being scammed out of $2,000 and stranded at the airport in Toronto.\\n\\nAlena Neklyudova and her nine-year-old daughter, Sasha Volkova, left their family behind, including Neklyudova\\'s husband and a 19-year-old son, who have been conscripted to fight for Ukraine during the Russian invasion.\\n\\nWhile still in Ukraine, they shuttled from place to place to avoid the war, taking shelter from bombs in a friend\\'s apartment building or in random basements.\\n\\n\"I still have nightmares from it,\" Sasha told CBC Kitchener-Waterloo. \\n\\nThey travelled through Moldova, Romania and Bulgaria before ending up in Turkey — all in one night — before catching a flight to Toronto in March.\\n\\nNeklyudova said she pre-paid $2,000 to arrange for a place to stay in Montreal before she and her daughter flew to Canada. The package deal included a pickup in Toronto, transportation to Montreal and temporary housing.\\n\\nBut when they landed at Pearson International Airport on March 17, no one was there to greet them, and it became clear that whoever took her money was not going to show up. \\n\\nWith nowhere to go, Neklyudova made a frantic post on the Facebook page Canada-Hosts Ukrainians, asking for help.\\n\\n\\nNeklyudova had reached out for help on the Facebook group Canada-Host Ukrainians after she had nowhere to go when she and her daughter landed in Toronto on March 17. (Canada-Host Ukrainians Facebook group)\\nDozens of people responded to her message, including \"Sergey\" in Cambridge, Ont. CBC News has agreed to grant him confidentiality because of concerns about security and safety.\\n\\nSergey discovered Neklyudova\\'s post while scanning various Facebook groups to learn how he could help the refugee effort in Ukraine.\\n\\n\"This was literally an SOS call, like \\'Save Our Selves\\' because they really didn\\'t have anywhere to go,\" he said. \\n\\nAfter losing $7K in a scam, this newcomer to Canada has words of caution and a positive spin\\nSergey speaks Russian and also has relatives in Ukraine who have been affected by the war. He said after a quick introduction and exchange of information, he went to the airport the next day to pick up the pair.\\n\\n\"When she came here, both of them, they looked disoriented,\" he said.\\n\\n\"Like, \\'Where are we? What are we doing here? Should we maybe go back?\\'\" Sergey said, translating for Neklyudova.\\n\\n\\nSasha plays with a house cat. She says she is looking forward to starting classes at a new school in Cambridge but admits she\\'s a little nervous. (Carmen Groleau/CBC)\\n\\'They actually started smiling\\'\\nNeklyudova and her daughter have been staying with Sergey and his family of six for almost two weeks. He has helped her get settled, whether it\\'s driving them to appointments or enrolling Sasha in school.\\n\\nSasha said she\\'s excited to go back to school but admits she\\'s a little nervous. Neklyudova is also eager to find work and improve her English. \\n\\n\"I am very happy,\" Neklyudova said since connecting with Sergey and his family. \\n\\nWhat K-W is doing to support arrival of Ukrainian refugees\\n\"They actually started smiling after being here and connecting with some people in the community and even with our neighbours,\" Sergey added.\\n\\nNeklyudova said she\\'s thankful with how supportive the community has been since they arrived in Cambridge and hopes she can reunite with her family once things settle in Ukraine.\\n\\n\"I didn\\'t have hope at all a week ago, but now I do,\" she said.\\n\\nAs for the $2,000, there\\'s little hope of seeing that again.'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import io;\n", "file = open('summarytext.txt', 'r');\n", "data = file.read();\n", "data" ] }, { "cell_type": "code", "execution_count": 16, "id": "6759f139", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Neklyudova said she pre-paid $2,000 to arrange for a place to stay in Montreal before she and her daughter flew to Canada.\n", "Neklyudova had reached out for help on the Facebook group Canada-Host Ukrainians after she had nowhere to go when she and her daughter landed in Toronto on March 17.\n", "Sergey discovered Neklyudova's post while scanning various Facebook groups to learn how he could help the refugee effort in Ukraine.\n", "Neklyudova said she's thankful with how supportive the community has been since they arrived in Cambridge and hopes she can reunite with her family once things settle in Ukraine.\n" ] } ], "source": [ "summary = summarize(data, word_count = 100)\n", "print (summary);" ] }, { "cell_type": "code", "execution_count": 19, "id": "c8da6397", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Neklyudova said she pre-paid $2,000 to arrange for a place to stay in Montreal before she and her daughter flew to Canada.\\nWith nowhere to go, Neklyudova made a frantic post on the Facebook page Canada-Hosts Ukrainians, asking for help.\\nNeklyudova had reached out for help on the Facebook group Canada-Host Ukrainians after she had nowhere to go when she and her daughter landed in Toronto on March 17.\\nSergey discovered Neklyudova\\'s post while scanning various Facebook groups to learn how he could help the refugee effort in Ukraine.\\nNeklyudova and her daughter have been staying with Sergey and his family of six for almost two weeks.\\n\"I am very happy,\" Neklyudova said since connecting with Sergey and his family.\\nNeklyudova said she\\'s thankful with how supportive the community has been since they arrived in Cambridge and hopes she can reunite with her family once things settle in Ukraine.'" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "summary = summarize(data, ratio=0.2);\n", "summary" ] }, { "cell_type": "code", "execution_count": null, "id": "24242c6c", "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": 5 }