From 483da44672613d011dac1025899e344830a841bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eirik=20=C3=98verby?= Date: Sat, 23 Aug 2025 23:56:11 +0200 Subject: [PATCH] Made it sort of a module, for easier inclusion in other scripts --- README.md | 14 ++++++++++++++ __init__.py | 0 conv.py => smiley.py | 14 ++++++++++++-- 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 __init__.py rename conv.py => smiley.py (75%) diff --git a/README.md b/README.md index f887fcc..f672ff1 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,20 @@ Takes text with unicode emojis on standard input, spits out emojis replaced by either classic smileys (`:-)`) or textual variants (`:joy:`) on standard output. +Alternatively, `import` it in your script and call `replace_emoji()`. + +For example, start by adding this repo to your project (you're using git, I +presume): +```sh +git submodule add https://git.anduin.net/ltning/emoji_to_smiley.git smiley +``` +then, in your code +```python +from smiley import smiley +for line in sys.stdin: + print(smiley.replace_emoji(line)) +``` + Needs ``` pip3.12 install emoji-data-python diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/conv.py b/smiley.py similarity index 75% rename from conv.py rename to smiley.py index bb080de..5054e49 100644 --- a/conv.py +++ b/smiley.py @@ -1,9 +1,13 @@ #!/usr/bin/env python3 import sys import emoji_data_python as ed + emoji_cp = {emoji.unified: emoji for emoji in ed.emoji_data} -for line in sys.stdin: +def main(): + pass + +def replace_emoji(line): for e in list(set(ed.get_emoji_regex().findall(line))): hexcp = format(ord(e),'x').upper() em = emoji_cp[hexcp] @@ -15,4 +19,10 @@ for line in sys.stdin: line=line.replace(e, ":" + em.short_name + ":") else: line=line.replace(e, "?") - print(line,end='') + #print(line,end='') + return line.strip() + +if __name__ == "__main__": + for line in sys.stdin: + print(replace_emoji(line)) +