" print(f\"\\nNumber of lines: {len(data_de), len(data_en)}\")\n",
"\n",
" pairs = [[de, en] for de, en in zip(data_de, data_en)]\n",
" if reverse:\n",
" pairs = [list(reversed(p)) for p in pairs]\n",
" input_lang = Lang(lang2)\n",
" output_lang = Lang(lang1)\n",
" else:\n",
" input_lang = Lang(lang1)\n",
" output_lang = Lang(lang2)\n",
"\n",
" print(pairs[:10])\n",
" return input_lang, output_lang, pairs\n",
"\n",
"def count_words(string: str) -> int:\n",
" return len(string.split())\n",
"\n",
"\n",
"#data_de_1, data_en_1 = load_data_1()\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"MAX_LENGTH = 10\n",
"\n",
"eng_prefixes = (\n",
" \"i am \", \"i m \",\n",
" \"he is\", \"he s \",\n",
" \"she is\", \"she s \",\n",
" \"you are\", \"you re \",\n",
" \"we are\", \"we re \",\n",
" \"they are\", \"they re \"\n",
")\n",
"\n",
"def filterPair(p):\n",
" return len(p[0].split(' ')) < MAX_LENGTH and \\\n",
" len(p[1].split(' ')) < MAX_LENGTH and \\\n",
" p[1].startswith(eng_prefixes)\n",
"\n",
"\n",
"def filterPairs(pairs):\n",
" return [pair for pair in pairs if filterPair(pair)]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1046809 1046809\n",
"Zeile: 692607\n",
"Deutsch: Momentan haben wir also eine Arbeitsgruppe.\n",
"English: For the time being, then, we have a working group.\n",
"\n",
"Zeile: 821888\n",
"Deutsch: Hier hat der Staat aber noch eine zweite, und ich glaube noch sehr viel wichtigere Verantwortung.\n",
"English: However, the state has a second and, to my mind, far more important responsibility here.\n",
"\n",
"Zeile: 14166\n",
"Deutsch: Die Berichterstatterin hat Änderungen vorgeschlagen, um diesen Vorschlag zu verstärken.\n",
"English: The rapporteur has proposed amendments to reinforce this proposal.\n",
"\n",
"\n",
"Number of lines: (1046809, 1046809)\n",
"[['Resumption of the session', 'Wiederaufnahme der Sitzungsperiode'], ['You have requested a debate on this subject in the course of the next few days, during this part-session.', 'Im Parlament besteht der Wunsch nach einer Aussprache im Verlauf dieser Sitzungsperiode in den nächsten Tagen.'], [\"Please rise, then, for this minute' s silence.\", 'Ich bitte Sie, sich zu einer Schweigeminute zu erheben.'], [\"(The House rose and observed a minute' s silence)\", '(Das Parlament erhebt sich zu einer Schweigeminute.)'], ['Madam President, on a point of order.', 'Frau Präsidentin, zur Geschäftsordnung.'], ['You will be aware from the press and television that there have been a number of bomb explosions and killings in Sri Lanka.', 'Wie Sie sicher aus der Presse und dem Fernsehen wissen, gab es in Sri Lanka mehrere Bombenexplosionen mit zahlreichen Toten.'], ['Yes, Mr Evans, I feel an initiative of the type you have just suggested would be entirely appropriate.', 'Ja, Herr Evans, ich denke, daß eine derartige Initiative durchaus angebracht ist.'], ['If the House agrees, I shall do as Mr Evans has suggested.', 'Wenn das Haus damit einverstanden ist, werde ich dem Vorschlag von Herrn Evans folgen.'], ['Madam President, on a point of order.', 'Frau Präsidentin, zur Geschäftsordnung.'], ['I would like your advice about Rule 143 concerning inadmissibility.', 'Könnten Sie mir eine Auskunft zu Artikel 143 im Zusammenhang mit der Unzulässigkeit geben?']]\n",
"File \u001b[1;32mc:\\Users\\konst\\miniconda3\\envs\\nlp-machine-learning-project\\Lib\\random.py:373\u001b[0m, in \u001b[0;36mRandom.choice\u001b[1;34m(self, seq)\u001b[0m\n\u001b[0;32m 370\u001b[0m \u001b[38;5;66;03m# As an accommodation for NumPy, we don't use \"if not seq\"\u001b[39;00m\n\u001b[0;32m 371\u001b[0m \u001b[38;5;66;03m# because bool(numpy.array()) raises a ValueError.\u001b[39;00m\n\u001b[0;32m 372\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(seq):\n\u001b[1;32m--> 373\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mIndexError\u001b[39;00m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mCannot choose from an empty sequence\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 374\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m seq[\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_randbelow(\u001b[38;5;28mlen\u001b[39m(seq))]\n",
"\u001b[1;31mIndexError\u001b[0m: Cannot choose from an empty sequence"
"Cell \u001b[1;32mIn[5], line 5\u001b[0m, in \u001b[0;36mreadLangs\u001b[1;34m(lang1, lang2, reverse)\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mReading lines...\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 4\u001b[0m \u001b[38;5;66;03m# Read the file and split into lines\u001b[39;00m\n\u001b[1;32m----> 5\u001b[0m lines \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mdata/\u001b[39;49m\u001b[38;5;132;43;01m%s\u001b[39;49;00m\u001b[38;5;124;43m-\u001b[39;49m\u001b[38;5;132;43;01m%s\u001b[39;49;00m\u001b[38;5;124;43m.txt\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m%\u001b[39;49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[43mlang1\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mlang2\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mencoding\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mutf-8\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\u001b[38;5;241m.\u001b[39m\\\n\u001b[0;32m 6\u001b[0m read()\u001b[38;5;241m.\u001b[39mstrip()\u001b[38;5;241m.\u001b[39msplit(\u001b[38;5;124m'\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 8\u001b[0m \u001b[38;5;66;03m# Split every line into pairs and normalize\u001b[39;00m\n\u001b[0;32m 9\u001b[0m pairs \u001b[38;5;241m=\u001b[39m [[normalizeString(s) \u001b[38;5;28;01mfor\u001b[39;00m s \u001b[38;5;129;01min\u001b[39;00m l\u001b[38;5;241m.\u001b[39msplit(\u001b[38;5;124m'\u001b[39m\u001b[38;5;130;01m\\t\u001b[39;00m\u001b[38;5;124m'\u001b[39m)] \u001b[38;5;28;01mfor\u001b[39;00m l \u001b[38;5;129;01min\u001b[39;00m lines]\n",
"\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'data/eng-fra.txt'"