Regular Expression to accept all Thai characters and English letters in python

后端 未结 3 1164
野的像风
野的像风 2020-12-30 12:23

I need to vectorize text documents in Thai (e.g Bag of Words, doc2vec).

First I want to go over each document, omitting everything except the Thai characters and En

3条回答
  •  我在风中等你
    2020-12-30 12:58

    Sadly, there are not many regular expression libraries with good Unicode support, and Python's re library is one of them. Oniguruma has proper Unicode support and I believe it has Python bindings, and Perl's built-in regular expressions have good Unicode support.

    I normally don't suggest that people switch languages, but in this case, you will save a lot of trouble by using Perl (and for the record, I have the gold Python badge, and haven't touched Perl in the past decade!). Here is a taste of how simple it is (it should be the same in Oniguruma, which again, I think has Python bindings):

    [^\p{Latin}\p{Thai}]+
    

    Here is Perl example code:

    #!/usr/bin/perl -w
    use utf8;
    $_ = "ทรูวิชั่นส์ ประ...abc123";
    s/[^\p{Latin}\p{Thai}]+/ /g;
    print;
    print "\n";
    

    Here is the output:

    ทรูวิชั่นส์ ประกาศถ่ายทอดสดศึกฟุตบอล พรีเมียร์ ลีก อังกฤษ ครบทุกนัดเป็นเวลา ปี ตั้งแต่ฤดูกาล พร้อมด้วยอีก ลีกดัง อาทิ ลา ลีกา สเปน กัลโช เซเรีย เอ อิตาลี และลีกเอิง ฝรั่งเศส ภายใต้แพ็กเกจสุดคุ้ม ทั้งผ่านมือถือ และโทรทัศน์ some English words here abc

提交回复
热议问题