python:使用正则表达式从日语推特文本中提取表情符号文本

python: extract the emoticon text from Japanese twitter text with regex

我使用以下正则表达式从日语推特中使用 python 提取表情符号文本。

// this is for extracting number, character, Japanese
text2 = r'[0-9A-Za-zぁ-んァ-ン一-龥]'  

non_text = r'[^0-9A-Za-zぁ-んァ-ン一-龥]'
// this is for extracting text that are allowed in Japanese emoticons
allow_text = r'[ovっつ゜ニノ三二]'
hw_kana = r'[ヲ-゚]'
open_branket = r'[\(∩ (]'
close_branket = r'[\)∩ )]'
arround_face = r'(?:' + non_text + '|' + allow_text + ')*'
face = r'(?!(?:' + text2 + '|' + hw_kana + '){3,}).{3,}'
face_string = arround_face + open_branket + face + close_branket +    
              arround_face
p_face = re.compile(face_string)

string1 = 'ふう。お腹いっぱい( ´•౪•`), 試験頑張るぞ\\\ ٩( ‘ω’ )و ////'
string2 = '心の相談は メール tiknathan@mail.goo.ne.jp までご連絡ください'
string3 = 'ドーピング系浪人生(n=1)'
string4 = '横浜は関内にある「 BAY らっきょ 」に初訪問してまいりました関東スープカレーブームの火付け役となったお店の「 人気NO.1 チキンカレー 」をいただきました(´∀`人)'
string5 = '鳥取県倉吉市   倉吉農業高校  3年食品科 (音楽部・茶道部)    AKB48大ファン高校生!まゆゆ、中野郁海ちゃん神推し    m0326w。♥。・゚♡゚・。♥。i0820n~現在♥大好きだよ♥       AKBファンの方はフォローお願いします^-^  \n\n来春から新社会人・・・の予定(´・ω・`)   '
string6 = 'うわ。。(-_-;)授業。運動会はなくなると?'
string7 = '毎月泊まっちゃえ♡親孝行*\(^o^)/*でも出来る時しとかないとだよ(o^^o)'

emoj1 = p_face.findall(string1)
emoj2 = p_face.findall(string2)
emoj3 = p_face.findall(string3)
emoj4 = p_face.findall(string4)
emoj5 = p_face.findall(string5)
emoj6 = p_face.findall(string6)
emoj7 = p_face.findall(string6)


print(emoj1)
print(emoj2)
print(emoj3)
print(emoj4)
print(emoj5)
print(emoj6)
print(emoj7)

但结果如下:

1.  ['( ´•౪•`), 試験頑張るぞ\\\ ٩( ‘ω’ )و']
2.  ['\u3000メール\u3000']
3.  ['(n=1)']
4.  ['「\u3000BAY\u3000'] 
5.  ['(´・ω・`)   ']
6.  ['。。(-_-;)']

但是有一些问题: string1,其实有两个表情:

    ( ´•౪•`) and \\\ ٩( ‘ω’ )و ////

但结果只显示一个表情符号,其中两个表情符号与其他日语文本一起显示。我只想在下面的列表中包含两个表情符号:

[ '( ´•౪•`)',' \\\ ٩( ‘ω’ )و ////']

其次,string5 其实♥。・゚♡゚・。♥。 和^-^ 也是表情符号,但这些表情无法通过回答的正则表达式提取。

此外,string2、string3、string 4中没有表情文字(〡ール 和(n=1)['「BAY']不是表情文字),但正则表达式提取了这些文字。能不能给个解决办法,谢谢! 请查看日文表情:http://kaomojiya.com/kao/?other/line

下面的正则表达式应该匹配你想要的

expr = '[^0-9A-Za-zぁ-んァ-ン一-龥ovっつ゜ニノ三二]*'       +  // [1]
           '[\(∩ (]'                                    +  // [2]
               '[^0-9A-Za-zぁ-んァ-ン一-龥ヲ-゚\)∩ )]*'    +  // [3]
           '[\)∩ )]'                                    +  // [4]
        '[^0-9A-Za-zぁ-んァ-ン一-龥ovっつ゜ニノ三二]*'         // [5]

