def parse(message): try: if message.text == '/reset' or message.text == '/start': return bot.send_message(message.chat.id, parser.parse_text(message.text)) except Exception: pass
def main(): print("Welcome to DnZ (Definitely not Zork)!") name = input("What is your name?\n") age = input("What is your age?\n") place = input("Where are you from?\n") user.assign_values(name, age, place) print() print(user) print() while True: print("What would you like to do?") choice = input().lower().split() parser.parse_text(choice) # user.effect() print()
def update_tab_comment(request): result = dict() result["success"] = True result["message"] = "success" input_message = "" if request.method == "POST": if request.POST["message"]: input_message = smart_unicode( request.POST["message"], encoding="utf-8", strings_only=False, errors="strict" ) if request.POST["feed_id"]: feed_id = request.POST["feed_id"] if input_message is not "": try: user = User.objects.get(username=request.user.username) except: return my_utils.return_error("Please Sign in first") try: message = TabFeed.objects.filter(id=feed_id, is_deleted=False)[0] except: return my_utils.return_error("No such Message") try: new_comment = TabComment(author=user, contents=input_message, message=message) new_comment.save() message.save() except: return my_utils.return_error("Insert Failed") else: return my_utils.return_error("Empty Message") try: item = dict() item["id"] = new_comment.id item["author"] = new_comment.author.username # item['author_picture']=UserProfile.objects.get(user=new_comment.author).picture.url item["author_picture"] = my_utils.get_user_thumbnail(new_comment.author) item["author_name"] = new_comment.author.last_name item["contents"] = parser.parse_text(new_comment.contents) item["reg_date"] = str(new_comment.reg_date) result["comment"] = item except Exception as e: print str(e) return HttpResponse(json.dumps(result, indent=4), mimetype="application/json")
def update_event_comment(request): result=dict() result['success']=True result['message']='success' input_message='' if request.method == 'POST': if request.POST['message']: input_message=smart_unicode(request.POST['message'], encoding='utf-8', strings_only=False, errors='strict') if request.POST['event_id']: event_id = request.POST['event_id'] if input_message is not '': try: user = User.objects.get(username=request.user.username) except: return my_utils.return_error('Please Sign in first') try: event = Event.objects.filter(id=event_id,is_deleted=False)[0] except: return my_utils.return_error('No such Event') try: new_comment = EventComment(author=user,contents=input_message,event=event) new_comment.save() except: return my_utils.return_error('Insert Failed') #TODO: Add To event host & comment authors NOTIFICATION else: return my_utils.return_error('Empty Message') try: item = dict() item['id']=new_comment.id item['author']=new_comment.author.username item['author_picture']=UserProfile.objects.get(user=new_comment.author).picture.url item['author_name']=new_comment.author.last_name item['contents']= parser.parse_text(new_comment.contents) item['reg_date']= str(new_comment.reg_date) result['comment']=item except Exception as e: print str(e) return HttpResponse(json.dumps(result, indent=4), mimetype='application/json')
def message_detail(request, message_id): t = loader.get_template('message_detail.html') context = RequestContext(request) my_utils.load_basic_info(request, context) try: user = User.objects.get(username=request.user.username) username = request.user.username username +="," except Exception as e: print str(e) return HttpResponseRedirect('signin') query_type = Q(author=user) | Q(receivers__contains=username) try: d_message = DirectMessage.objects.filter(query_type,is_deleted=False,id=message_id)[0] context['message']=d_message context['message'].author_profile=UserProfile.objects.get(user=d_message.author) context['message'].contents=parser.parse_text(d_message.contents) context['message'].receivers=context['message'].receivers.replace(",", ", ")[:-2] context['message'].reg_date = d_message.reg_date.strftime('%Y-%m-%d %H:%M:%S') #2011-06-14 22:23:10 context['type'] = 'all' if request.GET.get('type',False): context['type'] = request.GET['type'] except Exception as e: print str(e) context['message']=None context['error_message'] = 'You cannot access to this message.' return HttpResponse(t.render(context)) try: replies = DirectMessageReply.objects.filter(direct_message=d_message, is_deleted=False).order_by('reg_date') for reply in replies: reply.author_profile = UserProfile.objects.get(user=reply.author) reply.reg_date = reply.reg_date.strftime('%Y-%m-%d %H:%M:%S') context['replies']= replies print replies except Exception as e: print str(e) return HttpResponse(t.render(context))
def compile_view_ref(self, table_expr, view): # TODO(alan): This code allows fields from the view's implicit column # context to be selected, which probably isn't allowed in regular # BigQuery. # TODO(alan): We should check for cycles when evaluating views. # Otherwise, circular views will cause an infinite loop. # The view keeps its query as regular text, so we need to lex and parse # it, then include it as if it was a subquery. It's almost correct to # re-use the subquery compiling code, except that subquery aliases have # special semantics that we don't want to use; an alias on a view # should count for all returned fields. alias = table_expr.alias or table_expr.name uncompiled_view_ast = parser.parse_text(view.query) compiled_view_select = self.compile_select(uncompiled_view_ast) # We always want to apply either the alias or the full table name to # the returned type context. new_type_context = ( compiled_view_select.type_ctx.context_with_full_alias(alias)) return compiled_view_select.with_type_ctx(new_type_context)
if start is None: start = soup.find('a', {'name': start_href[1:]}) lines = parser.lines_between(start, [e for e in ends if e != start]) story = gutenberg.strip_headers(''.join(lines)).strip() write_path = path.join( story_dir, '%s-%04d-%04d.txt' % (slugify(title + ' by ' + author), index, i)) out.writerow([title, author, continent, country, write_path]) open(write_path, 'w', encoding='utf8').write(story) elif ext == '.txt': # txt stories = parser.parse_text(text, titles) if not stories: print(filename) if len(stories) != len(titles): print( filename, set([title for title, author in titles]) - set([title for title, author in stories.keys()])) i = 0 for (title, author), lines in stories.items(): i += 1 story = '\n'.join(lines).strip() if not story: print(title, filename) write_path = path.join(
def get_reply(text): parsed_text = parse_text(text) if parsed_text == "" or parsed_text is None: return "Sorry, I can't understand what are you saying" result = json.loads(parsed_text) global semester global dept global academy global subject print result if result['intent_type'] == 'GreetingIntent': return greeting_replies elif result['intent_type'] == 'GoodbyeIntent': return goodbye_replies elif result['intent_type'] == 'AcademyIntent': academy = result['AcademyKeyword'] reply = "Niceee! You are looking for " + str(academy) + "." if subject == "": reply += " What subject " + academy + " do you need?" return reply elif semester == "": reply += " Which semester do you read in?" return reply elif dept == "": reply += " Which dept. do you read in?" return reply else: return "Here is what I found for you" elif result['intent_type'] == 'DepartmentIntent': reply = "Awesome! You are a " + str( result['DepartmentKeyword']) + " student." dept = result['DepartmentKeyword'] if subject == "": reply += " Which subject?" return reply elif semester == "": reply += " Which semester do you read in?" return reply elif academy == "": reply += " What do you need? Chotha, Book or Lab Report?" return reply else: return "Here is what I found for you" elif result['intent_type'] == 'SubjectIntent': reply = "" if academy != "": reply = "Cool! You need a " + str( result['SubjectKeyword']) + academy subject = result['SubjectKeyword'] if academy == "": reply += " What subject " + academy + " do you need?" return reply elif semester == "": reply += " Which semester do you read in?" return reply elif dept == "": reply += " Which dept. do you read in?" return reply else: return "Here is what I found for you" elif result['intent_type'] == 'SemesterIntent': reply = "I see! You are a student of " + str(result['SemesterKeyword']) semester = result['SemesterKeyword'] if academy == "": reply += " What subject " + academy + " do you need?" return reply elif subject == "": reply += " Which subject?" return reply elif dept == "": reply += " Which dept. do you read in?" return reply else: return "Here is what I found for you"
def process_events(events, user): results=list() for event in events: item = dict() item['id']=event.id item['base_id']=event.id item['host']=event.host.username item['host_picture']=UserProfile.objects.get(user=event.host).picture.url item['host_name']=event.host.last_name item['title']= event.title item['location']= event.location item['attend_open']= (datetime.now() < event.start_time) try: item['start_time'] = str(event.start_time) except: pass try: item['end_time'] = str(event.end_time) except: pass item['contents']= parser.parse_text(event.contents) item['invited_users']= event.invited_users.replace(",", ", ") item['reg_date']= str(event.reg_date) item['public']=event.is_public item['comments']=list() try: comments = EventComment.objects.filter(event=event, is_deleted=False).order_by('reg_date') for comment in comments: c_item = dict() c_item['id']=comment.id c_item['author']=comment.author.username c_item['author_picture']=UserProfile.objects.get(user=comment.author).picture.url c_item['author_name']=comment.author.last_name c_item['contents']= parser.parse_text(comment.contents) c_item['reg_date']= str(comment.reg_date) item['comments'].append(c_item) except: pass item['attendees']=list() try: attendees = EventParticipate.objects.filter(event=event) for attendee in attendees: a_item=dict() a_item['username']=attendee.user.username a_item['picture']=UserProfile.objects.get(user=attendee.user).picture.url a_item['name']=attendee.user.last_name if attendee.attend_status == 'yes': item['attendees'].append(a_item) if attendee.user.username == user.username: item['attend_status']=attendee.attend_status print attendee.attend_status except: pass try: attending = EventParticipate.objects.filter(event=event, user=user)[0] item['attending'] = attending.attend_status except: item['attending'] = 'not yet' pass results.append(item) return results
def update_comment(request): result=dict() result['success']=True result['message']='success' input_message='' if request.method == 'POST': if request.POST['message']: input_message=smart_unicode(request.POST['message'], encoding='utf-8', strings_only=False, errors='strict') if request.POST['feed_id']: feed_id = request.POST['feed_id'] if input_message is not '': try: user = User.objects.get(username=request.user.username) except: return my_utils.return_error('Please Sign in first') try: message = Message.objects.filter(id=feed_id,is_deleted=False)[0] except: return my_utils.return_error('No such Message') try: new_comment = Comment(author=user,contents=input_message,message=message) new_comment.save() message.save() except: return my_utils.return_error('Insert Failed') #Add To author Timeline try: author_timeline_new = UserTimeline.objects.filter(message=message,user=user)[0] except: #현재 없는 경우에만 넣는다. try: print 'hi' author_timeline_new = UserTimeline.objects.get_or_create(message=message,user=user)[0] author_timeline_new.save() except: pass try: related_timelines = UserTimeline.objects.filter(message=message) if not related_timelines: pass for timeline in related_timelines: try: if timeline.user.username != request.user.username: timeline.save() #SEND NOTIFICATION info = dict() info['from'] = request.user info['to'] = timeline.user info['comment'] = input_message info['target_object'] = message register_noti(request, "new_comment",info) except: pass except Exception as e: print str(e) return my_utils.return_error('Related Timelilne Failed') #Question! Should do insert into related Topic timeline? else: return my_utils.return_error('Empty Message') try: item = dict() item['id']=new_comment.id item['author']=new_comment.author.username #item['author_picture']=UserProfile.objects.get(user=new_comment.author).picture.url item['author_picture']=my_utils.get_user_thumbnail(new_comment.author) item['author_name']=new_comment.author.last_name item['contents']= parser.parse_text(new_comment.contents) item['reg_date']= str(new_comment.reg_date) result['comment']=item except Exception as e: print str(e) return HttpResponse(json.dumps(result, indent=4), mimetype='application/json')
def process_messages(request, messages): d_messages=list() for message in messages: d_message = dict() d_message['id']=message.id d_message['base_id']=message.id d_message['author']=message.author.username d_message['author_name']=message.author.last_name if request.user == message.author: d_message['author_yn']=True else: d_message['author_yn']=False try: user_profile = UserProfile.objects.get(user=message.author) d_message['author_picture']= user_profile.picture.url except: d_message['author_picture']='/media/default.png' try: latest_reply = DirectMessageReply.objects.filter(direct_message=message, is_deleted=False).order_by('-reg_date')[0] d_message['latest_reply']=latest_reply.contents except Exception as e: d_message['latest_reply']=message.contents d_message['contents']= parser.parse_text(message.contents) d_message['contents_original']=message.contents d_message['receivers']= message.receivers.replace(",", ", ")[:-2] name_list='' split_list=message.receivers.split(',') split_list.pop() for i, receiver in enumerate(split_list): try: if receiver == '': continue user = User.objects.get(username=receiver) name_list += user.last_name if i != len(split_list)-1: name_list += ', ' except: pass replies_list = DirectMessageReply.objects.filter(direct_message=message, is_deleted=False).order_by('reg_date') replies=list() for reply in replies_list: item = dict() if request.user == reply.author: item['author_yn']=True else: item['author_yn']=False item['author']=reply.author.username item['author_name']=reply.author.last_name item['contents']=reply.contents item['reg_date']=str(reply.reg_date)[:-3] item['pretty_date']= my_utils.pretty_date(reply.reg_date) try: user_profile = UserProfile.objects.get(user=reply.author) item['author_picture']= user_profile.picture.url except: item['author_picture']='/media/default.png' replies.append(item) d_message['replies']=replies d_message['receivers_name']= name_list d_message['reg_date']= str(message.reg_date)[:-3] d_message['update_date']= str(message.update_date) d_message['pretty_date'] = my_utils.pretty_date(message.reg_date) d_messages.append(d_message) return d_messages
def reply_message(request): result=dict() result['success']=True result['message']='success' reply_message='' message_id='' if request.method == 'POST': if request.POST['message']: reply_message=smart_unicode(request.POST['message'], encoding='utf-8', strings_only=False, errors='strict') if request.POST['message_id']: message_id=request.POST['message_id'] print reply_message if message_id is '': return my_utils.return_error('No Message ID') if reply_message is not '': try: user = User.objects.get(username=request.user.username) except: return my_utils.return_error('Sign in first') try: user = User.objects.get(username=request.user.username) username = request.user.username username +="," query_type = Q(author=user) | Q(receivers__contains=username) d_message = DirectMessage.objects.filter(query_type,is_deleted=False,id=message_id)[0] except: return my_utils.return_error('You are now allowed to this message') try: new_dm_reply = DirectMessageReply(direct_message=d_message,author=user,contents=reply_message) new_dm_reply.save() d_message.save() except: return my_utils.return_error('Send Reply Failure') try: item = dict() item['id']=new_dm_reply.id item['author']=new_dm_reply.author.username item['contents']= parser.parse_text(new_dm_reply.contents) item['reg_date']= str(new_dm_reply.reg_date) try: user_profile = UserProfile.objects.get(user=new_dm_reply.author) item['author_picture']= user_profile.picture.url except: item['author_picture']='/media/default.png' result['reply']=item except Exception as e: print str(e) #TODO : SEND NOTIFICATION TO USERS #SEND NOTIFICATION try: author_name = d_message.author.username target_user = User.objects.get(username=author_name) info = dict() info['from'] = user info['to'] = target_user info['target_object'] = d_message register_noti(request, "new_dm_reply",info) except: pass else: return my_utils.return_error('empty message') return HttpResponse(json.dumps(result, indent=4), mimetype='application/json')
def parser(bot, update, mock_parse_text): if update.message.text == '/reset' or update.message.text == '/start': return update.message.reply_text(parse_text(update.message.text))
def process_messages(request, messages): feeds=list() for message in messages: feed = dict() feed['result_type']='feed' feed['id']=message.id try: user_profile = UserProfile.objects.get(user=message.author) feed['author']=message.author.username except: continue try: feed['author_name']=message.author.last_name feed['author_dept']=user_profile.dept feed['author_position']=user_profile.position #feed['author_picture']= user_profile.picture.url feed['author_picture']= get_user_thumbnail(message.author) except: feed['author_picture']='/media/default.png' feed['author_name']=message.author.last_name feed['contents']= parser.parse_text(message.contents) feed['contents_original']= message.contents if message.lat != '' and message.lng != '': feed['lat'] = message.lat feed['lng'] = message.lng feed['reg_date']= str(message.reg_date) feed['pretty_date'] = pretty_date(message.reg_date) feed['comments'] = list() feed['file_list'] = list() try: feed['base_id']=message.base_id except: feed['base_id']=message.id try: comments = Comment.objects.filter(message=message, is_deleted=False).order_by('reg_date') for comment in comments: item = dict() item['id']=comment.id item['author']=comment.author.username item['author_name']=comment.author.last_name try: user_profile = UserProfile.objects.get(user=comment.author) item['author_picture']= get_user_thumbnail(comment.author) except: item['author_picture']='/media/default.png' item['contents']= parser.parse_text(comment.contents) item['contents_original']= comment.contents item['reg_date']= str(comment.reg_date) feed['comments'].append(item) except: pass attach_list = message.attach_files.split('.') attach_files = list() for a_file in attach_list: if a_file != '': attach_files.append(a_file) try: files = File.objects.filter(id__in=attach_files) feed['file_list']=process_files(files) except Exception as e: print str(e) pass try: user = User.objects.get(username=request.user.username) is_favorited = UserFavorite.objects.filter(message=message, user=user)[0] feed['favorite']=True except: feed['favorite']=False pass feeds.append(feed) return feeds
def compile_text(text, tables_by_name): ast = parser.parse_text(text) return Compiler(tables_by_name).compile_select(ast)
def assert_parsed_select(self, text, expected_ast): actual_ast = parser.parse_text(text) self.assertEqual( expected_ast, actual_ast, 'Expected: %s, Actual %s.\nReprs: %r vs. %r.' % (expected_ast, actual_ast, expected_ast, actual_ast))
def assert_parsed_select(self, text, expected_ast): actual_ast = parser.parse_text(text) self.assertEqual(expected_ast, actual_ast, 'Expected: %s, Actual %s.\nReprs: %r vs. %r.' % (expected_ast, actual_ast, expected_ast, actual_ast))