{ "cells": [ { "cell_type": "markdown", "id": "coastal-lincoln", "metadata": {}, "source": [ "![Logo 1](https://git.wmi.amu.edu.pl/AITech/Szablon/raw/branch/master/Logotyp_AITech1.jpg)\n", "
\n", "

Komputerowe wspomaganie tłumaczenia

\n", "

3. Terminologia [laboratoria]

\n", "

Rafał Jaworski (2021)

\n", "
\n", "\n", "![Logo 2](https://git.wmi.amu.edu.pl/AITech/Szablon/raw/branch/master/Logotyp_AITech2.jpg)" ] }, { "cell_type": "markdown", "id": "aggregate-listing", "metadata": {}, "source": [ "Na dzisiejszych zajęciach zajmiemy się bliżej słownikami używanymi do wspomagania tłumaczenia. Oczywiście na rynku dostępnych jest bardzo wiele słowników w formacie elektronicznym. Wiele z nich jest gotowych do użycia w SDL Trados, memoQ i innych narzędziach CAT. Zawierają one setki tysięcy lub miliony haseł i oferują natychmiastową pomoc tłumaczowi." ] }, { "cell_type": "markdown", "id": "israeli-excuse", "metadata": {}, "source": [ "Problem jednak w tym, iż często nie zawierają odpowiedniej terminologii specjalistycznej - używanej przez klienta zamawiającego tłumaczenie. Terminy specjalistyczne są bardzo częste w tekstach tłumaczonych ze względu na następujące zjawiska:\n", "- Teksty o tematyce ogólnej są tłumaczone dość rzadko (nikt nie tłumaczy pocztówek z pozdrowieniami z wakacji...)\n", "- Te same słowa mogą mieć zarówno znaczenie ogólne, jak i bardzo specjalistyczne (np. \"dziedziczenie\" w kontekście prawnym lub informatycznym)\n", "- Klient używa nazw lub słów wymyślonych przez siebie, np. na potrzeby marketingowe." ] }, { "cell_type": "markdown", "id": "reflected-enforcement", "metadata": {}, "source": [ "Nietrywialnymi zadaniami stają się: odnalezienie terminu specjalistycznego w tekście źródłowym oraz podanie prawidłowego tłumaczenia tego terminu na język docelowy" ] }, { "cell_type": "markdown", "id": "statutory-florist", "metadata": {}, "source": [ "Brzmi prosto? Spróbujmy wykonać ręcznie tę drugą operację." ] }, { "cell_type": "markdown", "id": "danish-anchor", "metadata": {}, "source": [ "### Ćwiczenie 1: Podaj tłumaczenie terminu \"prowadnice szaf metalowych\" na język angielski. Opisz, z jakich narzędzi skorzystałaś/eś." ] }, { "cell_type": "markdown", "id": "diverse-sunglasses", "metadata": {}, "source": [ "Odpowiedź: Wynik z Google Translate to `metal cabinet guides`" ] }, { "cell_type": "markdown", "id": "limited-waterproof", "metadata": {}, "source": [ "W dalszych ćwiczeniach skupimy się jednak na odszukaniu terminu specjalistycznego w tekście. W tym celu będą potrzebne dwie operacje:\n", "1. Przygotowanie słownika specjalistycznego.\n", "2. Detekcja terminologii przy użyciu przygotowanego słownika specjalistycznego." ] }, { "cell_type": "markdown", "id": "literary-blues", "metadata": {}, "source": [ "Zajmijmy się najpierw krokiem nr 2 (gdyż jest prostszy). Rozważmy następujący tekst:" ] }, { "cell_type": "code", "execution_count": 11, "id": "loving-prince", "metadata": {}, "outputs": [], "source": [ "text = \" For all Java programmers:\"\n", "text += \" This section explains how to compile and run a Swing application from the command line.\"\n", "text += \" For information on compiling and running a Swing application using NetBeans IDE,\"\n", "text += \" see Running Tutorial Examples in NetBeans IDE. The compilation instructions work for all Swing programs\"\n", "text += \" — applets, as well as applications. Here are the steps you need to follow:\"\n", "text += \" Install the latest release of the Java SE platform, if you haven't already done so.\"\n", "text += \" Create a program that uses Swing components. Compile the program. Run the program.\"" ] }, { "cell_type": "markdown", "id": "extreme-cycling", "metadata": {}, "source": [ "Załóżmy, że posiadamy następujący słownik:" ] }, { "cell_type": "code", "execution_count": 12, "id": "bound-auction", "metadata": {}, "outputs": [], "source": [ "dictionary = ['program', 'application', 'applet' 'compile']" ] }, { "cell_type": "markdown", "id": "other-trinidad", "metadata": {}, "source": [ "### Ćwiczenie 2: Napisz program, który wypisze pozycje wszystkich wystąpień poszczególnych terminów specjalistycznych. Dla każdego terminu należy wypisać listę par (pozycja_startowa, pozycja końcowa)." ] }, { "cell_type": "code", "execution_count": 13, "id": "cognitive-cedar", "metadata": {}, "outputs": [], "source": [ "def terminology_lookup():\n", " for term in dictionary:\n", " start = 0\n", " while True:\n", " start = text.find(term, start)\n", " if start == -1:\n", " break\n", " end = start + len(term)\n", " print(f'{term}: ({start}, {end})')\n", " start = end" ] }, { "cell_type": "code", "execution_count": 14, "id": "0a4a26ba", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "program: (14, 21)\n", "program: (291, 298)\n", "program: (468, 475)\n", "program: (516, 523)\n", "program: (533, 540)\n", "application: (80, 91)\n", "application: (164, 175)\n", "application: (322, 333)\n" ] } ], "source": [ "terminology_lookup()" ] }, { "cell_type": "markdown", "id": "interior-things", "metadata": {}, "source": [ "Zwykłe wyszukiwanie w tekście ma pewne wady. Na przykład, gdy szukaliśmy słowa \"program\", złapaliśmy przypadkiem słowo \"programmer\". Złapaliśmy także słowo \"programs\", co jest poprawne, ale niepoprawnie podaliśmy jego pozycję w tekście." ] }, { "cell_type": "markdown", "id": "aggressive-plane", "metadata": {}, "source": [ "Żeby poradzić sobie z tymi problemami, musimy wykorzystać techniki przetwarzania języka naturalnego. Wypróbujmy pakiet spaCy:\n", "\n", "`pip3 install spacy`\n", "\n", "oraz\n", "\n", "`python3 -m spacy download en_core_web_sm`" ] }, { "cell_type": "code", "execution_count": 15, "id": "tribal-attention", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " for all Java programmer : this section explain how to compile and run a swing application from the command line . for information on compile and run a swing application use NetBeans IDE , see run Tutorial Examples in NetBeans IDE . the compilation instruction work for all Swing program — applet , as well as application . here be the step you need to follow : install the late release of the Java SE platform , if you have not already do so . create a program that use swing component . compile the program . run the program . " ] } ], "source": [ "import spacy\n", "nlp = spacy.load(\"en_core_web_sm\")\n", "\n", "doc = nlp(text)\n", "\n", "for token in doc:\n", " print(token.lemma_, end=' ')" ] }, { "cell_type": "markdown", "id": "regional-craft", "metadata": {}, "source": [ "Sukces! Nastąpił podział tekstu na słowa (tokenizacja) oraz sprowadzenie do formy podstawowej każdego słowa (lematyzacja)." ] }, { "cell_type": "markdown", "id": "toxic-subsection", "metadata": {}, "source": [ "### Ćwiczenie 3: Zmodyfikuj program z ćwiczenia 2 tak, aby zwracał również odmienione słowa. Na przykład, dla słowa \"program\" powinien znaleźć również \"programs\", ustawiając pozycje w tekście odpowiednio dla słowa \"programs\". Wykorzystaj właściwość idx tokenu." ] }, { "cell_type": "code", "execution_count": 40, "id": "surgical-demonstration", "metadata": {}, "outputs": [], "source": [ "def terminology_lookup():\n", " for term in dictionary:\n", " for token in doc:\n", " if token.lemma_ == term:\n", " print(f'{token}: ({token.idx}, {token.idx + len(token)})')" ] }, { "cell_type": "code", "execution_count": 39, "id": "74f600ea", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "programs: (291, 299)\n", "program: (468, 475)\n", "program: (516, 523)\n", "program: (533, 540)\n", "application: (80, 91)\n", "application: (164, 175)\n", "applications: (322, 334)\n" ] } ], "source": [ "terminology_lookup()" ] }, { "cell_type": "markdown", "id": "straight-letter", "metadata": {}, "source": [ "Teraz czas zająć się problemem przygotowania słownika specjalistycznego. W tym celu napiszemy nasz własny ekstraktor terminologii. Wejściem do ekstraktora będzie tekst zawierający specjalistyczną terminologię. Wyjściem - lista terminów." ] }, { "cell_type": "markdown", "id": "nearby-frontier", "metadata": {}, "source": [ "Przyjmijmy następujące podejście - terminami specjalistycznymi będą najcześćiej występujące rzeczowniki w tekście. Wykonajmy krok pierwszy:" ] }, { "cell_type": "markdown", "id": "harmful-lightning", "metadata": {}, "source": [ "### Ćwiczenie 4: Wypisz wszystkie rzeczowniki z tekstu. Wykorzystaj możliwości spaCy." ] }, { "cell_type": "code", "execution_count": 22, "id": "superb-butterfly", "metadata": {}, "outputs": [], "source": [ "def get_nouns(text):\n", " doc = nlp(text)\n", " return [token.text for token in doc if token.pos_ == 'NOUN']" ] }, { "cell_type": "code", "execution_count": 23, "id": "2bfedfa3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['programmers',\n", " 'section',\n", " 'Swing',\n", " 'application',\n", " 'command',\n", " 'line',\n", " 'information',\n", " 'Swing',\n", " 'application',\n", " 'compilation',\n", " 'instructions',\n", " 'programs',\n", " 'applets',\n", " 'applications',\n", " 'steps',\n", " 'release',\n", " 'platform',\n", " 'program',\n", " 'Swing',\n", " 'components',\n", " 'program',\n", " 'program']" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "get_nouns(text)" ] }, { "cell_type": "markdown", "id": "musical-creator", "metadata": {}, "source": [ "Teraz czas na podliczenie wystąpień poszczególnych rzeczowników. Uwaga - różne formy tego samego słowa zliczamy razem jako wystąpienia tego słowa (np. \"program\" i \"programs\"). Najwygodniejszą metodą podliczania jest zastosowanie tzw. tally (po polsku \"zestawienie\"). Jest to słownik, którego kluczem jest słowo w formie podstawowej, a wartością liczba wystąpień tego słowa, wliczając słowa odmienione. Przykład gotowego tally:" ] }, { "cell_type": "code", "execution_count": 19, "id": "acting-tolerance", "metadata": {}, "outputs": [], "source": [ "tally = {\"program\" : 4, \"component\" : 1}" ] }, { "cell_type": "markdown", "id": "vanilla-estimate", "metadata": {}, "source": [ "### Ćwiczenie 5: Napisz program do ekstrakcji terminologii z tekstu według powyższych wytycznych." ] }, { "cell_type": "code", "execution_count": 26, "id": "eight-redhead", "metadata": {}, "outputs": [], "source": [ "def extract_terms(text):\n", " doc = nlp(text)\n", " terms = {}\n", " for token in doc:\n", " if token.pos_ == 'NOUN':\n", " term = token.lemma_\n", " terms[term] = terms.get(term, 0) + 1\n", " return terms" ] }, { "cell_type": "code", "execution_count": 27, "id": "07c1122a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'programmer': 1,\n", " 'section': 1,\n", " 'swing': 3,\n", " 'application': 3,\n", " 'command': 1,\n", " 'line': 1,\n", " 'information': 1,\n", " 'compilation': 1,\n", " 'instruction': 1,\n", " 'program': 4,\n", " 'applet': 1,\n", " 'step': 1,\n", " 'release': 1,\n", " 'platform': 1,\n", " 'component': 1}" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "extract_terms(text)" ] }, { "cell_type": "markdown", "id": "loaded-smell", "metadata": {}, "source": [ "### Ćwiczenie 6: Rozszerz powyższy program o ekstrację czasowników i przymiotników." ] }, { "cell_type": "code", "execution_count": 32, "id": "monetary-mambo", "metadata": {}, "outputs": [], "source": [ "# Extract and count nouns, verbs and adjectives\n", "def extract_terms(text):\n", " doc = nlp(text)\n", " terms = {\"nouns\": {}, \"verbs\": {}, \"adjectives\": {}}\n", " for token in doc:\n", " if token.pos_ == 'NOUN':\n", " term = token.lemma_\n", " terms[\"nouns\"][term] = terms[\"nouns\"].get(term, 0) + 1\n", " elif token.pos_ == 'VERB':\n", " term = token.lemma_\n", " terms[\"verbs\"][term] = terms[\"verbs\"].get(term, 0) + 1\n", " elif token.pos_ == 'ADJ':\n", " term = token.lemma_\n", " terms[\"adjectives\"][term] = terms[\"adjectives\"].get(term, 0) + 1\n", "\n", " return terms" ] }, { "cell_type": "code", "execution_count": 35, "id": "1eb48136", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'adjectives': {'late': 1},\n", " 'nouns': {'applet': 1,\n", " 'application': 3,\n", " 'command': 1,\n", " 'compilation': 1,\n", " 'component': 1,\n", " 'information': 1,\n", " 'instruction': 1,\n", " 'line': 1,\n", " 'platform': 1,\n", " 'program': 4,\n", " 'programmer': 1,\n", " 'release': 1,\n", " 'section': 1,\n", " 'step': 1,\n", " 'swing': 3},\n", " 'verbs': {'compile': 3,\n", " 'create': 1,\n", " 'do': 1,\n", " 'explain': 1,\n", " 'follow': 1,\n", " 'install': 1,\n", " 'need': 1,\n", " 'run': 4,\n", " 'see': 1,\n", " 'use': 2,\n", " 'work': 1}}\n" ] } ], "source": [ "from pprint import pprint\n", "\n", "pprint(extract_terms(text))" ] }, { "cell_type": "code", "execution_count": null, "id": "62aeea83", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "author": "Rafał Jaworski", "email": "rjawor@amu.edu.pl", "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "lang": "pl", "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.10.14" }, "subtitle": "3. Terminologia", "title": "Komputerowe wspomaganie tłumaczenia", "year": "2021" }, "nbformat": 4, "nbformat_minor": 5 }