Exemplo n.º 1
0
 def remove_verse(cls, location: str):
     verse_record: Verse = VerseRead.get_verse_record(location)
     if verse_record is None:
         raise DebugException(503)
     else:
         # Remove all links and the verse
         link_records = VerseRead.get_link_record(verse_record,
                                                  jsonify=False)
         for link_record in link_records:
             Conn.session.delete(link_record)
         Conn.commit()
         Conn.session.delete(verse_record)
         Conn.commit()
         return VerseRead.jsonify_verse_record(verse_record)
Exemplo n.º 2
0
 def update_verse_location(location: str, new_location: str):
     # Check if verse exists
     verse_record: Verse = VerseRead.get_verse_record(location)
     # Check if new verse already exists
     new_verse_record: Verse = VerseRead.get_verse_record(new_location)
     # Check if location of the verse is updated
     if verse_record is not None and new_verse_record is None:
         # Update verse location if a new location is provided
         verse_record.location = new_location
         Conn.commit()
     elif new_verse_record is not None:
         raise DebugException(502)
     else:
         raise DebugException(503)
     return VerseRead.get_verse_record(new_location, jsonify=True)
Exemplo n.º 3
0
    def vote(cls, response_item, positive: bool):

        # Get Relevant Verse
        verse_record = VerseRead.get_verse_record(response_item["location"])

        # Get Matched Words
        matched_words = [
            breakdown_item["matched_word"]
            for breakdown_item in response_item["breakdown"]
        ]

        # Increase vote for matched keywords
        if positive:
            for keyword_link in verse_record.keyword_links:
                # Add a vote if the keyword is matched
                if keyword_link.keyword.word in matched_words:
                    keyword_link.votes += 1
                    keyword_link.keyword.total_votes += 1
                    print(keyword_link.keyword.word, "now has votes",
                          keyword_link.votes)
        else:
            for keyword_link in verse_record.keyword_links:
                # Add a vote if the keyword is matched
                if keyword_link.keyword.word in matched_words and keyword_link.votes > 1:
                    keyword_link.votes -= 1
                    keyword_link.keyword.total_votes -= 1
                    print(keyword_link.keyword.word, "now has votes",
                          keyword_link.votes)

        verse_record.update_max_vote()
        VerseConnection.session.commit()

        return "Done"
Exemplo n.º 4
0
    def reset_votes(cls):
        verses = VerseRead.get_verse_record()
        for verse_record in verses:
            verse_record.max_vote = 1
            for keyword_link in verse_record.keyword_links:
                keyword_link.votes = 1
        VerseConnection.session.commit()

        return "Completed"
Exemplo n.º 5
0
 def parse_verses(cls, verse_records_list=None) -> [Verse]:
     parsed_verses_list = list()
     all_verses = VerseRead.get_verse_record()
     verse_num = len(all_verses)
     if verse_records_list is None:
         verse_records_list = all_verses
     for verse in verse_records_list:
         parsed_verses_list.append(cls.parse_verse(verse, verse_num))
     return parsed_verses_list
Exemplo n.º 6
0
 def update_all_verses_text(version: str = "niv"):
     all_verses = VerseRead.get_verse_record()
     for verse in all_verses:
         try:
             verse.text = BibleQueryConnection.get_text_dict_with_parsing(
                 version, verse.location)
         except Exception as e:
             print(e)
             print(verse.location)
             break
     Conn.commit()
Exemplo n.º 7
0
 def british_to_american():
     keyword_records = VerseRead.get_keyword_record()
     all_keywords = [
         keyword_record.word for keyword_record in keyword_records
     ]
     for keyword_record in keyword_records:
         if BritishAmericanConnection.british_to_american(
                 keyword_record.word) in all_keywords:
             print(
                 keyword_record.word,
                 BritishAmericanConnection.british_to_american(
                     keyword_record.word))
     print("done")
