Esempio n. 1
0
def test_cli(query_arguments=testqueries):
    """run several complex queries and print their results to stdout"""
    for arg in query_arguments:
        book_list = Books(Results(Query(arg)))
        print "{0}:\n\n".format(arg)
        for book in book_list.books:
            print book.title, book.year
Esempio n. 2
0
def gen_textplans(query):
    """
    debug function: generates all text plans for a query.
    
    :type query: ``int`` or ``list`` of ``str``
    :param query: can be the index of a test query (e.g. 4) OR a list of 
    query parameters (e.g. ["-k", "phonology", "-l", "German"])
    
    :rtype: ``TextPlans``
    :return: a ``TextPlans`` instance, containing a number of text plans
    """
    textplans = []
    if type(query) is int:
        books = Books(Results(Query(testqueries[query])))
        return TextPlans(AllMessages(AllPropositions(AllFacts(books))))
    if type(query) is list:
        books = Books(Results(Query(query)))
        return TextPlans(AllMessages(AllPropositions(AllFacts(books))))
Esempio n. 3
0
def genallmessages(query):
    """
    debug function: generates all messages plans for a query.
    
    :type query: ``int`` or ``list`` of ``str``
    :param query: can be the index of a test query (e.g. 4) OR a list of 
    query parameters (e.g. ["-k", "phonology", "-l", "German"])
    
    :rtype: ``AllMessages``
    :return: all messages that could be generated for the query
    """
    if isinstance(query, int):
        books = Books(Results(Query(testqueries[query])))
        return AllMessages(AllPropositions(AllFacts(books)))
    elif isinstance(query, list):
        books = Books(Results(Query(query)))
        return AllMessages(AllPropositions(AllFacts(books)))
    elif isinstance(query, Query):
        books = Books(Results(query))
        return AllMessages(AllPropositions(AllFacts(books)))
Esempio n. 4
0
def genprops(querynumber=10):
    """    
    generates all propositions for all books in the database concerning a 
    specific query.
    
    :type querynumber: ``int``
    :param querynumber: the index of a query from the predefined list of 
    test queries (named 'testqueries')
    
    :rtype: ``AllPropositions``
    """
    books = Books(Results(Query(testqueries[querynumber])))
    return AllPropositions(AllFacts(books))
Esempio n. 5
0
def genmessages(booknumber=0, querynumber=10):
    """
    generates all messages for a book regarding a specific database query.
    
    :type booknumber: ``int``
    :param booknumber: the index of the book from the results list ("0" 
    would be the first book with the highest score)
    
    :type querynumber: ``int``
    :param querynumber: the index of a query from the predefined list of 
    test queries (named 'testqueries')
    
    :rtype: ``list`` of ``Message``s
    """
    books = Books(Results(Query(testqueries[querynumber])))
    am = AllMessages(AllPropositions(AllFacts(books)))

    for message in am.books[booknumber].messages.values():
        message.freeze()
        #freeze messages, so Rule()s can be tested against them
    return am.books[booknumber].messages.values()
Esempio n. 6
0
def dataimport():
    print('importing book data....', end='')
    bookdata = extract_book_data2()
    for data in bookdata:
        response = requests.get(
            'https://openlibrary.org/api/books?bibkeys=ISBN:{}&jscmd=data&format=json'
            .format(data['isbn']))
        json_data = json.loads(response.text)
        header = 'ISBN:{}'.format(data['isbn'])
        try:
            subjects = json_data[header]['subjects']
            genres = []
            reject = [
                'nyt', 'New Your Time', 'Library', 'library', 'Accessible',
                'Large', 'type', 'Print', 'print', 'Internet', 'Archive',
                'Wishlist', 'strips'
            ]
            fiction = ['Troll', 'Fiction', 'Batman', 'Spy', 'Spies']
            thriller = [
                'Homicide', 'Abusive', 'abusive', 'Thriller', 'Psychology',
                'Horror', 'Detectives', 'detectives', 'criminal', 'Criminal',
                'Secret', 'Violence'
            ]
            comic = ['comic', 'Graphic']
            sen = ['Horror comic books', 'Japan', 'Comic', 'attack', 'Fate']
            literature = [
                'English', 'century', 'Literature', 'Britain', 'British'
                'Revolution', 'Translation', 'translation', 'relation',
                'Historical', 'historical', 'academic'
            ]
            war = ['War', 'World', 'Ship']
            dra = [
                'Relations', 'Romance', 'Family', 'friendship', 'Female',
                'Friendship', 'Revenge', 'Teenage', 'drama', 'Uncle', 'uncle',
                'Grand', 'student', 'School', 'school'
            ]
            fant = [
                'Ambition', 'Dragon', 'Fantasy', 'Dream', 'Heroes',
                'Imaginary', 'creature', 'Titan', 'Sailor', 'fantasy'
            ]
            life = [
                'Life', 'parent', 'Parent', 'Lifestyle', 'life', 'Experience'
            ]
            comedy = ['funny', 'Jokes', 'Dream', 'Circus']
            scifi = [
                'Time', 'Science', 'Chemistry', 'Space', 'technology', 'space',
                'explore'
            ]
            supernatur = ['Supernatural', 'Vampires', 'Evil', 'Spirituality']
            superpow = ['Royalty', 'tale', 'God']
            for subject in subjects:
                if any(word in subject['name'] for word in reject):
                    continue
                elif any(word in subject['name'] for word in fiction):
                    genres.append('Fiction')
                elif any(word in subject['name'] for word in thriller):
                    genres.append('Thriller')
                elif any(word in subject['name'] for word in comic):
                    genres.append('Shounen')
                elif any(word in subject['name'] for word in sen):
                    genres.append('Seinen')
                elif any(word in subject['name'] for word in literature):
                    genres.append('Literature')
                elif any(word in subject['name'] for word in war):
                    genres.append('Military')
                elif any(word in subject['name'] for word in dra):
                    genres.append('Drama')
                elif any(word in subject['name'] for word in fant):
                    genres.append('Fantasy')
                elif any(word in subject['name'] for word in comedy):
                    genres.append('Comedy')
                elif any(word in subject['name'] for word in life):
                    genres.append('Slice of Life')
                elif any(word in subject['name'] for word in scifi):
                    genres.append('Sci-Fi')
                elif any(word in subject['name'] for word in supernatur):
                    genres.append('Supernatural')
                elif any(word in subject['name'] for word in superpow):
                    genres.append('Super Power')
                elif subject['name'] not in genres and not subject[
                        'name'].isdigit() and subject['name'].isalpha():
                    genres.append(subject['name'])
            genres = ', '.join(genres)
        except:
            genres = 'Undefined'
        if not genres or genres == ' ' or genres == 'Undefined':
            genres = []
            fiction_list = [
                'King', 'Lord', 'Harry', 'Star', 'Kill', 'Letter', 'Games',
                'Animal', 'Hobbit', 'Game', 'Great', 'Forgot', 'Dreams', 'Maze'
            ]
            if any(word in data['title'] for word in fiction_list):
                genres.append('Fiction')
            super_list = ['Super', 'power', 'Power', 'Mighty', 'mighty']
            if any(word in data['title'] for word in super_list):
                genres.append('Super Power')
            fantasylist = [
                'Dragon', 'Sun', 'Magic', 'Adventure', 'adventure', 'journey',
                'Journey', 'Tale', 'tale', 'Legend', 'legend', 'giant',
                'Giant', 'Dream'
            ]
            if any(word in data['title'] for word in fantasylist):
                genres.append('Fantasy')
            thrillerlist = [
                'Missing', 'Murderer', 'Grey', 'kill', 'Power', 'power',
                'Death', 'death'
            ]
            if any(word in data['title'] for word in thrillerlist):
                genres.append('Thriller')
            machinelist = [
                'Machine', 'machine', 'robot', 'Robot', 'iron', 'Iron',
                'metal', 'Metal'
            ]
            if any(word in data['title'] for word in machinelist):
                genres.append('Mecha')
            scifilist = ['Time', 'time']
            if any(word in data['title'] for word in scifilist):
                genres.append('Sci-FI')
            dramalist = [
                'Secret', 'boy', 'Girl', 'Moon', 'girl', 'moon', 'Family',
                'family', 'home', 'Love', 'Lovely'
            ]
            if any(word in data['title'] for word in dramalist):
                genres.append('Drama')
            litlist = ['Women', 'men', 'Men', 'City', 'Art']
            if any(word in data['title'] for word in litlist):
                genres.append('Literature')
            supernat_list = ['Spirit', 'spirit']
            if any(word in data['title'] for word in supernat_list):
                genres.append('Super Natural')
            if genres:
                genres = ', '.join(genres)
        if not genres:
            genres = 'Undefined'

        data['genre'] = genres
        try:
            data['isbn'] = float(data['isbn'])
            print(data['title'], data['genre'])
            Books(data['title'], data['authors'], data['rating'], data['isbn'],
                  data['isbn13'], data['original_publication_year'],
                  data['image_url'], data['small_image_url'], data['genre'],
                  data['revenue']).save()
        except Exception as e:
            print(e)

            continue
    print('done!')

    print('importing movie data....', end='')
    moviedata = extract_movie_data()
    for data in moviedata:
        try:
            Movies(data['Title'], data['Genre'], data['Description'],
                   data['Director'], data['Actors'], data['Year'],
                   data['Runtime (Minutes)'], data['Rating'],
                   data['Revenue (Millions)'], data['Metascore']).save()
        except:
            continue
    print('done!')

    print('importing anime data....', end='')
    animedata = extract_anime_data()
    count = 0
    for data in animedata:
        if count == 9000:
            break
        try:
            print(data)
            Animes(data['name'], data['genre'], data['type'], data['episodes'],
                   data['rating'], data['start_date'], data['end_date'],
                   data['revenue']).save()
            count += 1
        except Exception as e:
            print(e)
            continue
    print('done!')
Esempio n. 7
0
def generate_textplans(query):
    """generates all text plans for a database query"""
    books = Books(Results(query))
    return TextPlans(AllMessages(AllPropositions(AllFacts(books))))