diff --git a/notebooks/pythainlp-get-started.ipynb b/notebooks/pythainlp-get-started.ipynb index bc131bd4f..956a959b5 100644 --- a/notebooks/pythainlp-get-started.ipynb +++ b/notebooks/pythainlp-get-started.ipynb @@ -386,6 +386,88 @@ "cell_type": "code", "execution_count": 15, "metadata": {}, + "outputs": [], + "source": [ + "speedtest_text = \"\"\"\n", + "ครบรอบ 14 ปี ตากใบ เช้าวันนั้น 25 ต.ค. 2547 ผู้ชุมนุมชายกว่า 1,370 คน\n", + "ถูกโยนขึ้นรถยีเอ็มซี 22 หรือ 24 คัน นอนซ้อนกันคันละ 4-5 ชั้น เดินทางจากสถานีตำรวจตากใบ ไปไกล 150 กิโลเมตร\n", + "ไปถึงค่ายอิงคยุทธบริหาร ใช้เวลากว่า 6 ชั่วโมง / ในอีกคดีที่ญาติฟ้องร้องรัฐ คดีจบลงที่การประนีประนอมยอมความ\n", + "กระทรวงกลาโหมจ่ายค่าสินไหมทดแทนรวม 42 ล้านบาทให้กับญาติผู้เสียหาย 79 ราย\n", + "ปิดหีบและนับคะแนนเสร็จแล้ว ที่หน่วยเลือกตั้งที่ 32 เขต 13 แขวงหัวหมาก เขตบางกะปิ กรุงเทพมหานคร\n", + "ผู้สมัคร ส.ส. และตัวแทนพรรคการเมืองจากหลายพรรคต่างมาเฝ้าสังเกตการนับคะแนนอย่างใกล้ชิด โดย\n", + "ฐิติภัสร์ โชติเดชาชัยนันต์ จากพรรคพลังประชารัฐ และพริษฐ์ วัชรสินธุ จากพรรคประชาธิปัตย์ได้คะแนน\n", + "96 คะแนนเท่ากัน\n", + "เช้าวันอาทิตย์ที่ 21 เมษายน 2019 ซึ่งเป็นวันอีสเตอร์ วันสำคัญของชาวคริสต์\n", + "เกิดเหตุระเบิดต่อเนื่องในโบสถ์คริสต์และโรงแรมอย่างน้อย 7 แห่งในประเทศศรีลังกา\n", + "มีผู้เสียชีวิตแล้วอย่างน้อย 156 คน และบาดเจ็บหลายร้อยคน ยังไม่มีข้อมูลว่าผู้ก่อเหตุมาจากฝ่ายใด\n", + "จีนกำหนดจัดการประชุมข้อริเริ่มสายแถบและเส้นทางในช่วงปลายสัปดาห์นี้ ปักกิ่งยืนยันว่า\n", + "อภิมหาโครงการเชื่อมโลกของจีนไม่ใช่เครื่องมือแผ่อิทธิพล แต่ยินดีรับฟังข้อวิจารณ์ เช่น ประเด็นกับดักหนี้สิน\n", + "และความไม่โปร่งใส รัฐบาลปักกิ่งบอกว่า เวทีประชุม Belt and Road Forum ในช่วงวันที่ 25-27 เมษายน\n", + "ถือเป็นงานการทูตที่สำคัญที่สุดของจีนในปี 2019\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CPU times: user 1.05 s, sys: 8.68 ms, total: 1.06 s\n", + "Wall time: 1.08 s\n" + ] + } + ], + "source": [ + "# Speed test: Calling \"longest\" engine through word_tokenize wrapper\n", + "%time tokens = word_tokenize(speedtest_text, engine=\"longest\")" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CPU times: user 11.6 ms, sys: 235 µs, total: 11.8 ms\n", + "Wall time: 11.8 ms\n" + ] + } + ], + "source": [ + "# Speed test: Calling \"newmm\" engine through word_tokenize wrapper\n", + "%time tokens = word_tokenize(speedtest_text, engine=\"newmm\")" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CPU times: user 10.6 ms, sys: 562 µs, total: 11.1 ms\n", + "Wall time: 12.4 ms\n" + ] + } + ], + "source": [ + "# Speed test: Directly call \"newmm\" engine from pythainlp.tokenize.newmm\n", + "%time tokens = pythainlp.tokenize.newmm.segment(speedtest_text)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, "outputs": [ { "data": { @@ -407,15 +489,16 @@ " 'มี|ความเป็นไปได้|อย่างไรบ้าง|']" ] }, - "execution_count": 15, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ + "# Get all possible segmentations\n", "from pythainlp.tokenize.multi_cut import find_all_segment, mmcut, segment\n", "\n", - "find_all_segment(\"มีความเป็นไปได้อย่างไรบ้าง\")\n" + "find_all_segment(\"มีความเป็นไปได้อย่างไรบ้าง\")" ] }, { @@ -429,7 +512,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 20, "metadata": {}, "outputs": [ { @@ -438,7 +521,7 @@ "['ป', 'ระ', 'เท', 'ศ', 'ไท', 'ย']" ] }, - "execution_count": 16, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } @@ -458,7 +541,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 21, "metadata": {}, "outputs": [ { @@ -467,7 +550,7 @@ "['ป', 'ระ', 'เท', 'ศ', 'ไท', 'ย']" ] }, - "execution_count": 17, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } @@ -480,7 +563,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 22, "metadata": {}, "outputs": [ { @@ -489,7 +572,7 @@ "{1, 3, 5, 6, 8, 9}" ] }, - "execution_count": 18, + "execution_count": 22, "metadata": {}, "output_type": "execute_result" } @@ -500,7 +583,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 23, "metadata": {}, "outputs": [ { @@ -525,7 +608,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 24, "metadata": {}, "outputs": [ { @@ -534,7 +617,7 @@ "'maeo'" ] }, - "execution_count": 20, + "execution_count": 24, "metadata": {}, "output_type": "execute_result" } @@ -547,7 +630,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 25, "metadata": {}, "outputs": [ { @@ -556,7 +639,7 @@ "'mɛːw'" ] }, - "execution_count": 21, + "execution_count": 25, "metadata": {}, "output_type": "execute_result" } @@ -569,7 +652,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 26, "metadata": {}, "outputs": [], "source": [ @@ -586,7 +669,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 27, "metadata": {}, "outputs": [ { @@ -595,7 +678,7 @@ "True" ] }, - "execution_count": 23, + "execution_count": 27, "metadata": {}, "output_type": "execute_result" } @@ -617,7 +700,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 28, "metadata": {}, "outputs": [ { @@ -641,7 +724,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 29, "metadata": {}, "outputs": [ { @@ -681,7 +764,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 30, "metadata": {}, "outputs": [ { @@ -690,7 +773,7 @@ "['เหลียม', 'เหลือม']" ] }, - "execution_count": 26, + "execution_count": 30, "metadata": {}, "output_type": "execute_result" } @@ -704,7 +787,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 31, "metadata": {}, "outputs": [ { @@ -713,7 +796,7 @@ "'เหลียม'" ] }, - "execution_count": 27, + "execution_count": 31, "metadata": {}, "output_type": "execute_result" } @@ -736,7 +819,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 32, "metadata": {}, "outputs": [ { @@ -759,24 +842,24 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "[('แสดงทรรศนะ', 2),\n", - " ('เจ้าอธิการ', 4),\n", - " ('วินิจฉัย', 133),\n", - " ('อ่อนหวาน', 90),\n", - " ('ไตรตรา', 3),\n", - " ('คำๆ', 15),\n", - " ('ปริ่ม', 13),\n", - " ('มนุ', 3),\n", - " ('ส้าง', 5)]" + "[('ดวงๆ', 3),\n", + " ('กระพือ', 6),\n", + " ('อุปสมบท', 17),\n", + " ('หาเช้ากินค่ำ', 14),\n", + " ('จะเห็นได้ว่า', 152),\n", + " ('ยวด', 2),\n", + " ('กล่อม', 182),\n", + " ('เต้า', 37),\n", + " ('จัว', 2)]" ] }, - "execution_count": 29, + "execution_count": 33, "metadata": {}, "output_type": "execute_result" } @@ -794,7 +877,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 34, "metadata": {}, "outputs": [ { @@ -803,7 +886,7 @@ "39977" ] }, - "execution_count": 30, + "execution_count": 34, "metadata": {}, "output_type": "execute_result" } @@ -815,7 +898,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 35, "metadata": {}, "outputs": [ { @@ -824,7 +907,7 @@ "30379" ] }, - "execution_count": 31, + "execution_count": 35, "metadata": {}, "output_type": "execute_result" } @@ -836,7 +919,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 36, "metadata": {}, "outputs": [ { @@ -845,7 +928,7 @@ "76706" ] }, - "execution_count": 32, + "execution_count": 36, "metadata": {}, "output_type": "execute_result" } @@ -857,7 +940,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 37, "metadata": {}, "outputs": [ { @@ -866,7 +949,7 @@ "76700" ] }, - "execution_count": 33, + "execution_count": 37, "metadata": {}, "output_type": "execute_result" } @@ -888,7 +971,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 38, "metadata": {}, "outputs": [ { @@ -897,7 +980,7 @@ "[('การ', 'FIXN'), ('เดินทาง', 'VACT')]" ] }, - "execution_count": 34, + "execution_count": 38, "metadata": {}, "output_type": "execute_result" } @@ -910,7 +993,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 39, "metadata": {}, "outputs": [ { @@ -935,7 +1018,7 @@ " (\"'อธิบดีกรมประชาสัมพันธ์'\", 'NCMN')]]" ] }, - "execution_count": 35, + "execution_count": 39, "metadata": {}, "output_type": "execute_result" } @@ -963,7 +1046,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 40, "metadata": {}, "outputs": [ { @@ -1000,7 +1083,7 @@ " ('บาท', 'NOUN', 'I-MONEY')]" ] }, - "execution_count": 36, + "execution_count": 40, "metadata": {}, "output_type": "execute_result" } @@ -1021,7 +1104,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 41, "metadata": {}, "outputs": [ { @@ -1041,7 +1124,7 @@ "0.99259853" ] }, - "execution_count": 37, + "execution_count": 41, "metadata": {}, "output_type": "execute_result" } @@ -1054,7 +1137,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 42, "metadata": {}, "outputs": [ { @@ -1070,7 +1153,7 @@ "'แมว'" ] }, - "execution_count": 38, + "execution_count": 42, "metadata": {}, "output_type": "execute_result" } @@ -1088,7 +1171,7 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 43, "metadata": {}, "outputs": [ { @@ -1097,7 +1180,7 @@ "'หนึ่งล้านสองแสนสามหมื่นสี่พันห้าร้อยหกสิบเจ็ดล้านแปดแสนเก้าหมื่นหนึ่งร้อยยี่สิบสามบาทสี่สิบห้าสตางค์'" ] }, - "execution_count": 39, + "execution_count": 43, "metadata": {}, "output_type": "execute_result" } @@ -1110,7 +1193,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 44, "metadata": {}, "outputs": [ { @@ -1119,12 +1202,13 @@ "'หนึ่งบาทเก้าสิบเอ็ดสตางค์'" ] }, - "execution_count": 40, + "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ + "# bahttext() will round the satang part\n", "bahttext(1.909)" ] },