{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Using regular expressions to search texts for patterns\n", "\n", "### Dowload sample corpora" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%time\n", "!wget -c https://object.pouta.csc.fi/OPUS-OpenSubtitles/v2018/tmx/en-zh_tw.tmx.gz" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%time\n", "!wget -c http://nlp.csie.org/~rubentsui/files/TranslationTech1082/Regex/nytimes.txt.gz" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%!\n", "gunzip en-zh_tw.tmx.gz\n", "gunzip nytimes.txt.gz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Search OpenSubtitles 2018" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%time\n", "phrase = '發酒瘋'; corpus = 'en-zh_tw.tmx'\n", "!fgrep -B1 \"$phrase\" $corpus --color=always | sed '/--/d' " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Search sample New York Times corpus" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%time\n", "regex = 'performance[- ]enhanc\\w+ \\w+'\n", "corpus = 'nytimes.txt'\n", "print(\"Phrases matched and their frequencies:\")\n", "!egrep -i -o \"$regex\" $corpus | sort | uniq -c | sort -nr\n", "print(\"\\nConcordancer output:\")\n", "!egrep -i -A1 \"$regex\" $corpus --color=always " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }