Skip to content

Update emojize_text.py #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions python/examples/basic/workdir/emojize_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
Usage:
emojize_text.py SOURCE_FILE [OUTPUT_FILE]
"""
import sys;
from emoji import unicode_codes;
import sys
from emoji import emojize

# Open source file
source_file_path = sys.argv[1]
Expand All @@ -21,14 +21,25 @@
out_file = open(sys.argv[2], 'w') if len(sys.argv) > 2 else sys.stdout

# Transform text
emoji_dict = unicode_codes.get_emoji_unicode_dict('en')
output = ''
for line in source_file.readlines():
output_line = []
for word in line.split():
output_line.append(emoji_dict.get(f':{word}:', word))
out_file.write(" ".join(output_line))
# Try to convert the word to an emoji using the format :word:
emoji_word = emojize(f':{word}:', language='en')
# If the word wasn't converted (still has colons), use the original word
if emoji_word == f':{word}:':
output_line.append(word)
else:
output_line.append(emoji_word)
line_text = " ".join(output_line)
out_file.write(line_text)
out_file.write('\n')
# Accumulate output for the final print
output += line_text + '\n'

# Print output
print(output)

# Print output
print(output)