2017-07-05 7 views
1

ユニコードでは、文字はEmoji propertyです。Pythonの文字のEmojiプロパティをチェックするには?

文字が絵文字であるかどうかを判断するためにPythonでの標準的な方法はありますか?

私はunicodedataを知っているが、すべてのこれらの余分な文字の詳細を公開するためには表示されません。

注:リンクに提供されるように、私は、unicdoe標準の「絵文字」と呼ばれる特定の属性について尋ねています。私はパターン範囲の任意のリストを持ちたいとは思わないし、標準的なライブラリを使用するのが望ましい。

+1

[emojisをPythonの文字列から削除する]可能な複製(https://stackoverflow.com/questions/33404752/removing-emojis-from-a-string-in-python) – kabanus

+0

@kabanus重複していません。他の質問は、文字の無作為なリストを絵文字として指定します。私は、特にUnicode標準によって絵文字としてマークされているものについて尋ねています。 –

答えて

0

これは私が絵文字情報をロードするために作成することになったコードです。 get_emoji関数はデータファイルを取得し、解析し、列挙コールバックを呼び出します。残りのコードは、これを使用して必要な情報のJSONファイルを生成します。

#!/usr/bin/env python3 
# Generates a list of emoji characters and names in JS format 
import urllib.request 
import unicodedata 
import re, json 

''' 
Enumerates the Emoji characters that match an attributes from the Unicode standard (the Emoji list). 

@param on_emoji A callback that is called with each found character. Signature `on_emoji(code_point_value)` 
@param attribute The attribute that is desired, such as `Emoji` or `Emoji_Presentation` 
''' 
def get_emoji(on_emoji, attribute): 
    with urllib.request.urlopen('http://www.unicode.org/Public/emoji/5.0/emoji-data.txt') as f: 
     content = f.read().decode(f.headers.get_content_charset()) 

     cldr = re.compile('^([0-9A-F]+)(..([0-9A-F]+))?([^;]*);([^#]*)#(.*)$') 
     for line in content.splitlines(): 
      m = cldr.match(line) 
      if m == None: 
       continue 

      line_attribute = m.group(5).strip() 
      if line_attribute != attribute: 
       continue 

      code_point = int(m.group(1),16) 
      if m.group(3) == None: 
       on_emoji(code_point) 
      else: 
       to_code_point = int(m.group(3),16) 
       for i in range(code_point,to_code_point+1): 
        on_emoji(i) 


# Dumps the values into a JSON format 
def print_emoji(value): 
    c = chr(value) 
    try: 
     obj = { 
      'code': value, 
      'name': unicodedata.name(c).lower(), 
     } 
     print(json.dumps(obj),',') 
    except: 
     # Unicode DB is likely outdated in installed Python 
     pass 

print("module.exports = [") 
get_emoji(print_emoji, "Emoji_Presentation") 
print("]") 

それが私の元の問題を解決しました。質問そのものに答えるためには、結果を辞書に貼り付けて検索するだけです。

-1

私は

import re 

emoji_pattern = re.compile("[" 
           u"\U0001F600-\U0001F64F" # emoticons 
           u"\U0001F300-\U0001F5FF" # symbols & pictographs 
           u"\U0001F680-\U0001F6FF" # transport & map symbols 
           u"\U0001F1E0-\U0001F1FF" # flags (iOS) 
           "]+", flags=re.UNICODE) 

はまた、この質問をチェックアウトする前に成功し、次の正規表現パターンを使用している:私はにリンクされ、質問後removing emojis from a string in Python

+0

これらの範囲は、標準のUnicodeの絵文字データのリストに含まれていない範囲です。 –

+0

@ edA-qamort-ora-yでも同じことをやっていますが、範囲全体を含めるように拡張するだけです。 –

+0

@ edA-qamort-ora-y https://pypi.python.org/pypi/emojiをチェックしてください –

0

を、あなたはそこに何をチェックするには、プロパティに建てられたがありませんが、に比較されますemojiPattern.matchを使用して

import urllib.request as ur 
import re 

html = str(ur.urlopen('http://www.unicode.org/Public/emoji/5.0/emoji-data.txt').read()) 
codes=list(map(lambda x: '-'.join(['\\U'+a.zfill(8) for a in x.split('..')]).encode().decode('unicode-escape'),re.findall(r'(?<=\\n)[\w.]+',html))) 
emojiPattern = re.compile('['+','.join(codes)+']',flags=re.UNICODE) 

:あなたが提供するページを使用して、独自のパターンを作成することができますそのページに含まれるUnicodeコードそれが更新/別のバージョンがアップロードされている場合はそれを変更してください。