Exemplo n.º 8
0
 def update_frequency_info():
     all_verses = VerseRead.get_verse_record()
     all_keywords = VerseRead.get_keyword_record()
     all_keywords_dict = dict([(keyword_record.word, {
         "number_of_appearance": 0,
         "total_votes": 0
     }) for keyword_record in all_keywords])
     for verse_index, verse_record in enumerate(all_verses):
         DebugTools.logging.print_debug("Frequency Update Progress:",
                                        (verse_index + 1), "/",
                                        len(all_verses))
         verse_record.update_max_vote()
         for keyword_link in verse_record.keyword_links:
             all_keywords_dict[
                 keyword_link.keyword.word]["number_of_appearance"] += 1
             all_keywords_dict[keyword_link.keyword.
                               word]["total_votes"] += keyword_link.votes
     for keyword_record in all_keywords:
         keyword_record.appearance = all_keywords_dict[
             keyword_record.word]["number_of_appearance"]
         keyword_record.total_votes = all_keywords_dict[
             keyword_record.word]["total_votes"]
     Conn.commit()
Exemplo n.º 9
0
    def add_verse(location: str,
                  lvl1: [str] = [],
                  lvl2: [str] = [],
                  lvl3: [str] = []):
        # Check if verse already exists
        verse_record: Verse = VerseRead.get_verse_record(location)
        if verse_record is None:
            # Add the verse record if the verse does not exist yet and retrieve added record
            Conn.session.add(Conn.verse_table(location))
            Conn.commit()
            verse_record = VerseRead.get_verse_record(location)
        else:
            # If verse already exists, raise an error
            raise DebugException(502)

        # Combine keywords into one list
        keyword_list = [
            (word, 3)
            for word in VerseWrite._clean_list(lvl3, convert_to_american=True)
        ]
        keyword_list.extend([
            (word, 2)
            for word in VerseWrite._clean_list(lvl2, convert_to_american=True)
        ])
        keyword_list.extend([
            (word, 1)
            for word in VerseWrite._clean_list(lvl1, convert_to_american=True)
        ])

        # Update keyword links
        for keyword, level in keyword_list:
            # Check if keyword already exists in DB
            keyword_record = VerseRead.get_keyword_record(keyword)
            # Add keyword if it doesn't exist
            if keyword_record is None:
                VerseWrite.add_keyword(keyword)
                keyword_record = VerseRead.get_keyword_record(keyword)

            # Check if link already exists in DB
            link_record = VerseRead.get_link_record(verse_record,
                                                    keyword_record)

            if link_record is None:
                # Add link if it doesn't exist
                Conn.session.add(
                    Conn.link_table(verse_record, keyword_record, level))
                Conn.commit()
            else:
                # Update link if it exists
                link_record.level = level
                Conn.commit()
        return VerseRead.get_verse_record(location, jsonify=True)
Exemplo n.º 10
0
    def edit_link(location: str,
                  word: str,
                  level: int,
                  delete: bool = False,
                  jsonify=False):
        # Clean words only if it is not deleting
        if not delete:
            word = VerseWrite._clean(word, convert_to_american=True)

        # Verify weight level
        if (not isinstance(level, int)) or level > 3 or level < 1:
            print(isinstance(level, int))
            raise DebugException(504)

        # Check if link already exists
        verse_record: Verse = VerseRead.get_verse_record(location)
        keyword_record: Keyword = VerseRead.get_keyword_record(word)
        if keyword_record is None:
            # If keyword does not exist, add it to db
            VerseWrite.add_keyword(word)
            keyword_record = VerseRead.get_keyword_record(word)
        link_record: VerseKeywordLink = VerseRead.get_link_record(
            verse_record, keyword_record)

        if link_record is not None:
            if delete:
                # Update link if it already exists
                link_record.keyword.appearance -= 1
                link_record.keyword.total_votes -= link_record.votes
                link_record.verse.update_max_vote()
                Conn.session.delete(link_record)
                Conn.commit()
                return VerseRead.jsonify_link_record(link_record)
            else:
                # Update link if it already exists
                link_record.level = level
                Conn.commit()
        else:
            if delete:
                # Trying to delete non-existent link
                raise DebugException(503)
            else:
                # Add link if it doesnt exist
                verse_record.keyword_links.append(
                    Conn.link_table(verse_record, keyword_record, level))
                Conn.commit()

        return VerseRead.get_link_record(verse_record, keyword_record, jsonify)
Exemplo n.º 11
0
 def search_verse(cls, query_string):
     verse_json = VerseRead.search_verse(query_string=query_string,
                                         jsonify=True)
     return verse_json