def build_index(directory): # read in the documents start = time.time() logging.debug('Reading in and tokenising the documents started at {}'.format(start)) all_documents = [] stories = Story.find('all', '') for story in stories: logging.debug('\t *', story.title) content = ''.join([ para.content + ' ' for para in story.get_approved_paragraphs()]) all_documents.append(Document(content, name=story.id)) logging.debug('Ended after {}'.format(time.time() - start)) start = time.time() logging.debug('Computing the word relevancy values started at {}'.format(start)) # compute the index index = defaultdict(defaultdict) for document in all_documents: index[document.name] = dict(build_index_for_doc(document, all_documents)) logging.debug('Ended after {}'.format(time.time() - start)) return index
def get_story(self): from dbapi.story import Story try: return Story.find('id', self.story_id)[0] except IndexError: raise Exception('This paragraph does not belong to any exsiting' ' story in the datebase.')
def build_index(directory): # read in the documents start = time.time() logging.debug('Reading in and tokenising the documents started at {}'.format(start)) # grab the stories (headers for the stories anyway) stories = Story.find('all', '') # load in the documents all_documents = [] for story in stories: logging.debug('\t *', story.title) content = story.title + ' ' + ' '.join([paragraph.content for paragraph in story.get_approved_paragraphs()]) all_documents.append(Document(content, name=story.id)) logging.debug('Ended after {} seconds'.format(time.time() - start)) start = time.time() logging.debug('Computing the word relevancy values started at {}'.format(start)) # compute the index index = defaultdict(defaultdict) for document in all_documents: for word in document.tokens: index[document.name][word] = ( term_freq(word, document, all_documents) * inverse_document_freq(word, document, all_documents) ) logging.debug('Ended after {} seconds'.format(time.time() - start)) return index
def add_to_story_tree(response, story_id): try: story_id = int(story_id) username = response.get_secure_cookie('username') user = User.find('username', str(username,'utf-8'))[0] addition_to_story = response.get_argument('paragraph') story = Story.find('id', story_id)[0] if False:#not Rules.check(addition_to_story, story.id): #return to story view without updating.... TODO: show error response.write('DR') response.redirect('/view_story/{}'.format(story_id)) return response.write('aoeuhotnaeuhaoe ') parent = Paragraph.find('id',int(response.get_argument('parentId')))[0] if parent: parent.chain_paragraph(user, addition_to_story).save() response.redirect('/view_story/{}'.format(story_id)) except Exception as e: response.write(str(e)) raise e
def add_comment(response, story_id): username = response.get_secure_cookie('username') user = User.find('username', str(username, 'utf-8')) if not user: raise Exception("Expected user account when adding to story") user = user[0] story = Story.find('id', story_id)[0] story.add_comment(user, response.get_argument('commentbox')).save() response.redirect('/view_story/{}'.format(story_id))
def view_story(response, id): user = response.get_secure_cookie('username') story = Story.find('id', id) author = User.find('id', story[0].author_id) if user is not None: context = {'current_user':User.find('username', str(user, 'utf-8'))[0], 'story':story[0], 'author':author[0]} else: context = {'current_user':None, 'story':story[0], 'author':author[0]} html = template.render_file('templates/viewingstory.html', context) response.write(html)
def view_story_list(response): user = response.get_secure_cookie('username') stories = Story.find('all', '') if user is not None: context = {'current_user':User.find('username', str(user, 'utf-8'))[0], 'stories':stories} else: context = {'current_user':None, 'stories':stories} html = template.render_file('templates/storylist.html', context) response.write(html)
def display_story_tree(resp, story_id): user = resp.get_secure_cookie('username') story = Story.find('id', story_id)[0] author = User.find('id', story.author_id)[0] if user is not None: user = User.find('username', str(user, 'utf-8'))[0] else: user = None try: resp.write(template.render_file('view_story_tree.html', {'node': create_tree(story), 'story': story, 'author': author, 'user': user})) except Exception as e: story.delete()
def search_results(response): context = defaultdict() user = response.get_secure_cookie('username') stories = Story.find('all', '') try: context.update({ 'current_user': User.find('username', str(user, 'utf-8'))[0]}) except TypeError: context.update({ 'current_user': None}) cursor = conn.cursor() if response.get_arguments('storyquery') != []: query = response.get_argument('storyquery') results = search(cursor, conn, query) stories = [] for result in results: stories.append(Story.find('id', int(result[0]))[0]) context['stories'] = stories context['query'] = query context['story'] = '' assert 'query' in context print("QUERY", repr(query)) html = template.render_file('templates/storylist.html', context) # html = template.render_file('templates/minimal.html', context) response.write(html) elif response.get_arguments('sort') != []: # filter logic here pass else: context['stories'] = stories context['story'] = None html = template.render_file('templates/storylist.html', context) response.write(html)
def process_new_story(response): username = response.get_secure_cookie('username') user = User.find('username', str(username, 'utf-8'))[0] title = response.get_argument('title') story_text = response.get_argument('story') rule = response.get_argument('rule') comment = response.get_argument('comment') story = Story.create(user, title, comment) story.save() story.add_paragraph(user, story_text).save() response.redirect('/view_story/{}'.format(story.id))
def main(): # make sure we are using the same database as the rest of the code from dbapi import conn cursor = conn.cursor() searchindex.create_table(conn, True) # do the search function results = search(cursor, conn, input('Q? ')) print() for result in results: print(Story.find('id', result[0])[0].title, '-->', result[1]) conn.close()
def add_to_story(response, id): username = response.get_secure_cookie('username') user = User.find('username', str(username, 'utf-8')) if not user: raise Exception("Expected user account when adding to story") user = user[0] addition_to_story = response.get_argument('paragraph') story = Story.find('id', id)[0] if not Rules.check(addition_to_story, story.id): #reject the story print('Rejected') #return to story view without updating.... TODO: show error response.redirect('/view_story/{}'.format(id)) return added_paragraph = story.add_paragraph(user, addition_to_story) added_paragraph.save() response.redirect('/view_story/{}'.format(id))
def chain_paragraph(self, userObj, content): '''Returns a Paragraph object, that when saved, is the child of this paragraph.''' from dbapi.story import Story return Story.find('id', self.story_id)[0].add_paragraph(userObj, content, self.id)