SlideShare a Scribd company logo
Regular Expression

กับ ภาษาไทย
กรกฎ เชาวะวณิช
Senior Data Scientist
True Digital Group
Thai NLP Meetup #4
Regular Expression (regex)
• เป็น pattern ไว้ประมวลผล text
• match = ตรวจว่าป้อน email ถูกไหม
• search, findall = ค้นเอกสาร หาข้อความแปลกๆ
• replace = ค้น แล้วแทนที่ (sub) หรือลบทิ้ง
• split = ค้น แล้วใช้เป็นตัวตัด ให้เป็นก้อนๆ
องค์ประกอบหลัก
• a, X, 9, ก ตัวมันเอง
• . (จุด) อักษรทุกตัว เว้น n
• w อักษรในคำ a-z, A-Z, 0-9
• s space,tnrf ตรงข้าม S
• d digit = [0-9] บอกกลุ่ม
• [^0-9] กลุ่มตรงข้าม
• + 1 หรือมากกว่า
• * 0 หรือมากกว่า
• ? 0 หรือ 1
• +?, *? เอาสั้นๆ
• {2} {2,} {2,5} {,5}
ตัวอะไร กี่ตัว
wwd{5}AZ10220
ตัวพิเศษ
• ^ เริ่มต้นบรรทัด
• $ จบเนื้อหา b เริ่มหรือจบคำ
•  ใช้นำอักษรพิเศษ เช่น . จะจุดธรรมดา  คือ slash
• ( ) ใช้เก็บเนื้อหา เพื่อดึงออกไปใช้ หรือ ไปแทนที่
• | = หรือ เช่น cat|dog อาจต้องใช้วงเล็บช่วย
Advanced
• non-capture (?:cat|dog)
• look ahead (?=xxx) (?!xxx)
• look behind (?<=xxx) (?<!xxx)
• named group (?P<ชื่อ>เนื้อหา)
• flags (?aiLmsux) เช่น (?s) (?x) เหมือน re.S, re.X
• ต้องเปิด manual ทุกที
• ใน python มีตัวที่เก่งกว่า re ชื่อ regex
Thai Unicode Table
• ก = 0E01
• ๙ = 0E59 ๛ = 0E5B โคมูตร
• [ก-๙] ตัวอักษรไทย
• สระ [ะ-ไ]
• สระหน้า [เ-ไ]
• กลุ่มวรรณยุกต์ลอย [็่้๊๋์ํ]
ตัวอย่าง
อักษรไทยล้วน
• text = “สวัสดี ชาวโลก”
• pat = r“^[ก-๙]+$” # เป็นตัวไทยหมดไหม?
• re.match(pat, text) # True or False ?
Thai Normalization
• 1 คือ ย้าย วรรณยุกต์ ที่อยู่แทรก ไปไว้หน้า และรวม อำ ด้วย
• 2 คือ ย้าย วรรณยุกต์ ที่อยู่หลัง ไปไว้หน้า
Thai Sort
ลบ space ของการตัดคำ
ตัวอย่างอื่นๆ
• Soundex
• TCC (Thai Character Cluster)
• งาน clean data จำนวนมาก
• pattern สะกดคำผิด = spell correct
ขอบคุณ

More Related Content

What's hot

25 lat etyki biznesu w Polsce - raport_2019
25 lat etyki biznesu w Polsce - raport_201925 lat etyki biznesu w Polsce - raport_2019
25 lat etyki biznesu w Polsce - raport_2019
Fundacja Rozwoju Menedżerskiego - Manage or Die
 
Securité des applications web
Securité des applications webSecurité des applications web
Securité des applications web
Marcel TCHOULEGHEU
 
Jpa 잘 (하는 척) 하기
Jpa 잘 (하는 척) 하기Jpa 잘 (하는 척) 하기
Jpa 잘 (하는 척) 하기
경원 이
 
How to configure PyCharm for Odoo development in Windows?
How to configure PyCharm for Odoo development in Windows?How to configure PyCharm for Odoo development in Windows?
How to configure PyCharm for Odoo development in Windows?
Celine George
 
Tjenestekatalog som verktøy i dialog med forretning
Tjenestekatalog som verktøy i dialog med forretning Tjenestekatalog som verktøy i dialog med forretning
Tjenestekatalog som verktøy i dialog med forretning
Andreas Aasen
 
Synthèse de l'offshore et les si v1.0
Synthèse de l'offshore et les si v1.0Synthèse de l'offshore et les si v1.0
Synthèse de l'offshore et les si v1.0
Jean Luc Deixonne
 
Domain Name System
Domain Name SystemDomain Name System
Domain Name System
babaoui mohamed
 
Théorie de normalisation-base de données
Théorie de normalisation-base de donnéesThéorie de normalisation-base de données
Théorie de normalisation-base de données
Yassine Badri
 
