{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from pythainlp.util import normalize" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "62143" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "words = []\n", "with open(\"words_th.txt\", \"r\") as f:\n", " words = f.readlines()\n", "words = [w.strip() for w in words]\n", "len(words)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['ก ข ไม่กระดิกหู', 'ก.', 'ก.ค.', 'ก.ต.', 'ก.ป.ส.']" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "words[0:5]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "62143" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "normalized_words = [normalize(w) for w in words]\n", "len(normalized_words)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['ก ข ไม่กระดิกหู', 'ก.', 'ก.ค.', 'ก.ต.', 'ก.ป.ส.']" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "normalized_words[0:5]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "62137" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_words = []\n", "\n", "# remove duplicates, keep same order\n", "for w in normalized_words:\n", " if w not in new_words:\n", " new_words.append(w)\n", "\n", "len(new_words)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['ก ข ไม่กระดิกหู', 'ก.', 'ก.ค.', 'ก.ต.', 'ก.ป.ส.']" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_words[0:5]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "with open(\"words_th_new.txt\", \"w\") as f:\n", " for w in new_words:\n", " f.write(w)\n", " f.write(\"\\n\")" ] } ], "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.9.5" } }, "nbformat": 4, "nbformat_minor": 4 }