你不能尝试here

它首先匹配潜在的特殊字符(除数字、罗马字、平假名、片假名和汉字外的任何字符,加上特殊假名[1] 和你一样。然后,它匹配你所说的 open_branket [2],然后是任何非汉字,非数字等 和非 close_branket [3].最后,它匹配表情符号的结尾,就像您使用 [4][5]

一样

编辑

string4 = ...
string5 = ...

string4 的问题是字符 BAYBAY 不同。第二个是通常的 ASCII 字符 0x420x410x59,而第一个是 unicode characters between 0xff21 and 0xff3a. You can just add them to the list of rejected characters ([3]). You might also want to add their lower case version (0xff41) 到 (0xff5a) 以及相应的数字,从 0xff100xff19 您可能会对阅读 this page about fullwidth and halfwidth.

感兴趣

string5 的问题是这些表情符号不包含您定义的任何 open/close 字符。对于第一个表情符号,如果这是 acceptable,可以通过将 添加到起始字符列表来解决。但是,它并没有解决 ^-^.

的问题

我建议改变策略。 looks to work not too bad 是选择一组出现在普通文本中的常见字符(我们称之为 C)和可能出现在表情符号中的 C 的子集(我们称之为 S) 和一个数字 x。然后你可以构建以下正则表达式:

(?:C*)(?P<match>(?:[^C]|S){x,})(?:C*)

此表达式将匹配非捕获组中的 "regular" 文本,后跟至少 x "non regular" 个字符或子集 S 中的字符的捕获序列在名为 match 的组中捕获,后跟任何未捕获的 "regular" 文本。

调查unicodetable,我定义C如下集合

\u4e00-\u9fff      => CJK Unified Ideographs
\u3400-\u4dbf      => CJK Unified Ideographs Extension A
\uf900-\ufaff      => CJK Compatibility Ideographs
\u3040-\u309f      => Hiragana
\u30a0-\u30ff      => Katakana
\u3000-\u303f      => "CJK Symbol and punctuation"
\uff21-\uff3a      => fullwidth A to Z
\uff41-\uff5a      => fullwidth a to z
\uff10-\uff19      => Fullwidth 0 to 9
\uff00-\uff0e      => Fullwidth form of some punctuation characters
A-Z                => ASCII A to Z
a-z                => ASCII a to z
0-9                => ASCII numbers
@.,;!\?  ~♥\     => other punctuation characters

S作为[人・;皿。゜°うぅ]x3但是你需要更多地检查日本表情符号集来完善它。

了解更多信息

这导致以下正则表达式

(?:[\u4E00-\u9FFF\u3400-\u4DBF\uF900-\ufaff\u3040-\u309f\u30a0-\u30ff\u3000-\u303f\uff21-\uff3a\uff41-\uff5a\uff10-\uff19\uff00-\uff0eA-Za-z0-9@.,;!\?  ~♥\]*)(?P<match>(?:[^\u4E00-\u9FFF\u3400-\u380f\uF900-\ufaff\u3040-\u309f\u30a0-\u30ff\u3000-\u303f\uff21-\uff3a\uff41-\uff5a\uff10-\uff19A-Za-z0-9\r\n]|[人・;皿。゜°うぅ]){3,})(?:[\u4E00-\u9FFF\u3400-\u4DBF\uF900-\ufaff\u3040-\u309f\u30a0-\u30ff\u3000-\u303f\uff21-\uff3a\uff41-\uff5a\uff10-\uff19A-Za-z0-9@.,;  ~♥\]*)

总而言之,我认为不可能将每个日语表情符号都与一个正则表达式相匹配,因为它们不遵循任何明确定义的模式。此外,它们看起来包含并有时以常规文本结尾。例如 (。´-д-)疲れた。。 取自您的 link。另一个解决方案,如表情符号数据库,可能值得研究