Gemfire Introduction
Gemfire Introduction Gemfire Introduction
Gemfire Introduction
VMware Tanzu Korea
 
Cloud computing
Cloud computingCloud computing
Cloud computing
Abdelghani ACHIBANE
 
Translation d'adresse réseau (NAT)
Translation d'adresse réseau (NAT)Translation d'adresse réseau (NAT)
Translation d'adresse réseau (NAT)
Manassé Achim kpaya
 
COURS_PYTHON_22.ppt
COURS_PYTHON_22.pptCOURS_PYTHON_22.ppt
COURS_PYTHON_22.ppt
IbtissameAbbad1
 
Introduction JavaEE
Introduction JavaEEIntroduction JavaEE
Introduction JavaEE
Mourad HASSINI
 
Prise en main de Jhipster
Prise en main de JhipsterPrise en main de Jhipster
Prise en main de Jhipster
Kokou Gaglo
 
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton ClassesJava Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Abdul Rahman Sherzad
 
Cours design pattern m youssfi partie 8 stat, template method, command , medi...
Cours design pattern m youssfi partie 8 stat, template method, command , medi...Cours design pattern m youssfi partie 8 stat, template method, command , medi...
Cours design pattern m youssfi partie 8 stat, template method, command , medi...
ENSET, Université Hassan II Casablanca
 
POO
POOPOO
softCours design pattern m youssfi partie 9 creation des objets abstract fact...
softCours design pattern m youssfi partie 9 creation des objets abstract fact...softCours design pattern m youssfi partie 9 creation des objets abstract fact...
softCours design pattern m youssfi partie 9 creation des objets abstract fact...
ENSET, Université Hassan II Casablanca
 
Fondamentaux java
Fondamentaux javaFondamentaux java
Fondamentaux java
Ines Ouaz
 

What's hot (20)

Fonctionnement du web
Fonctionnement du webFonctionnement du web
Fonctionnement du web
 
25 lat etyki biznesu w Polsce - raport_2019
25 lat etyki biznesu w Polsce - raport_201925 lat etyki biznesu w Polsce - raport_2019
25 lat etyki biznesu w Polsce - raport_2019
 
Securité des applications web
Securité des applications webSecurité des applications web
Securité des applications web
 
Jpa 잘 (하는 척) 하기
Jpa 잘 (하는 척) 하기Jpa 잘 (하는 척) 하기
Jpa 잘 (하는 척) 하기
 
How to configure PyCharm for Odoo development in Windows?
How to configure PyCharm for Odoo development in Windows?How to configure PyCharm for Odoo development in Windows?
How to configure PyCharm for Odoo development in Windows?
 
Tjenestekatalog som verktøy i dialog med forretning
Tjenestekatalog som verktøy i dialog med forretning Tjenestekatalog som verktøy i dialog med forretning
Tjenestekatalog som verktøy i dialog med forretning
 
Synthèse de l'offshore et les si v1.0
Synthèse de l'offshore et les si v1.0Synthèse de l'offshore et les si v1.0
Synthèse de l'offshore et les si v1.0
 
Domain Name System
Domain Name SystemDomain Name System
Domain Name System
 
Théorie de normalisation-base de données
Théorie de normalisation-base de donnéesThéorie de normalisation-base de données
Théorie de normalisation-base de données
 
Gemfire Introduction
Gemfire Introduction Gemfire Introduction
Gemfire Introduction
 
Cloud computing
Cloud computingCloud computing
Cloud computing
 
Translation d'adresse réseau (NAT)
Translation d'adresse réseau (NAT)Translation d'adresse réseau (NAT)
Translation d'adresse réseau (NAT)
 
COURS_PYTHON_22.ppt
COURS_PYTHON_22.pptCOURS_PYTHON_22.ppt
COURS_PYTHON_22.ppt
 
Introduction JavaEE
Introduction JavaEEIntroduction JavaEE
Introduction JavaEE
 
Prise en main de Jhipster
Prise en main de JhipsterPrise en main de Jhipster
Prise en main de Jhipster
 
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton ClassesJava Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
 
Cours design pattern m youssfi partie 8 stat, template method, command , medi...
Cours design pattern m youssfi partie 8 stat, template method, command , medi...Cours design pattern m youssfi partie 8 stat, template method, command , medi...
Cours design pattern m youssfi partie 8 stat, template method, command , medi...
 
POO
POOPOO
POO
 
softCours design pattern m youssfi partie 9 creation des objets abstract fact...
softCours design pattern m youssfi partie 9 creation des objets abstract fact...softCours design pattern m youssfi partie 9 creation des objets abstract fact...
softCours design pattern m youssfi partie 9 creation des objets abstract fact...
 
Fondamentaux java
Fondamentaux javaFondamentaux java
Fondamentaux java
 

More from Korakot Chaovavanich

