def goToSettingsButtonsDensity(p): text_msg = "Your current button density is set to: " + person.getHighlightedString(p,str(p.maxCharsPerLine)) + \ " characters per line. Press on " + BACK_BUTTON +\ 'if you are happy with this choice, otherwise enter a new value between ' + \ person.getHighlightedString(p, str(MIN_CHARS_PER_LINE)) + ' and ' +\ person.getHighlightedString(p, str(MAX_CHARS_PER_LINE)) + '.\n\n' +\ "The buttons below will show you how the density changes according to the value.\n\n" sampleSentence = "This is a sample sentence to show you how the buttons density changes according to your settings" sentenceButtons = util.splitTextOnSpaces(sampleSentence) sentenceButtonsArrangement = util.segmentArrayOnMaxChars( sentenceButtons, p.maxCharsPerLine, ) kb = sentenceButtonsArrangement kb.insert(0, [BACK_BUTTON]) tell(p.chat_id, text_msg, kb, markdown=True) person.setState(p, -12)
def goToSettingsHighlightingMode(p): text_msg = "Your current highlighting mode is set to: " + \ person.getHighlightedString(p, p.highlightingMode) + "\n" + "The options are: \n\n" options = person.MARKDOWN_OPTIONS.keys() for mode in options: markdownChar = person.MARKDOWN_OPTIONS[mode] text_msg += " " + mode + ": " + markdownChar + "sample text" + markdownChar + "\n" text_msg += "\nSelect an highlighting mode." logging.debug('dealWithSettingHighlightingMode: ' + text_msg) kb = [options, [BACK_BUTTON]] tell(p.chat_id, text_msg, kb=kb, markdown=True) person.setState(p, -11)
def renderMwe(p, indexes, showCategory=False, showConfidence=False): result = "" sentence_word_tokens = sentAnno.getCurrentSentenceSplitSpaceToken( p) # [[tok1,tok2],[tok3,tok4],...] token_index = 0 started = False hasGap = False markdownChar = person.getMarkdownChar(p) for w in sentence_word_tokens: selectedWord = False for t in w: num = '(' + str(token_index + 1) + ')' if person.containsMarkdownSymbols(t): # Tags must not be nested. https://core.telegram.org/bots/api#formatting-options ecaped_md_num_token = util.escapeMarkdown(t + num) else: ecaped_md_num_token = markdownChar + t + markdownChar + num if token_index in indexes: if hasGap: result += "... " hasGap = False result += ecaped_md_num_token started = True selectedWord = True elif started: hasGap = True token_index += 1 if selectedWord: result += ' ' result.strip() if showCategory or showConfidence: if sentAnno.LANGUAGE_LEFT_TO_RIGHT[p.language]: ARROW = RIGHT_ARROW else: ARROW = LEFT_ARROW cat, conf = sentAnno.getCategoryAndConfidence(p, indexes) if showCategory: category = person.getHighlightedString(p, cat) result += " " + ARROW + " " + category if sentAnno.CONFIDENCE_ACTIVE and showConfidence: confidence = ' ' + HISTOGRAM + '=' + NUMBERS_0_10[ conf] if sentAnno.CONFIDENCE_ACTIVE else '' result += confidence return result
def renderSentenceWithMwesList(p, slashedNumbers=True): sentenceNumber = str(person.getCurrentSentenceIndex(p) + 1) totalSentences = sentAnno.totalSentPersonCurrentLang(p) sentence_flat = util.escapeMarkdown(sentAnno.getCurrentSentenceFlat(p)) text_msg = "Current sentence (" + \ person.getHighlightedString(p, sentenceNumber + '/' + str(totalSentences)) + \ '):\n\n' + \ SECTION_SEPARATOR_BOTTOM + "\n" + \ sentence_flat + "\n" + \ SECTION_SEPARATOR_BOTTOM + '\n\n' currentMwes = p.selectedMwes markDownChar = person.getMarkdownChar(p) if currentMwes == None: text_msg += CANCEL + " You have " + markDownChar + "NOT" + markDownChar + " annotated this sentence." elif currentMwes == {}: text_msg += WRITE + " YOUR ANNOTATION:\n" + EMPTY_BIN + " You marked this sentence with " + \ markDownChar + "NO MWEs" + markDownChar + "." else: mweCount = len(currentMwes) text_msg += WRITE + " YOUR ANNOTATION: " + str(mweCount) + " MWE" + \ ("s:" if mweCount>1 else ":") + "\n" + renderSelectedMwes(p, slashedNumbers) return text_msg
def post(self): urlfetch.set_default_fetch_deadline(60) body = json.loads(self.request.body) logging.info('request body:') logging.info(body) self.response.write(json.dumps(body)) # update_id = body['update_id'] message = body['message'] #message_id = message.get('message_id') # date = message.get('date') if "chat" not in message: return # fr = message.get('from') chat = message['chat'] chat_id = chat['id'] if "first_name" not in chat: return text = message.get('text').encode('utf-8') if "text" in message else "" name = chat["first_name"].encode('utf-8') last_name = chat["last_name"].encode( 'utf-8') if "last_name" in chat else "-" username = chat["username"] if "username" in chat else "-" #location = message["location"] if "location" in message else None #logging.debug('location: ' + str(location)) def reply(msg=None, kb=None, hideKb=True, markdown=False): tell(chat_id, msg, kb, hideKb, markdown) p = ndb.Key(Person, str(chat_id)).get() if p is None: # new user logging.info("Text: " + text) if text == '/help': reply(INSTRUCTIONS) elif text.startswith("/start"): tell_masters("New user: "******"Hi " + name + ", " + "welcome to the Parseme Shared Task annotation!") restart(p) else: reply("Something didn't work... please contact @kercos") else: # known user person.updateUsername(p, username) if text == '/state': if p.state in STATES: reply("You are in state " + str(p.state) + ": " + STATES[p.state]) else: reply("You are in state " + str(p.state)) elif text.startswith("/start"): reply("Hi " + name + ", " + "welcome back to the Parseme Shared Task annotation!") person.reInitializePerson(p, chat_id, name, last_name, username) restart(p) #------------- # SETTINGS #------------- elif p.state == -1: # settings if text == BACK_BUTTON: restart(p) elif text == HIGHTLIGHTING_MODE_BUTTON: goToSettingsHighlightingMode(p) # state -11 elif text == BUTTONS_DENSITY_BUTTON: goToSettingsButtonsDensity(p) # state -12 else: reply(FROWNING_FACE + " Sorry, I can't understand...") elif p.state == -11: # settings -> hightlighting mode if text == BACK_BUTTON: goToSetting(p) elif text in person.MARKDOWN_OPTIONS.keys(): person.setHighlightingMode(p, text) reply("Successfully set highlighting mode to " + person.getHighlightedString(p, text), markdown=True) goToSetting(p) #state -1 else: reply(FROWNING_FACE + " Sorry, I can't understand...") elif p.state == -12: # settings -> buttons density if text == BACK_BUTTON: goToSetting(p) elif util.RepresentsInt(text): charPerLine = int(text) tot_sentences = sentAnno.totalSentPersonCurrentLang(p) if charPerLine >= MIN_CHARS_PER_LINE and charPerLine <= MAX_CHARS_PER_LINE: person.setMaxCharsPerLine(p, charPerLine) goToSettingsButtonsDensity(p) # state -1 else: reply( "Sorry, not a valid index. Please enter an index between " + person.getHighlightedString( p, str(MIN_CHARS_PER_LINE)) + " and " + person.getHighlightedString( p, str(MAX_CHARS_PER_LINE)) + ".") else: reply(FROWNING_FACE + " Sorry, I can't understand...") # ------------- # INITIAL STATE # ------------- elif p.state == 0: # INITIAL STATE if text in ['/help', HELP_BUTTON]: reply(INSTRUCTIONS) elif text == SETTINGS_BUTTON: goToSetting(p) #state -1 elif text == INFO_BUTTON: textMsg = "Number of annotated sentences per language:\n" for lang, flag in sentAnno.LANGUAGE_FLAGS.iteritems(): progress = str(person.getLanguageProgress(p, lang)) total = str(sentAnno.totalSentLang(lang)) textMsg += flag + ' ' + progress + '/' + total + '\n' reply(textMsg) elif text == ANNOTATE_BUTTON_EN: person.setLanguage(p, 'ENGLISH') person.importSelectedMwe(p) displayCurrentSentence(p) #state 1 elif text == ANNOTATE_BUTTON_IR: person.setLanguage(p, 'FARSI') person.importSelectedMwe(p) displayCurrentSentence(p) # state 1 elif chat_id in key.MASTER_CHAT_ID: if text == '/test': logging.debug('test') #description = object.getObjectsById(4866725958909952) #logging.debug('description: ' + description) #logging.debug('type: ' + str(type(description))) #reply(description) #reply("Méthode de Français") #deferred.defer(tell_fede, "Hello, world!") elif text == '/testMarkdown': reply( "aaaa*xxxx*zzzzz\naaaa_xxxx_zzzz\naaaa`xxxx`zzzz", markdown=True) #reply("fdsadfa [dfsafd] dsfasdf a", markdown=True) #reply("fdasfd *df\\*as* fa fsa", markdown=True) #text = "*" + util.escapeMarkdown("`[8]") + "*" #reply(text, markdown=True) #logging.debug("reply text: " + text) elif text == '/testBitMap': #bitmaptest.testBitMap(100, 20) bitmaptest.testBitMap(5000, 500) reply('Successfully finished test (see log).') elif text.startswith('/broadcast ') and len(text) > 11: msg = text[11:] #.encode('utf-8') deferred.defer(broadcast, msg) elif text == '/restartAllUsers': deferred.defer( restartAllUsers, "ParsemeBot v.2 is out! " + GRINNING_FACE) elif text == '/resetAllUsers': c = resetAllUsers() reply("Resetted users: " + str(c)) else: reply(FROWNING_FACE + " Sorry, I can't understand...") #setLanguage(d.language) else: reply(FROWNING_FACE + "Sorry I didn't understand you ...\n" + "Press the HELP button to get more info.") # -------------------------- # DISPLAY CURRENT SENTENCE # -------------------------- elif p.state == 1: # display current sentence if text == BACK_BUTTON: restart(p) elif text == '/progress': reply("Annotated sentence(s) for " + p.language + ': ' + str(person.getLanguageProgress(p)) + '/' + str(sentAnno.totalSentPersonCurrentLang(p))) elif text == '/nextna': index = person.getNextNonAnnSentIndex(p) if index: jumpToSentence(p, index + 1) else: reply(EXCLAMATION + " No next non-annotated sentence") elif text == '/prevna': index = person.getPrevNonAnnSentIndex(p) if index: jumpToSentence(p, index + 1) else: reply(EXCLAMATION + " No previous non-annotated sentence") elif text == ADD_MWE_BUTTON: prepareToSelectTokens(p) #state 30 elif text == NO_MWE_BUTTON and p.selectedMwes == None: reply( CHECK + " The current sentence has been marked without MWEs.") sentAnno.setEmptyMwes(p) #mirroring on person #person.setEmptyMwes(p) person.setCurrentSentenceAnnotated(p) displayCurrentSentence(p) # state 1 elif text == MOD_MWE_BUTTON and p.selectedMwes != None: # reply(UNDER_CONSTRUCTION + " currently working on this.") askWhichAnnotationToModify(p) # state 50 elif text == REMOVE_MWE_BUTTON and p.selectedMwes != None: askWhichAnnotationToRemove(p) # state 60 elif text == NEXT_BUTTON: if person.hasNextSentece(p): person.goToNextSentence(p) person.importSelectedMwe(p) displayCurrentSentence(p) #state 1 else: displayCurrentSentence(p) reply(EXCLAMATION + " You reached the last sentence") # state 1 # state 1 elif text == PREV_BUTTON: if person.hasPreviousSentece(p): person.goToPrevSentence(p) person.importSelectedMwe(p) displayCurrentSentence(p) #state 1 else: displayCurrentSentence(p) reply(EXCLAMATION + " You reached the first sentence") # state 1 # state 1 elif text == GO_TO_BUTTON: tot_sentences = sentAnno.totalSentPersonCurrentLang(p) reply( "Please insert the index of the sentence you want to jumpt to, between " + person.getHighlightedString(p, str(1)) + " and " + person.getHighlightedString(p, str(tot_sentences)) + " (you can also enter the number without pressing the " + JUMP_ARROW + " button).", markdown=True) elif text.startswith('/') and util.RepresentsInt(text[1:]): mwe_number = int(text[1:]) jumpToModifySingleMwe(p, mwe_number) #state 40 elif util.RepresentsInt(text): index = int(text) tot_sentences = sentAnno.totalSentPersonCurrentLang(p) if index > 0 and index <= tot_sentences: jumpToSentence(p, index) # state 1 else: reply( EXCLAMATION + "Not a valid index. If you want to jump to a specific sentence, " "please enter an index between " + person.getHighlightedString(p, str(1)) + " and " + person.getHighlightedString( p, str(tot_sentences)) + '.', markdown=True) else: reply(FROWNING_FACE + " Sorry, I can't understand...") elif p.state == 30: # add MWE -> select tokens if text == BACK_BUTTON: displayCurrentSentence(p) # state 1 elif text.endswith(']'): token_number_str = text[text.rindex('[') + 1:-1] #logging.debug('number_str: ' + token_number_str) if util.RepresentsInt(token_number_str): token_number = int(token_number_str) if token_number > 0 and token_number <= len( p.selectedTokens): dealWithTokensSelection(p, token_number - 1) #state 30 else: reply("Not a valid token.") else: reply("Not a valid token.") elif text == CHOOSE_CAT_BUTTON: currentMwes = p.selectedMwes if currentMwes and person.getSelectedTokensIndexTuple( p) in currentMwes: reply(EXCLAMATION + ' MWE has been already inserted!') else: askAnnotationLabel(p) # state 31 elif text == CONFIRM_CHANNGES: dealWithChangeMwe(p) # state 1 else: reply(FROWNING_FACE + " Sorry, I can't understand...") elif p.state == 31: # add MWE -> select category if text == BACK_BUTTON: if p.last_state == 40: displayCurrentSentence(p) # state 1 else: dealWithTokensSelection(p) # state 30 elif text in categories.ANNOTATION_CATS: indexes = person.getSelectedTokensIndexTuple(p) mwe_flat = renderMwe(p, indexes, False, False) reply(CHECK + " The MWE " + mwe_flat + " has been marked with category " + person.getHighlightedString(p, text) + '\n', markdown=True) sentAnno.appendMwe(p, text) #mirroring on person #person.appendMwe(p, text) person.setCurrentSentenceAnnotated(p) displayCurrentSentence(p) # state 1 else: reply(FROWNING_FACE + " Sorry, I can't understand...") elif p.state == 40: # user has selected a single MWE via the /number option if text == BACK_BUTTON: displayCurrentSentence(p) # state 1 elif text == CHANGE_CAT_BUTTON: askAnnotationLabel(p) # state 31 elif text == REMOVE_BUTTON: mwe_to_remove = person.getSelectedTokensIndexTuple(p) mwe_flat = renderMwe(p, mwe_to_remove, True, False) #logging.debug("after REMOVE_MWE_BUTTON: " + str(mwe_to_remove)) #logging.debug("mwe_to_remove: " + str(mwe_to_remove)) #logging.debug("current mwes: " + str(p.selectedMwes)) sentAnno.removeMwe(p, mwe_to_remove) #mirroring on person #person.removeMwe(p, mwe_to_remove) tell(p.chat_id, CHECK + " The MWE " + mwe_flat + " has been removed!", markdown=True) displayCurrentSentence(p) # state 1 elif text == CHANGE_MWE_ELEMENTS: p.currentMweTmp = person.getSelectedTokensIndexTuple(p) dealWithTokensSelection(p) # state 30 elif text == CHANGE_MWE_CONFIDENCE: askConfidenceLevel(p) # state 70 else: reply(FROWNING_FACE + " Sorry, I can't understand...") elif p.state == 50: # modify MWE if text == BACK_BUTTON: displayCurrentSentence(p) # state 1 elif util.RepresentsInt(text): mwe_number = int(text) jumpToModifySingleMwe(p, mwe_number) # state 40 else: reply(FROWNING_FACE + " Sorry, I can't understand...") elif p.state == 60: # remove MWE if text == BACK_BUTTON: displayCurrentSentence(p) # state 1 elif util.RepresentsInt(text): mwe_number = int(text) dealWithRemoveMwe(p, mwe_number) # state 1 else: reply(FROWNING_FACE + " Sorry, I can't understand...") elif p.state == 70: # change confidence level MWE if text == BACK_BUTTON: displayCurrentSentence(p) # state 1 elif util.RepresentsInt(text): newConf = int(text) dealWithChangeConfidenceLevel(p, newConf) # state 1 elif text in NUMBERS_0_10[1:]: newConf = NUMBERS_0_10.index(text) dealWithChangeConfidenceLevel(p, newConf) # state 1 else: reply(FROWNING_FACE + " Sorry, I can't understand...") else: reply("There is a problem (" + str(p.state).encode('utf-8') + "). Report this to @kercos" + '\n') restart(p)