def get_Emoji_sentiment(self, lemma): lexeme_sentiment = {} unicode_emoji = Emoji.ascii_to_unicode(lemma.upper()) if len(unicode_emoji) == 1: category = unicodedata.category(unicode_emoji) if category == 'So': # is "Symbol other" (a specific unicode category) shortcode = Emoji.unicode_to_shortcode(unicode_emoji) if shortcode != unicode_emoji: # is an emoji only if it has an emoji shortcode # check whether the emoji is in the emoji polarity lexicon if unicode_emoji in self.emoji: # is in the lexicon emodict = self.emoji[unicode_emoji] lexeme_sentiment["0"] = { "shortcode":shortcode, "negativity":emodict["negativity"], "positivity":emodict["positivity"] } else: # tokenize the shortcode and get its tokens polarities from SentiWordNet tokens = shortcode.strip(' :').split('_') # shortcode tokenization seems an easy problem to solve negativity = 0 positivity = 0 count = 0 # count the number of shortcode tokens with a synset for token in tokens: synsets = list(swn.senti_synsets(token)) if len(synsets)>0: senti_synset = synsets[0] negativity += senti_synset.neg_score() positivity += senti_synset.pos_score() count += 1 if count > 1: # take the average of all shortcode tokens polarities negativity /= count positivity /= count lexeme_sentiment["0"] = { "shortcode":shortcode, "negativity":negativity, "positivity":positivity } # print(lexeme_sentiment["0"]) return lexeme_sentiment
def test_shortcode_to_image(self): txt = 'Hello world! 😄 :smile:' expected = """Hello world! 😄 <img class="emojione" alt="😄" src="https://cdn.jsdelivr.net/emojione/assets/3.0/png/64/1f604.png"/>""" self.assertEqual(Emoji.shortcode_to_image(txt), expected) Emoji.unicode_alt = False expected = """Hello world! 😄 <img class="emojione" alt=":smile:" src="https://cdn.jsdelivr.net/emojione/assets/3.0/png/64/1f604.png"/>""" self.assertEqual(Emoji.shortcode_to_image(txt), expected) Emoji.unicode_alt = True
def test_shortcode_to_image(self): txt = 'Hello world! 😄 :smile:' expected = """Hello world! 😄 <img class="emojione" alt="😄" src="https://cdn.jsdelivr.net/emojione/assets/3.1/png/64/1f604.png"/>""" self.assertEqual(Emoji.shortcode_to_image(txt), expected) Emoji.unicode_alt = False expected = """Hello world! 😄 <img class="emojione" alt=":smile:" src="https://cdn.jsdelivr.net/emojione/assets/3.1/png/64/1f604.png"/>""" self.assertEqual(Emoji.shortcode_to_image(txt), expected) Emoji.unicode_alt = True
def test_emoji_at_end_of_sentence_with_alternate_punctuation(self): unicode = "Emoji One's official mascot is 🐌!" shortcode = "Emoji One's official mascot is :snail:!" image = ( 'Emoji One\'s official mascot is <img class="emojione" alt="🐌" src="https://cdn.jsdelivr.net/emojione/assets/png/1f40c.png' + self.cache_bust_param + '"/>!' ) self.assertEqual(Emoji.unicode_to_image(unicode), image) self.assertEqual(Emoji.shortcode_to_image(shortcode), image)
def test_shortcode_to_image(self): txt = 'Hello world! 😄 :smile:' expected = """Hello world! 😄 <img class="emojione" alt="😄" src="//cdn.jsdelivr.net/emojione/assets/png/1F604.png%s"/>""" %\ Emoji.cache_bust_param self.assertEqual(Emoji.shortcode_to_image(txt), expected) Emoji.unicode_alt = False expected = """Hello world! 😄 <img class="emojione" alt=":smile:" src="//cdn.jsdelivr.net/emojione/assets/png/1F604.png%s"/>""" %\ Emoji.cache_bust_param self.assertEqual(Emoji.shortcode_to_image(txt), expected) Emoji.unicode_alt = True
def test_emoji_at_end_of_sentence_with_preceeding_colon(self): unicode = "Emoji One's official mascot: 🐌" shortcode = "Emoji One's official mascot: :snail:" image = ( 'Emoji One\'s official mascot: <img class="emojione" alt="🐌" src="https://cdn.jsdelivr.net/emojione/assets/png/1f40c.png' + self.cache_bust_param + '"/>' ) self.assertEqual(Emoji.unicode_to_image(unicode), image) self.assertEqual(Emoji.shortcode_to_image(shortcode), image)
def test_emoji_inside_sentence_with_comma(self): unicode = "The 🐌, is Emoji One's official mascot." shortcode = "The :snail:, is Emoji One's official mascot." image = ( 'The <img class="emojione" alt="🐌" src="https://cdn.jsdelivr.net/emojione/assets/png/1f40c.png' + self.cache_bust_param + "\"/>, is Emoji One's official mascot." ) self.assertEqual(Emoji.unicode_to_image(unicode), image) self.assertEqual(Emoji.shortcode_to_image(shortcode), image)
def test_emoji_at_start_of_sentence_with_apostrophe(self): unicode = "🐌's are cool!" shortcode = ":snail:'s are cool!" image = ( '<img class="emojione" alt="🐌" src="https://cdn.jsdelivr.net/emojione/assets/png/1f40c.png' + self.cache_bust_param + "\"/>'s are cool!" ) self.assertEqual(Emoji.unicode_to_image(unicode), image) self.assertEqual(Emoji.shortcode_to_image(shortcode), image)
def test_emoji_at_start_of_sentence(self): unicode = "🐌 mail." shortcode = ":snail: mail." image = ( '<img class="emojione" alt="🐌" src="https://cdn.jsdelivr.net/emojione/assets/png/1f40c.png' + self.cache_bust_param + '"/> mail.' ) self.assertEqual(Emoji.unicode_to_image(unicode), image) self.assertEqual(Emoji.shortcode_to_image(shortcode), image)
def test_single_unicode_char(self): unicode = "🐌" shortcode = ":snail:" image = ( '<img class="emojione" alt="🐌" src="https://cdn.jsdelivr.net/emojione/assets/png/1f40c.png' + self.cache_bust_param + '"/>' ) self.assertEqual(Emoji.unicode_to_image(unicode), image) self.assertEqual(Emoji.shortcode_to_image(shortcode), image)
def replace_with_emoji(effect, size): """ Reportlab's Paragraph doesn't accept normal html <image> tag's attributes like 'class', 'alt'. Its a little hack to remove those attrbs """ e = "" for i, c in enumerate(effect): if c in emoji.UNICODE_EMOJI: e += Emoji.to_image(c) else: e += c # e = effect # for item in sorted(emoji.UNICODE_EMOJI.keys(), key = len, reverse = True): # if item in effect: # print(item) # if Emoji.to_image(item) != item: # e = re.sub(item, Emoji.to_image(item), e) # print(item) # Emoji.to_image(c) # e = re.sub(re.escape(item), re.escape(emoji.UNICODE_EMOJI[item]), e) e = e.replace('class="emojione " style="" ', 'height=%s width=%s' % (size, size)) return re.sub('alt="'+Emoji.shortcode_regexp+'"', '', e)
def view_poll(poll_id): fragment = Fragment.objects.get_or_404(id=poll_id) if fragment.story.bulletin not in g.current_account.bulletins: abort(401) poll_questions = PollQuestions.objects(fragment=fragment) total = float(sum([len(i.users) for i in poll_questions])) def get_persent(n, total): if total > 0: return n / total * 100 return 0 data = [{ 'answer': q.text, 'votes': len(q.users), 'persent': get_persent(len(q.users), total) } for q in poll_questions] # add total stats data.append({'answer': 'All answers', 'votes': int(total), 'persent': 100}) fragment.text = Emoji.shortcode_to_unicode(fragment.text) return render_template('polls/questions.html', fragment=fragment, data=data)
def send_text_fragment(self, chat_id, f, show_keyboard=None): self.typing(chat_id, f.text) self.bot.sendMessage( chat_id, Emoji.shortcode_to_unicode(f.text), buttons=show_keyboard )
def send_poll_fragment(self, chat_id, f): self.typing(chat_id, f.text) self.bot.sendMessagePostback( chat_id, Emoji.shortcode_to_unicode(f.text), buttons=self.get_poll_keyboard(f) )
def send_text_fragment(self, chat_id, f, show_keyboard=None): self.typing(chat_id, f.text) self.bot.sendMessage( chat_id, Emoji.shortcode_to_unicode(f.text), disable_web_page_preview=True, reply_markup=show_keyboard )
def send_poll_fragment(self, chat_id, f): self.typing(chat_id, f.text) self.bot.sendMessage( chat_id, Emoji.shortcode_to_unicode(f.text), disable_web_page_preview=True, reply_markup=self.get_poll_keyboard(f) )
def get_answers_keyboard(self, answers): show_keyboard = { 'keyboard': [ [Emoji.shortcode_to_unicode(f.text)] for f in answers ], 'one_time_keyboard': True, 'resize_keyboard': True } return show_keyboard
def action_from_answer(self, user, msg, story_model=Story): answers = self.get_prev_answers(user, story_model=story_model) if answers: text = Emoji.unicode_to_shortcode(self.read_user_message(msg)) for f in answers: if f.text == text: return f.action return None
def replace_with_emoji_pdf(text, size): """ Reportlab's Paragraph doesn't accept normal html <image> tag's attributes like 'class', 'alt'. Its a little hack to remove those attrbs """ text = Emoji.to_image(text) text = text.replace('class="emojione"', 'height=%s width=%s' %(size, size)) return re.sub('alt="'+Emoji.shortcode_regexp+'"', '', text)
def to_image(text, **kwargs): """ Parse short code and unicode code to emotion :param text: :param kwargs[css]: css class :param kwargs[style]: icon stylesheet :return: string """ return Emoji.to_image(text, **kwargs)
def test_shortcode_to_ascii(self): txt = 'Hello world! 😄 :smile:' expected = [ 'Hello world! 😄 :]', 'Hello world! 😄 :-)', 'Hello world! 😄 =)', 'Hello world! 😄 :)', 'Hello world! 😄 =]' ] output = Emoji.shortcode_to_ascii(txt) self.assertIn(output, expected)
def send_fragment(chat_id, fragment, reply_markup): print fragment.type, fragment.text, fragment.url if fragment.type == fragment.TYPE_PARAGRAPH: time.sleep(TYPING_TIME) bot.sendMessage(chat_id, Emoji.shortcode_to_unicode(fragment.text), reply_markup=reply_markup, disable_web_page_preview=True) else: bot.sendPhoto(chat_id, ('image.jpg', urlopen(fragment.url)), reply_markup=reply_markup)
def emoji2cfd(emoji, emoji_width = 1): ''' Converts the inputted emoji to numpy array and feeds it into image2cdf. The emoji_width parameter helps scale the emoji properly for the cfd solver ''' emoji_img_item = Emoji.to_image(emoji) start = a.find("src=") link = a[start+5: -3] im = imageio.imread(link)
def emoji_replace(text, autoescape=True): """ Replaces Unicode and Shortcode emoji's with embedded images, using the Emojipy library Args: text: A string to be matched for shortcode and unicode emojis autoescape: Optional argument for autoescaping of input string before processing Returns: Safe text """ # Escape text if it is not safe autoescape = autoescape and not isinstance(text, SafeData) if autoescape: text = escape(text) text = Emoji.unicode_to_image(text) text = Emoji.shortcode_to_image(text) return mark_safe(text)
def test_shortcode_to_ascii(self): txt = 'Hello world! 😄 :slight_smile:' expected = [ 'Hello world! 😄 :]', 'Hello world! 😄 :-)', 'Hello world! 😄 =)', 'Hello world! 😄 :)', 'Hello world! 😄 =]' ] output = Emoji.shortcode_to_ascii(txt) self.assertIn(output, expected)
def send_lead_answers(self, chat_id, lead, fragments): show_keyboard = { 'keyboard': [ [Emoji.shortcode_to_unicode(f.text)] for f in fragments ], 'one_time_keyboard': True, 'resize_keyboard': True } self.typing(chat_id, lead) self.bot.sendMessage(chat_id, lead, reply_markup=show_keyboard, disable_web_page_preview=True)
def action_from_answer(self, user, msg): s = Story.objects.filter(bulletin=user.current_bulletin, order=user.current_story_order).first() if s is None: return f = Fragment.objects.filter(story=s, text=Emoji.unicode_to_shortcode( msg['text'])).first() if f is None: return return f.action
def start_story(bulletin, chat_user, chat_id): print 'NEW STORY' story = Story.objects( bulletin=bulletin).order_by('order')[chat_user.current_story] answer = Fragment.objects.filter( story=story, type__ne=Fragment.TYPE_IMAGE).order_by('order')[0] show_keyboard = { 'keyboard': [[Emoji.shortcode_to_unicode(answer.text), 'Next']], 'one_time_keyboard': True, 'resize_keyboard': True } bot.sendChatAction(chat_id, "typing") time.sleep(TYPING_TIME) bot.sendMessage(chat_id, story.lead, reply_markup=show_keyboard, disable_web_page_preview=True) chat_user.current_fragment += 1
def emoji2cdf(emoji, emoji_width=1): ''' Converts the inputted emoji to numpy array and feeds it into image2cdf. The emoji_width parameter helps scale the emoji properly for the cfd solver ''' emoji_img_item = Emoji.to_image(emoji) start = a.find("src=") link = a[start + 5:-3] im = imageio.imread(link) aerodynamics = { 'coefficient_of_drag': .3, 'coefficient_of_lift': 1, 'wing_area': 1, 'frontal_area': .2, 'properties': 1 } return aerodynamics
def polls(): poll_fragments = [] for bulletin in g.current_account.bulletins: for story in bulletin.content: for fragment in story.content: if fragment.type == Fragment.TYPE_POLL: fragment.text = Emoji.shortcode_to_unicode(fragment.text) poll_fragments.append(fragment) polls = PollQuestions.objects(fragment__in=poll_fragments) for f in poll_fragments: for p in polls: if f == p.fragment: if not hasattr(f, 'answers'): f.answers = 0 f.answers += len(p.users) return render_template('polls/index.html', poll_fragments=poll_fragments)
def get_poll_keyboard(self, fragment, question_id=None): def get_callback_data(fragment, question): if isinstance(fragment, Document): fragment = fragment.id if isinstance(question, Document): question = question.id return '{fragment},{question}'.format(fragment=fragment, question=question) buttons = [] for q in PollQuestions.objects(fragment=fragment): text = q.text if question_id and str(q.id) == question_id: text = q.text + Emoji.shortcode_to_unicode(' :white_check_mark:') buttons.append( [InlineKeyboardButton(text=text, callback_data=get_callback_data(fragment, q))] ) return InlineKeyboardMarkup(inline_keyboard=buttons)
def ascii_to_image(text, **kwargs): return Emoji.ascii_to_image(text, **kwargs)
def emoji2image(emoji): a = Emoji.to_image(emoji) url = a[a.find("src=")+5:a.find("/>")-1] image = imageio.imread(url) return image
def test_single_unicode_char(self): unicode = '🐌' shortcode = ':snail:' image = '<img class="emojione" alt="🐌" src="https://cdn.jsdelivr.net/emojione/assets/3.1/png/64/1f40c.png"/>' self.assertEqual(Emoji.unicode_to_image(unicode), image) self.assertEqual(Emoji.shortcode_to_image(shortcode), image)
def test_emoji_at_start_of_sentence(self): unicode = '🐌 mail.' shortcode = ':snail: mail.' image = '<img class="emojione" alt="🐌" src="https://cdn.jsdelivr.net/emojione/assets/3.1/png/64/1f40c.png"/> mail.' self.assertEqual(Emoji.unicode_to_image(unicode), image) self.assertEqual(Emoji.shortcode_to_image(shortcode), image)
def test_emoji_inside_img_tag(self): unicode = 'The <img class="emojione" alt="🐌" src="https://cdn.jsdelivr.net/emojione/assets/png/1f40c.png" /> is Emoji One\'s official mascot.' self.assertEqual(Emoji.unicode_to_image(unicode), unicode) self.assertEqual(Emoji.shortcode_to_image(unicode), unicode)
def test_emoji_at_end_of_sentence_with_preceeding_colon(self): unicode = 'EmojiOne\'s original mascot was: 🐌' shortcode = 'EmojiOne\'s original mascot was: :snail:' image = 'EmojiOne\'s original mascot was: <img class="joypixels" alt="🐌" src="https://cdn.jsdelivr.net/joypixels/assets/4.5/png/64/1f40c.png"/>' self.assertEqual(Emoji.unicode_to_image(unicode), image) self.assertEqual(Emoji.shortcode_to_image(shortcode), image)
def bearImage(): rawHTML = Emoji.to_image("\U0001F43B") downloadedImage = image(rawHTML) return downloadedImage
def send_text_fragment(self, chat_id, f): self.bot.sendMessage(chat_id, Emoji.shortcode_to_unicode(f.text), disable_web_page_preview=True)
def test_emoji_inside_sentence_with_comma(self): unicode = 'The 🐌, is Emoji One\'s official mascot.' shortcode = 'The :snail:, is Emoji One\'s official mascot.' image = 'The <img class="emojione" alt="🐌" src="//cdn.jsdelivr.net/emojione/assets/png/1F40C.png'+self.cache_bust_param+'"/>, is Emoji One\'s official mascot.' self.assertEqual(Emoji.unicode_to_image(unicode), image) self.assertEqual(Emoji.shortcode_to_image(shortcode), image)
def test_single_unicode_char(self): unicode = '🐌' shortcode = ':snail:' image = '<img class="emojione" alt="🐌" src="//cdn.jsdelivr.net/emojione/assets/png/1F40C.png'+self.cache_bust_param+'"/>' self.assertEqual(Emoji.unicode_to_image(unicode), image) self.assertEqual(Emoji.shortcode_to_image(shortcode), image)
def test_emoji_at_end_of_sentence_with_alternate_punctuation(self): unicode = 'EmojiOne\'s original mascot was 🐌!' shortcode = 'EmojiOne\'s original mascot was :snail:!' image = 'EmojiOne\'s original mascot was <img class="joypixels" alt="🐌" src="https://cdn.jsdelivr.net/joypixels/assets/4.5/png/64/1f40c.png"/>!' self.assertEqual(Emoji.unicode_to_image(unicode), image) self.assertEqual(Emoji.shortcode_to_image(shortcode), image)
def shortcode_to_image(text, **kwargs): return Emoji.shortcode_to_image(text, **kwargs)
def test_emoji_at_start_of_sentence_with_apostrophe(self): unicode = '🐌\'s are cool!' shortcode = ':snail:\'s are cool!' image = '<img class="emojione" alt="🐌" src="https://cdn.jsdelivr.net/emojione/assets/png/1F40C.png'+self.cache_bust_param+'"/>\'s are cool!' self.assertEqual(Emoji.unicode_to_image(unicode), image) self.assertEqual(Emoji.shortcode_to_image(shortcode), image)
def rainbowImage(): rawHTML = Emoji.to_image("\U0001F308") downloadedImage = image(rawHTML) return downloadedImage
def test_emoji_inside_img_tag(self): unicode = 'The <img class="joypixels" alt="🐌" src="https://cdn.jsdelivr.net/joypixels/assets/4.5/png/64/1f40c.png"/> was EmojiOne\'s original mascot.' self.assertEqual(Emoji.unicode_to_image(unicode), unicode) self.assertEqual(Emoji.shortcode_to_image(unicode), unicode)
def bullImage(): rawHTML = Emoji.to_image("\U0001F402") downloadedImage = image(rawHTML) return downloadedImage
def test_ascii_char(self): ascii = ">:/" image = '<img class="emojione" alt="😕" src="https://cdn.jsdelivr.net/emojione/assets/3.1/png/64/1f615.png"/>' self.assertEqual(Emoji.ascii_to_image(ascii), image)
def test_emoji_at_end_of_sentence(self): unicode = 'Emoji One\'s official mascot is 🐌.' shortcode = 'Emoji One\'s official mascot is :snail:.' image = 'Emoji One\'s official mascot is <img class="emojione" alt="🐌" src="https://cdn.jsdelivr.net/emojione/assets/png/1F40C.png'+self.cache_bust_param+'"/>.' self.assertEqual(Emoji.unicode_to_image(unicode), image) self.assertEqual(Emoji.shortcode_to_image(shortcode), image)
def test_unicode_to_image(self): txt = 'Hello world! 😄 :smile:' expected = """Hello world! <img class="emojione" alt="😄" src="https://cdn.jsdelivr.net/emojione/assets/png/1f604.png%s"/> :smile:""" %\ Emoji.cache_bust_param self.assertEqual(Emoji.unicode_to_image(txt), expected)
def test_emoji_inside_object_tag(self): unicode = 'The <object class="emojione" data="//cdn.jsdelivr.net/emojione/assets/svg/1f40c.svg" type="image/svg+xml" standby="🐌">🐌</object> is Emoji One\'s official mascot' self.assertEqual(Emoji.unicode_to_image(unicode), unicode) self.assertEqual(Emoji.shortcode_to_image(unicode), unicode)
def test_emoji_at_end_of_sentence_with_preceeding_colon(self): unicode = 'EmojiOne\'s original mascot: 🐌' shortcode = 'EmojiOne\'s original mascot: :snail:' image = 'EmojiOne\'s original mascot: <img class="emojione" alt="🐌" src="https://cdn.jsdelivr.net/emojione/assets/3.1/png/64/1f40c.png"/>' self.assertEqual(Emoji.unicode_to_image(unicode), image) self.assertEqual(Emoji.shortcode_to_image(shortcode), image)
def test_emoji_inside_sentence_with_comma(self): unicode = 'The 🐌, is EmojiOne\'s original mascot.' shortcode = 'The :snail:, is EmojiOne\'s original mascot.' image = 'The <img class="emojione" alt="🐌" src="https://cdn.jsdelivr.net/emojione/assets/3.1/png/64/1f40c.png"/>, is EmojiOne\'s original mascot.' self.assertEqual(Emoji.unicode_to_image(unicode), image) self.assertEqual(Emoji.shortcode_to_image(shortcode), image)
def test_emoji_at_end_of_sentence_with_alternate_punctuation(self): unicode = 'EmojiOne\'s original mascot is 🐌!' shortcode = 'EmojiOne\'s original mascot is :snail:!' image = 'EmojiOne\'s original mascot is <img class="emojione" alt="🐌" src="https://cdn.jsdelivr.net/emojione/assets/3.1/png/64/1f40c.png"/>!' self.assertEqual(Emoji.unicode_to_image(unicode), image) self.assertEqual(Emoji.shortcode_to_image(shortcode), image)
def unicode_to_image(text, **kwargs): return Emoji.unicode_to_image(text, **kwargs)
def ascii_to_unicode(text, **kwargs): return Emoji.ascii_to_unicode(text, **kwargs)
def to_image(text, **kwargs): return Emoji.to_image(text, **kwargs)
def test_ascii_char(self): ascii = ">:/" image = '<img class="joypixels" alt="😕" src="https://cdn.jsdelivr.net/joypixels/assets/4.5/png/64/1f615.png"/>' self.assertEqual(Emoji.ascii_to_image(ascii), image)