Made it sort of a module, for easier inclusion in other scripts

This commit is contained in:
Eirik Øverby 2025-08-23 23:56:11 +02:00
parent a8ddc68a1d
commit 483da44672
3 changed files with 26 additions and 2 deletions

View file

@ -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 either classic smileys (`:-)`) or textual variants (`:joy:`) on standard
output. output.
Alternatively, `import` it in your script and call `replace_emoji(<string>)`.
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 Needs
``` ```
pip3.12 install emoji-data-python pip3.12 install emoji-data-python

0
__init__.py Normal file
View file

View file

@ -1,9 +1,13 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import sys import sys
import emoji_data_python as ed import emoji_data_python as ed
emoji_cp = {emoji.unified: emoji for emoji in ed.emoji_data} 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))): for e in list(set(ed.get_emoji_regex().findall(line))):
hexcp = format(ord(e),'x').upper() hexcp = format(ord(e),'x').upper()
em = emoji_cp[hexcp] em = emoji_cp[hexcp]
@ -15,4 +19,10 @@ for line in sys.stdin:
line=line.replace(e, ":" + em.short_name + ":") line=line.replace(e, ":" + em.short_name + ":")
else: else:
line=line.replace(e, "?") 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))