def do_last_page(self): display = Verse() self.verse = int(self.verse) - 10 zpage = Page(self.statement) verse = zpage.page_down() # if you did not receive a verse.... if len(verse) == 0: # construct a count self.statement to retrieve the total number of verses Total_Pages = zpage.count_chapter_verses(self.book, self.chapter) Total_Chapters = zpage.count_books_chapters(self.book) # if your trying to get a chapter that is not within the book, then # switch to the next book if int(self.chapter) >= Total_Chapters: BookTitle = SierraDAO.GetBookId(self.book) - 1 BookTitle = zpage.retrieve_title(BookTitle) self.book = BookTitle self.chapter = 1 self.verse = 0 verse = Page(self.statement) verse = verse.page_down() # if the verse we are currently at is equal to or greater than the # page count "number of verses in our book" then turn the chapter elif self.verse >= Total_Pages: self.chapter = int(self.chapter) - 1 self.verse = 0 verse = Page(self.statement) verse = verse.page_up()
def parse_file(file): try: with open(file, 'r') as fp: verse_list = [] new_verse = True text = '' author = '' for line in fp: if new_verse: words = line.split() for word in words: split = re.search("[a-zA-Z]*", word).group() if split != '': author = split new_verse = False elif line in ['\n', '\r\n']: verse_list.append(Verse(text=text, author=author)) new_verse = True text = '' else: text += line return verse_list except IOError: print("Error while loading the file") return None
def process_edit_request(r, message): """ Processes an edit request if found in check_messages(). Simply reconstructs an updated VerseBot comments based on the user's revised verse quotations, and replaces the outdated comment with the updated one. Additionally, the bot sends the user a message letting them know that the edit has been completed successfully. NOTE: Users can only edit a VerseBot comment if they authored the parent comment of VerseBot's reply. """ try: comment_url = message.body[1:message.body.find('}')] comment = r.get_submission(comment_url) except: message.reply( 'An error occurred while processing your request. Please make sure that you do not modify the subject line of your message to VerseBot.' ) comment = False if message.author == comment.author and comment: verses_to_find = regex.find_bracketed_text(message.body) lookup_list = list() if len(verses_to_find) != 0: for ver in verses_to_find: next_ver = regex.find_verses(ver) lookup_list.append(str(next_ver)) if len(lookup_list) != 0: for reply in comment.comments[0].replies: if str(reply.author) == get_bot_username(): try: print( str(comment.author) + ' has requested a comment edit...') link = reply.permalink[24:comment.permalink. find('/', 24)] verse_object = Verse(lookup_list, message, comment_url, link) edited_comment = '*^This ^comment ^has ^been ^edited ^by ^' + str( comment.author ) + '.* \n\n' + verse_object.get_comment() remove_invalid_statistics(reply.body, link) reply.edit(edited_comment) database.update_db_stats(verse_object) verse_object.clear_verses() message.mark_as_read() message.reply( '[Your triggered VerseBot response](' + comment_url + ') has been successfully edited to reflect your updated quotations.' ) break except: print( 'Comment edit failed. Will try again later...') verse_object.clear_verses() lookup_list[:] = [] else: message.mark_as_read() else: message.mark_as_read()
def parse_lines_AccentedTischendorf(self, lines): mystring = "\n".join(lines) words = mystring.split() self.end_monad += 1 index = -1 break_kind = "." for w in words: index += 1 if w[0] in "0123456789": myarr = w.split(":") mychapter = int(myarr[0]) try: myverse = int(myarr[1]) except: print "UP100: w = '%s' words[index:+30] = '%s'" % ( w, words[index:index + 30]) chapter_end = self.end_monad - 1 if self.chapter != mychapter: if self.chapter <> 0: self.parseChapter(self.chapter, chapter_end) self.chapter = mychapter self.verse = myverse verse = Verse([], self.bookname, self.booknumber) verse.chapter = mychapter verse.verse = myverse verse.first_monad = self.end_monad verse.last_monad = self.end_monad self.verses.append(verse) elif w == "{P}": break_kind = "P" elif w == "{C}": break_kind = "C" elif w == "-": pass # Remove silently. FIXME: Do something more intelligent! else: wd = word.Word(self.end_monad, variant_none) wd.surface = w if "&" in wd.surface: [wd.surface, wd.qere] = wd.surface.split("&") else: wd.qere = wd.surface wd.qere_noaccents = word.RemoveAccents( word.BETAtoOLBtranslate(wd.qere)) #wd.parsing = "CONJ" #wd.Strongs1 = 9999 wd.makeSurfacesAccentedTischendorf() wd.break_kind = break_kind break_kind = "." #print wd.surface, wd.accented_surface self.verses[-1].words.append(wd) self.verses[-1].last_monad = self.end_monad self.end_monad += 1 self.parseChapter(self.chapter, chapter_end)
def respond_to_username_mention(self, msg): """ Responds to a username mention. This could either contain one or more valid Bible verse quotation requests, or it could simply be a username mention without any valid Bible verses. If there are valid Bible verses, VerseBot generates a response that contains the text from these quotations. Otherwise, the message is forwarded to the VerseBot admin for review. :param msg: The message that contains the username mention """ verses = find_verses(msg.body) if verses is not None: response = Response(msg, self.parser) for verse in verses: book_name = books.get_book(verse[0]) if book_name is not None: v = Verse( book_name, # Book verse[1], # Chapter verse[3], # Translation msg.author, # User msg.subreddit.display_name, # Subreddit verse[2]) # Verse if not response.is_duplicate_verse(v): response.add_verse(v) if len(response.verse_list) != 0: message_response = response.construct_message() if message_response is not None: self.log.info("Replying to %s with verse quotations..." % msg.author) try: msg.reply(message_response) database.update_db_stats(response.verse_list) database.increment_comment_count() except praw.errors.Forbidden: # This message is unreachable. pass except praw.errors.APIException as err: if err.error_type in ("TOO_OLD", "DELETED_LINK", "DELETED_COMMENT"): self.log.warning("An error occurred while replying" " with error_type %s." % err.error_type) else: self.log.info("No verses found in this message. " "Forwarding to /u/%s..." % VERSEBOT_ADMIN) try: self.r.send_message( VERSEBOT_ADMIN, "Forwarded VerseBot Message", "%s\n\n[[Link to Original Message](%s)]" % (msg.body, msg.permalink)) except requests.ConnectionError: pass
def parseVerse(self, verse_lines, first_monad, is_last_verse_of_book, read_what): verse = Verse(verse_lines, self.bookname, self.booknumber) self.verses.append(verse) chapter_end = self.end_monad self.end_monad = verse.parse(first_monad, read_what) chapter = verse.chapter if is_last_verse_of_book: chapter_end = self.end_monad self.parseChapter(self.chapter, chapter_end) elif self.chapter <> chapter: if self.chapter <> 0: self.parseChapter(self.chapter, chapter_end) self.chapter = chapter
def parseDollarLine(self, line): myline = line[3:] mylist = myline.split(":") myindex = mylist[0].rfind(" ") mybook = mylist[0][0:myindex] mychapter = mylist[0][myindex+1:] myverse = mylist[1] #print "'%s' chapter '%s' verse '%s'" % (mybook,mychapter,myverse) bookname = impnames[mybook] if self.curBook != mybook: self.curBook = mybook newbook = Book("./" + bookname + ".TBA") print "UP2: newbook.bookname = %s, newbook.booknumber = %d" % (newbook.bookname, newbook.booknumber) self.books.append(newbook) book = self.books[-1] self.verse = Verse([], book.bookname, book.booknumber) self.verse.chapter = int(mychapter) self.verse.verse = int(myverse)
def process_linear_verse(self, mychapterverse): myarr = mychapterverse[0:mychapterverse.find(".")].split(":") mychapter = int(myarr[0]) myverse = int(myarr[1]) chapter_end = self.end_monad self.end_monad += 1 if self.chapter != mychapter: if self.chapter <> 0: self.parseChapter(self.chapter, chapter_end) self.chapter = mychapter if self.verse != myverse: self.verse = myverse verse = Verse([], self.bookname, self.booknumber) verse.chapter = mychapter verse.verse = myverse verse.first_monad = self.end_monad verse.last_monad = self.end_monad self.verses.append(verse)
def do_next_page(self): display = Verse() self.verse = int(self.verse) + 10 zpage = Page(self.statement) verse = zpage.page_up() # if you did not receive a verse.... if len(verse) == 0: # construct a count self.statement to retrieve the total number of verses # see about replacing these with properties Total_Pages = zpage.count_chapter_verses(self.book, self.chapter) Total_Chapters = zpage.count_books_chapters(self.book) # if your trying to get a chapter that is not within the book. then switch to the next book if int(self.chapter) >= Total_Chapters: BookTitle = SierraDAO.GetBookId(self.book) + 1 BookTitle = zpage.retrieve_title(BookTitle) self.book = BookTitle self.chapter = 1 self.verse = 0 verse = Page(self.statement) verse = verse.page_up() # if the verse we are currently at is equal to or greater than # the page count "number of verses in our book" then turn the chapter elif self.verse >= Total_Pages: self.chapter = int(self.chapter) + 1 self.verse = 0 verse = Page(self.statement) verse = verse.page_up() for n in verse: zformat = display.wrap(n[0]) display.show(zformat)
NEXUS ----- https://github.com/soft9000/TheBibleProjects """ import sqlite3 import argparse from verse import Verse from sierra_dao import SierraDAO import mark_dao from menu import do_menu from Pagination import Page, PageOps display = Verse() def say_done(): """ What we see whenever a menu is done. """ print("(exit)") def do_book_cv(): """ Show a book:chapter:verse """ cvn = input("Enter chap:book#:vers# = ").strip() zset = SierraDAO.ParseClassicVerse(cvn) if zset == False: display.show(None) else: dao = SierraDAO.GetDAO()
def respond_to_edit_request(self, msg): """ Responds to an edit request. The bot will parse the body of the message, looking for verse quotations. These will replace the quotations that were placed in the original response to the user. Once the comment has been successfully edited, the bot then sends a message to the user letting them know that their verse quotations have been updated. :param msg: The message that contains the edit request """ try: comment_url = re.search("\{(.*?)\}", msg.body).group(1) comment = self.r.get_submission(comment_url) except: try: msg.reply("An error occurred while processing your edit " "request. Please make sure that you do not modify " "the subject line of your message to %s." % REDDIT_USERNAME) except requests.ConnectionError: pass return if msg.author == comment.author and comment: verses = find_verses(msg.body) if verses is not None: for reply in comment.comments[0].replies: if str(reply.author) == REDDIT_USERNAME: try: self.log.info( "%s has requested a comment edit..." % comment.author) sub = re.search("/r/(.*?)/", comment_url).group(1) response = Response(msg, self.parser, comment_url) for verse in verses: book_name = books.get_book(verse[0]) if book_name is not None: v = Verse( book_name, # Book verse[1], # Chapter verse[3], # Translation msg.author, # User sub, # Subreddit verse[2]) # Verse if not response.is_duplicate_verse(v): response.add_verse(v) if len(response.verse_list) != 0: msg_response = ("*^This ^comment ^has ^been " "^edited ^by ^%s.*\n\n" % msg.author) msg_response += response.construct_message() if msg_response is not None: self.log.info( "Editing %s's comment with " "updated verse quotations..." % msg.author) database.remove_invalid_stats( reply.body, sub) reply.edit(msg_response) database.update_db_stats( response.verse_list) try: msg.reply( "[Your triggered %s " "response](%s) has been " "successfully edited to " "reflect your updated " "quotations." % (REDDIT_USERNAME, comment_url)) except requests.ConnectionError: pass break except: self.log.warning("Comment edit failed. " "Will try again later...")
import os from verse import Verse verses = [] with open('newTestament') as f: for line in f: line = line[0:-2] #takes the ~ off each line line = line.split("|") verses.append(Verse(line[0], line[1], line[2], line[3])) # for i in range(0,len(verses) - 1): # print(verses[i].toString()) search = input("Enter word or phrase to search: ").lower() results = [] for entry in verses: if search in entry.text.lower(): results.append(entry) for i in range(len(results)): print("[" + str(i + 1) + "]", end=" ") print(results[i].toString()) selection = int(input("Make choice to view chapter: ")) selVerse = results[selection - 1] chapter = [] for entry in verses: if selVerse.book == entry.book and selVerse.chapter == entry.chapter: chapter.append(entry)
def main(): """ The main program for VerseBot. Starts a loop that infinitely retrieves, scans, processes, and replies to comments. """ print('Starting up VerseBot...') # Connects to reddit via PRAW. try: r = praw.Reddit(user_agent=( 'VerseBot by /u/mgrieger. Github: https://github.com/matthieugrieger/versebot' )) r.login(config.get_bot_username(), config.get_bot_password()) print('Connected to reddit!') except: print( 'Connection to reddit failed. Either reddit is down at the moment or something in the config is incorrect.' ) exit() print('Connecting to database...') database.connect() print('Cleaning database...') database.clean_comment_id_database() print('Retrieving supported translations...') find_supported_translations() lookup_list = list() comment_ids_this_session = set() #check_message_timer = datetime.datetime.utcnow() print('Beginning to scan comments...') while True: comments = praw.helpers.comment_stream(r, 'all', limit=None) for comment in comments: # This if statement allows for messages to be checked every 2 minutes (or so). Ideally this would be # done with another timed thread, but Reddit objects in PRAW (which check_messages() takes as # an argument) are not thread-safe. #if (datetime.datetime.utcnow() - check_message_timer).seconds >= 120: # print('Checking messages...') # check_messages(r) # check_message_timer = datetime.datetime.utcnow() if comment.author != config.get_bot_username( ) and not database.check_comment_id( comment.id) and comment.id not in comment_ids_this_session: comment_ids_this_session.add(comment.id) verses_to_find = regex.find_bracketed_text(comment.body) if len(verses_to_find) != 0: for ver in verses_to_find: next_ver = regex.find_verses(ver) lookup_list.append(str(next_ver)) if len(lookup_list) != 0: verse_object = Verse(lookup_list, comment) next_comment = verse_object.get_comment() if next_comment != False: try: comment.reply(next_comment) except requests.exceptions.HTTPError, err: # If true, this means the bot is banned. if str(err) == '403 Client Error: Forbidden': print( 'Banned from subreddit. Cannot reply.') next_comment = False except praw.errors.APIException, err: if err.error_type in ('TOO_OLD', 'DELETED_LINK', 'DELETED_COMMENT'): next_comment = False else: raise else: next_comment = False if next_comment != False: print('Inserting new comment id to database...') database.add_comment_id(comment.id) print('Updating statistics...') database.update_db_stats(verse_object) lookup_list[:] = [] else: try: comment_ids_this_session.remove(comment.id) except KeyError: pass lookup_list[:] = [] verse_object.clear_verses()
def add_book(book, translation): chapters = [] verses = [] chapters.extend(range(1, (bible_books[book] + 1))) for chapter in chapters: url = 'https://www.biblegateway.com/passage/?search=' + book + '+' + str( chapter) + '&version=' + translation html = requests.get(url) soup = bs4.BeautifulSoup(html.text, 'lxml') # We don't want section titles, but in the HTML they are labeled similarly as the main text, so we get rid of them first section_headers = soup.find_all('h3') for section in section_headers: section.decompose() verse = 1 missed_verse = False while True: verse_lookup = 'text ' + book_abbreviations[book] + '-' + str( chapter) + '-' + str(verse) selection = soup.find_all(class_=verse_lookup) # Checks for more than one missed verse in a row before breaking to next chapter. This covers vss. that are only in the footnotes. if not selection: if missed_verse: break missed_verse = True verse += 1 continue # Some verses use multiple "Matt-1-1" tags, so verse_text needs to be defined as a blank string outside the upcoming loop verse_text = '' for item in selection: for span in item('span'): # These two if statements keep .decompose() from deleting red letter text or the divine name LORD if 'class="woj' not in str(span): if 'class="small-caps' not in str(span): span.decompose() for sup in item('sup'): sup.decompose() # Here we ensure all HTML tags attached to one verse get added together as part of one verse. verse_text += item.getText() + ' ' verses.append( Verse(book, chapter, verse, translation, verse_text[:-1], 0)) verse += 1 return verses
def respond_to_edit_request(self, message): """ Responds to an edit request. The bot will parse the body of the message, looking for verse quotations. These will replace the quotations that were placed in the original response to the user. Once the comment has been successfully edited, the bot then sends a message to the user letting them know that their verse quotations have been updated. """ try: comment_url = message.body[1:message.body.find("}")] comment = self.r.get_submission(comment_url) except: try: message.reply( "An error occurred while processing your edit request. " "Please make sure that you do not modify the subject line of your message to %s." % REDDIT_USERNAME) except requests.exceptions.ConnectionError: pass return if message.author == comment.author and comment: verses = find_verses(message.body) if verses is not None: for reply in comment.comments[0].replies: if str(reply.author) == REDDIT_USERNAME: try: self.log.info( "%s has requested a comment edit..." % comment.author) link = reply.permalink[24:comment.permalink. find("/", 24)] response = Response(message, self.parser, comment_url) for verse in verses: book_name = books.get_book(verse[0]) if book_name is not None: v = Verse( book_name, # Book verse[1], # Chapter verse[3], # Translation message.author, # User link, # Subreddit verse[2]) # Verse if not response.is_duplicate_verse(v): response.add_verse(v) if len(response.verse_list) != 0: message_response = ( "*^This ^comment ^has ^been ^edited ^by ^%s.*\n\n" % message.author) message_response += response.construct_message( ) if message_response is not None: self.log.info( "Editing %s's comment with updated verse quotations..." % message.author) database.remove_invalid_statistics( reply.body, link) reply.edit(message_response) database.update_db_stats( response.verse_list) try: message.reply( "[Your triggered %s response](%s) has been successfully edited to reflect" " your updated quotations." % (REDDIT_USERNAME, comment_url)) except requests.exceptions.ConnectionError: pass break except: raise self.log.warning( "Comment edit failed. Will try again later...") break
def first_sentence(self, number): return Verse().first_sentence(number)
def second_sentence(self, number): return Verse().second_sentence(number)
if __name__ == "__main__": from verse import Verse from sierra_dao import SierraDAO import mark_dao from kjv.py import Turn_Page assert SierraDAO.ParseClassicVerse("Genesis:1:1") assert SierraDAO.ParseClassicVerse("gEnesis:1:1") assert SierraDAO.ParseClassicVerse("Gene:1:1") assert SierraDAO.ParseClassicVerse("gEnE:1:1") assert SierraDAO.ParseClassicVerse("Gene:0:1") == False assert SierraDAO.ParseClassicVerse("Gene:1:0") == False dao = SierraDAO.GetDAO() books = list(dao.list_books()) assert "Genesis" in books assert dao.get_sierra_book("gEnE", 1, 2) == 2 v = Verse() test = dao.search("verse LIKE '%PERFECT%'") assert len(list(test)) == 124 assert dao.get_sierra(123)["sierra"] == "123" assert dao.get_sierra(123)["sierra"] == "123" zdict = dao.list_book_table() zbooks = list(zdict) assert len(zbooks) == 81 print("Testing Success!") bmk = mark_dao.BookMark(15, 99) mark_dao.BookMarks.Sync(bmk) print("Testing Success!") # TODO: Test the BookMarks class signature - Full C.R.U.D ops.
def third_sentence(self, number): return Verse().third_sentence(number)
# -*- coding: utf-8 -*- # @Author: LogicJake # @Date: 2019-03-19 10:13:05 # @Last Modified time: 2019-03-19 10:14:17 from verse import Verse if __name__ == '__main__': epoches = 10 dimension = 16 node_num = 2110 similarity_file = 'citeseer_CN.csv' verse = Verse(node_num, dimension) verse.embedding(similarity_file, epoches, log=True) verse.save_embedding('embedding.csv')
def last_sentence(self, number): return Verse().last_sentence(number)