How to find and count emoticons in a string using python?

前端 未结 3 1608
难免孤独
难免孤独 2020-12-02 00:12

This topic has been addressed for text based emoticons at link1, link2, link3. However, I would like to do something slightly different than matching simple emoticons. I\'m

3条回答
  •  情歌与酒
    2020-12-02 00:49

    If you are trying to read unicode characters outside the ascii range, don't convert into the ascii range. Just leave it as unicode and work from there (untested):

    import sys
    
    count = 0
    emoticons = set(range(int('1f600',16), int('1f650', 16)))
    for row in sys.stdin:
        for char in row:
            if ord(char) in emoticons:
                count += 1
    print "%d emoticons found" % count
    

    Not the best solution, but it should work.

提交回复
热议问题