{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## RusAge\n", "Data from RusAge (https://www.kaggle.com/oldaandozerskaya/fiction-corpus-for-agebased-text-classification)\n", "\n", "RusAge: Corpus for Age-Based Text Classification\n", "Russian fiction books' previews with age rating labels." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import nltk\n", "nltk.download('punkt')\n", "nltk.download('stopwords')\n", "from nltk.corpus import stopwords\n", "import string\n", "from nltk.corpus import PlaintextCorpusReader\n", "from nltk.text import Text" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "corpus = PlaintextCorpusReader(\"./rusage/abstracts/\", '.*\\.txt', encoding='utf-8')\n", "corpus_tokens = corpus.words()\n", "print(corpus_tokens[:10])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "files=corpus.fileids()\n", "\n", "for f in files:\n", " print (f)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "freq = nltk.FreqDist(corpus.words())\n", "#common words \n", "print(\"Common Words:\", freq.most_common(10))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "russian_stopwords = stopwords.words(\"russian\") + list(string.punctuation)\n", "\n", "#Preprocess function\n", "def preprocess_text(words):\n", " \n", " wordsFiltered = []\n", "\n", " for w in words:\n", " if w not in russian_stopwords:\n", " wordsFiltered.append(w)\n", "\n", " return wordsFiltered\n", "\n", "\n", "preprocess_corpus = preprocess_text(corpus.words())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "freq = nltk.FreqDist(preprocess_corpus)\n", "#common words \n", "print(\"Common Words:\", freq.most_common(20))\n", "#specific words \n", "print(\"Specific Word: \", freq.get(\"истории\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t = Text(corpus_tokens)\n", "t.concordance('Толстого', lines=20)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.9" } }, "nbformat": 4, "nbformat_minor": 4 }