Spaces:
Sleeping
Sleeping
| import random | |
| import re | |
| import gradio as gr | |
| import emoji | |
| # ------------------------- | |
| # Config | |
| # ------------------------- | |
| ELIGIBLE = ("NOUN", "ADJ") | |
| WORD2EMOJI = { | |
| "heart":"โค๏ธ","love":"โค๏ธ","car":"๐","bike":"๐ฒ","bus":"๐","train":"๐","plane":"โ๏ธ", | |
| "pizza":"๐","burger":"๐","coffee":"โ","tea":"๐ต","book":"๐","laptop":"๐ป", | |
| "phone":"๐ฑ","camera":"๐ท","money":"๐ฐ","rain":"๐ง๏ธ","sun":"โ๏ธ","moon":"๐","star":"โญ", | |
| "fire":"๐ฅ","party":"๐","gift":"๐","music":"๐ต","dog":"๐ถ","cat":"๐ฑ","bird":"๐ฆ","flower":"๐ธ", | |
| "tree":"๐ณ","house":"๐ ","school":"๐ซ","office":"๐ข","hospital":"๐ฅ","food":"๐ฝ๏ธ", | |
| "cake":"๐ฐ","cookie":"๐ช","chocolate":"๐ซ","icecream":"๐จ","beach":"๐๏ธ","mountain":"โฐ๏ธ","city":"๐๏ธ", | |
| "rocket":"๐","earth":"๐","world":"๐","mail":"โ๏ธ","football":"๐","soccer":"โฝ","basketball":"๐", | |
| "tennis":"๐พ","medal":"๐ ","trophy":"๐","alarm":"โฐ","idea":"๐ก","check":"โ ","cross":"โ", | |
| "warning":"โ ๏ธ", | |
| # adjectives | |
| "happy":"๐","sad":"๐ข","angry":"๐ก","tired":"๐ด","sleepy":"๐ด","cool":"๐", | |
| "lovely":"๐ฅฐ","beautiful":"๐","pretty":"๐","handsome":"๐คต","smart":"๐ง ","brilliant":"๐ง ", | |
| "fast":"โก","quick":"โก","slow":"๐ข","hot":"๐ฅ","cold":"โ๏ธ","sweet":"๐ฌ","salty":"๐ง", | |
| "spicy":"๐ถ๏ธ","fresh":"๐ซง","strong":"๐ช","weak":"๐ซค","shiny":"โจ","clean":"๐ซง","dirty":"๐งน", | |
| "busy":"๐","rich":"๐ฐ","poor":"๐ช","lucky":"๐","fun":"๐","romantic":"๐","scary":"๐ฑ" | |
| } | |
| ADJECTIVE_WORDS = { | |
| "happy","sad","angry","tired","sleepy","cool","lovely","beautiful","pretty", | |
| "handsome","smart","brilliant","fast","quick","slow","hot","cold","sweet","salty","spicy", | |
| "fresh","strong","weak","shiny","clean","dirty","busy","rich","poor","lucky","fun","romantic","scary" | |
| } | |
| _ADJ_SUFFIXES = ("y","ive","ful","less","ous","al","ic","able","ible","ish") | |
| _NOUN_SUFFIXES = ("tion","ment","ness","ity","er","or","ship","age","ance","ence","ism","ist") | |
| # ------------------------- | |
| # Helpers | |
| # ------------------------- | |
| def try_emojize_alias(word): | |
| lemma = word.lower() | |
| for cand in (f":{lemma}:", f":{lemma}_face:", f":{lemma}_emoji:", f":{lemma}_button:"): | |
| try: | |
| em = emoji.emojize(cand, language="alias") | |
| except TypeError: | |
| em = emoji.emojize(cand) | |
| if em != cand: | |
| return em | |
| return None | |
| def tokenize(text): | |
| return re.findall(r"\w+|[^\w\s]", text, flags=re.UNICODE) | |
| def simple_tag(tokens, allow_nouns=True, allow_adjs=True): | |
| tagged = [] | |
| for t in tokens: | |
| if not re.search(r"[A-Za-z]", t): | |
| tagged.append((t, "OTHER")) | |
| continue | |
| low = t.lower() | |
| if low in WORD2EMOJI: | |
| tag = "ADJ" if low in ADJECTIVE_WORDS else "NOUN" | |
| if (tag == "NOUN" and not allow_nouns) or (tag == "ADJ" and not allow_adjs): | |
| tag = "OTHER" | |
| tagged.append((t, tag)) | |
| continue | |
| tag = "OTHER" | |
| if low.endswith(_ADJ_SUFFIXES): | |
| tag = "ADJ" | |
| elif low.endswith(_NOUN_SUFFIXES) or t[:1].isupper(): | |
| tag = "NOUN" | |
| if (tag == "NOUN" and not allow_nouns) or (tag == "ADJ" and not allow_adjs): | |
| tag = "OTHER" | |
| tagged.append((t, tag)) | |
| return tagged | |
| _EMOJI_RE = re.compile("[" "\U0001F300-\U0001FAFF" "\u2600-\u26FF" "\u2700-\u27BF" "]", flags=re.UNICODE) | |
| def emoji_count(s): return len(_EMOJI_RE.findall(s)) | |
| # ------------------------- | |
| # Core translate | |
| # ------------------------- | |
| def translate_text(text, mode="Replace", replacement_rate=1.0, keep_case=True, dedupe=True, | |
| allow_nouns=True, allow_adjs=True): | |
| text = (text or "").strip() | |
| if not text: | |
| return "", "๐งฎ Emojis: 0", "๐ Replaced words: 0" | |
| tokens = tokenize(text) | |
| tagged = simple_tag(tokens, allow_nouns, allow_adjs) | |
| out, replaced = [], 0 | |
| for tok, tag in tagged: | |
| eligible = (tag in ELIGIBLE) and re.search(r"[A-Za-z]", tok) | |
| if eligible and random.random() <= replacement_rate: | |
| em = WORD2EMOJI.get(tok.lower()) or try_emojize_alias(tok.lower()) | |
| if em: | |
| replaced += 1 | |
| out.append(em if mode == "Replace" else f"{tok} {em}") | |
| continue | |
| out.append(tok) | |
| # spacing: no space before , . ! ? ; : ) ] } | |
| result = "" | |
| for i, t in enumerate(out): | |
| result += t | |
| if i < len(out) - 1: | |
| nxt = out[i + 1] | |
| if not re.match(r"^[,\.!\?;:\)\]\}]+$", nxt): | |
| result += " " | |
| if not keep_case: | |
| result = re.sub(r"[A-Za-z]+", lambda m: m.group(0).lower(), result) | |
| return result, f"๐งฎ Emojis: {emoji_count(result)}", f"๐ Replaced words: {replaced}" | |
| # ------------------------- | |
| # UI | |
| # ------------------------- | |
| EXAMPLES = [ | |
| "I love spicy pizza and hot coffee on a rainy day.", | |
| "The smart dog chased a happy cat around the house.", | |
| "We had a beautiful view of the city from the mountain.", | |
| "She wrote a brilliant idea in her notebook.", | |
| "This is a strong team with a cool product.", | |
| ] | |
| with gr.Blocks(title="Emoji Translator ๐โก๏ธ๐๐๐") as demo: | |
| gr.Markdown("## Emoji Translator ๐โก๏ธ๐๐๐\nType a sentence โ Iโll replace **nouns/adjectives** with matching emojis.") | |
| with gr.Row(): | |
| with gr.Column(scale=5): | |
| inp = gr.Textbox(label="Your sentence", lines=4, placeholder="Type hereโฆ") | |
| mode = gr.Radio(choices=["Replace", "Append"], value="Replace", label="Mode") | |
| rate = gr.Slider(0, 1, 1, 0.05, label="Replacement rate") | |
| with gr.Row(): | |
| keep_case = gr.Checkbox(True, "Preserve capitalization") | |
| dedupe = gr.Checkbox(True, "Avoid duplicate emojis") | |
| with gr.Row(): | |
| allow_nouns = gr.Checkbox(True, "Target nouns") | |
| allow_adjs = gr.Checkbox(True, "Target adjectives") | |
| btn = gr.Button("Translate โจ") | |
| clear_btn = gr.Button("Clear") | |
| gr.Examples(EXAMPLES, inputs=inp) | |
| with gr.Column(scale=6): | |
| out = gr.Textbox(label="Emojiโfied text", lines=6, show_copy_button=True, interactive=False) | |
| emoji_stat = gr.Markdown("๐งฎ Emojis: 0") | |
| repl_stat = gr.Markdown("๐ Replaced words: 0") | |
| def run_translate(text, mode, rate, keep_case, dedupe, allow_nouns, allow_adjs): | |
| return translate_text(text, mode, rate, keep_case, dedupe, allow_nouns, allow_adjs) | |
| btn.click(run_translate, [inp, mode, rate, keep_case, dedupe, allow_nouns, allow_adjs], [out, emoji_stat, repl_stat]) | |
| def clear_all(): | |
| return "", "๐งฎ Emojis: 0", "๐ Replaced words: 0", "" | |
| clear_btn.click(clear_all, None, [out, emoji_stat, repl_stat, inp]) | |
| if __name__ == "__main__": | |
| demo.launch() |