Vajirayana Digital Library Introduction
Vajirayana Digital Library IntroductionVajirayana Digital Library Introduction
Vajirayana Digital Library Introduction
Korakot Chaovavanich
 
Build your own ASR engine
Build your own ASR engineBuild your own ASR engine
Build your own ASR engine
Korakot Chaovavanich
 
How Pantip manage its Thai Database
How Pantip manage its Thai DatabaseHow Pantip manage its Thai Database
How Pantip manage its Thai Database
Korakot Chaovavanich
 
Line hackathon
Line hackathonLine hackathon
Line hackathon
Korakot Chaovavanich
 
Speech-to-Text API - Thai NLP Meetup #2
Speech-to-Text API - Thai NLP Meetup #2Speech-to-Text API - Thai NLP Meetup #2
Speech-to-Text API - Thai NLP Meetup #2
Korakot Chaovavanich
 
Thai NLP resources
Thai NLP resourcesThai NLP resources
Thai NLP resources
Korakot Chaovavanich
 

More from Korakot Chaovavanich (6)

Vajirayana Digital Library Introduction
Vajirayana Digital Library IntroductionVajirayana Digital Library Introduction
Vajirayana Digital Library Introduction
 
Build your own ASR engine
Build your own ASR engineBuild your own ASR engine
Build your own ASR engine
 
How Pantip manage its Thai Database
How Pantip manage its Thai DatabaseHow Pantip manage its Thai Database
How Pantip manage its Thai Database
 
Line hackathon
Line hackathonLine hackathon
Line hackathon
 
Speech-to-Text API - Thai NLP Meetup #2
Speech-to-Text API - Thai NLP Meetup #2Speech-to-Text API - Thai NLP Meetup #2
Speech-to-Text API - Thai NLP Meetup #2
 
Thai NLP resources
Thai NLP resourcesThai NLP resources
Thai NLP resources
 

Meetup 4 regexp

  • 1. Regular Expression
 กับ ภาษาไทย กรกฎ เชาวะวณิช Senior Data Scientist True Digital Group Thai NLP Meetup #4
  • 2. Regular Expression (regex) • เป็น pattern ไว้ประมวลผล text • match = ตรวจว่าป้อน email ถูกไหม • search, findall = ค้นเอกสาร หาข้อความแปลกๆ • replace = ค้น แล้วแทนที่ (sub) หรือลบทิ้ง • split = ค้น แล้วใช้เป็นตัวตัด ให้เป็นก้อนๆ
  • 3. องค์ประกอบหลัก • a, X, 9, ก ตัวมันเอง • . (จุด) อักษรทุกตัว เว้น n • w อักษรในคำ a-z, A-Z, 0-9 • s space,tnrf ตรงข้าม S • d digit = [0-9] บอกกลุ่ม • [^0-9] กลุ่มตรงข้าม • + 1 หรือมากกว่า • * 0 หรือมากกว่า • ? 0 หรือ 1 • +?, *? เอาสั้นๆ • {2} {2,} {2,5} {,5} ตัวอะไร กี่ตัว wwd{5}AZ10220
  • 4. ตัวพิเศษ • ^ เริ่มต้นบรรทัด • $ จบเนื้อหา b เริ่มหรือจบคำ • ใช้นำอักษรพิเศษ เช่น . จะจุดธรรมดา คือ slash • ( ) ใช้เก็บเนื้อหา เพื่อดึงออกไปใช้ หรือ ไปแทนที่ • | = หรือ เช่น cat|dog อาจต้องใช้วงเล็บช่วย
  • 5. Advanced • non-capture (?:cat|dog) • look ahead (?=xxx) (?!xxx) • look behind (?<=xxx) (?<!xxx) • named group (?P<ชื่อ>เนื้อหา) • flags (?aiLmsux) เช่น (?s) (?x) เหมือน re.S, re.X • ต้องเปิด manual ทุกที • ใน python มีตัวที่เก่งกว่า re ชื่อ regex
  • 6. Thai Unicode Table • ก = 0E01 • ๙ = 0E59 ๛ = 0E5B โคมูตร • [ก-๙] ตัวอักษรไทย • สระ [ะ-ไ] • สระหน้า [เ-ไ] • กลุ่มวรรณยุกต์ลอย [็่้๊๋์ํ]
  • 8. อักษรไทยล้วน • text = “สวัสดี ชาวโลก” • pat = r“^[ก-๙]+$” # เป็นตัวไทยหมดไหม? • re.match(pat, text) # True or False ?
  • 10. • 1 คือ ย้าย วรรณยุกต์ ที่อยู่แทรก ไปไว้หน้า และรวม อำ ด้วย • 2 คือ ย้าย วรรณยุกต์ ที่อยู่หลัง ไปไว้หน้า
  • 13. ตัวอย่างอื่นๆ • Soundex • TCC (Thai Character Cluster) • งาน clean data จำนวนมาก • pattern สะกดคำผิด = spell correct
  • 14.