def displayResults():
    user_email = request.get_cookie("email")
    global userHistory
    global docsSorted
    global firstWord
    global pagesNeeded

    # results table
    # getting the sentence entered by the user
    searchSentence = request.forms.get('search')

    # making sure not to pass in an empty string
    if (searchSentence != None):
        searchSentence = searchSentence.lower()
        occurences = countNumberOfWords(searchSentence)

        # converting to list to arrange in descending order by value
        occurencesList = sorted(occurences.items(), key=lambda t: t[1], reverse=True)
        print(occurencesList)

    # Top 20 searched words table
    s = bottle.request.environ.get('beaker.session')

    if user_email not in userHistory:  # create data for user if their email hasn't been used yet
        userHistory[user_email] = searchSentence + " "
    else:
        userHistory[user_email] += searchSentence + " "

    sentence = userHistory[user_email]
    # making sure not to pass in an empty string
    if (sentence != None):
        sentence = sentence.lower()
        newOccurences = countNumberOfWords(sentence)

        # obtaining the top twenty out of all the top occurences
        topTwenty = sorted(newOccurences.items(), key=lambda t: t[1], reverse=True)
        topTwenty = topTwenty[:20]

#passing essential details to the template to display on the front page
    picture_name = "logo_transparent.png"
    if (searchSentence != None):
        firstWord = searchSentence.lower().split()[0]

        orderedDocIds = getDocIDsDict(firstWord, word_to_wordID, wordID_to_docIDs, docID_to_pageRank)
        titlesSorted = get_sorted_titles(orderedDocIds, docID_to_title, docID_to_url)
        docsSorted = get_sorted_urls(orderedDocIds, docID_to_url)
        
        descrByQuery = getDescription(firstWord, docID_to_description)
        descriptionsSorted = get_sorted_descriptions(orderedDocIds, descrByQuery)

        #docsSorted = finder(firstWord)
        if (docsSorted == 0):
            bottle.redirect("http://localhost:8080/urlNonExistent")
        if len(docsSorted) <= 5:
            return template('loggedInResults',
                            picture=picture_name, searchSentence=searchSentence, 
                            titlesList=titlesSorted, urlsList=docsSorted, descriptionList=descriptionsSorted, 
                            user_email=user_email)
        else:
            remainder = len(docsSorted) % 5
            print('remainder is: ', remainder)
            counter = 0
            if remainder == 0:
                pagesNeeded = len(docsSorted) / 5
                print('yahan hun')
                print(pagesNeeded)
            else:
                print('ithe')
                pagesNeeded = (len(docsSorted) // 5) + 1
                print(pagesNeeded)
            newURl = "http://localhost:8080/resultsLoggedIn/1"
            bottle.redirect(newURl)

    if(docsSorted == 0):
        bottle.redirect("http://localhost:8080/urlNonExistent")
def index():
    global userSignedIn

    global titlesSorted
    global docsSorted
    global descriptionsSorted

    global firstWord
    global pagesNeeded
    occurencesList = []
    sortedTopTwentyDictionary = dict()
    searchSentence = ""

    # getting the sentence entered by the user
    searchSentence = request.forms.get('search')
    print('search sentence is: ', searchSentence)

    # making sure not to pass in an empty string
    if (searchSentence != None):
        searchSentence = searchSentence.lower()
        occurences = countNumberOfWords(searchSentence)

        # converting to list to arrange in descending order by value
        occurencesList = sorted(occurences.items(), key=lambda t: t[1], reverse=True)
        print(occurencesList)
    num = 1
    # passing essential details to the template to dsiplay on the front page
    picture_name = "logo_transparent.png"
    if (searchSentence != None):
        firstWord = searchSentence.lower().split()[0]

        orderedDocIds = getDocIDsDict(firstWord, word_to_wordID, wordID_to_docIDs, docID_to_pageRank)
        titlesSorted = get_sorted_titles(orderedDocIds, docID_to_title, docID_to_url)
        print "IN INDEX: Titles Sorted", titlesSorted
        docsSorted = get_sorted_urls(orderedDocIds, docID_to_url)
        
        descrByQuery = getDescription(firstWord, docID_to_description)
        descriptionsSorted = get_sorted_descriptions(orderedDocIds, descrByQuery)

        if (docsSorted == 0):
            bottle.redirect("http://localhost:8080/urlNonExistent")
        if len(docsSorted) <= 5:
            return template('index', occurences=occurencesList, firstWord=firstWord,
                            picture=picture_name, searchSentence=searchSentence, 
                            titlesList=titlesSorted, urlsList=docsSorted, descriptionList=descriptionsSorted)
        else:
            remainder = len(docsSorted) % 5
            print('remainder is: ', remainder)
            counter = 0
            if remainder == 0:
                pagesNeeded = len(docsSorted) / 5
                print('yahan hun')
                print(pagesNeeded)
            else:
                print('ithe')
                pagesNeeded = (len(docsSorted) // 5) + 1
                print(pagesNeeded)
            newURl = "http://localhost:8080/results/1"
            bottle.redirect(newURl)

    if(docsSorted == 0):
        bottle.redirect("http://localhost:8080/urlNonExistent")

    return template('index',
                    picture=picture_name, urlsList=docsSorted)
def index():
    global userSignedIn
    global docsSorted
    global firstWord
    global pagesNeeded
    global newsArticlesImage
    global newsArticlesPublishedAt
    global newsArticlesHeadlines
    global newsArticlesDescription

    global titlesSorted

    global descriptionsSorted
    global shouldSearchMap
    global searchMapString
    global IP_ADDRESS

    print('post /: ', shouldSearchMap)

    occurencesList = []
    sortedTopTwentyDictionary = dict()
    searchSentence = ""

    # getting the sentence entered by the user
    searchSentence = request.forms.get('search')
    print('search sentence is: ', searchSentence)

    # making sure not to pass in an empty string
    if (searchSentence != None):
        if shouldSearchMap:
            searchMapString = searchSentence
            print('Im here')
            bottle.redirect("http://" + IP_ADDRESS + "/sendRequest")
        searchSentence = searchSentence.lower()
        occurences = countNumberOfWords(searchSentence)

        # converting to list to arrange in descending order by value
        occurencesList = sorted(occurences.items(),
                                key=lambda t: t[1],
                                reverse=True)
        print(occurencesList)
    num = 1
    # passing essential details to the template to dsiplay on the front page
    picture_name = "logo_transparent.png"
    if (searchSentence != None):
        firstWord = searchSentence.lower().split()[0]

        orderedDocIds = getDocIDsDict(firstWord, word_to_wordID,
                                      wordID_to_docIDs, docID_to_pageRank)
        if len(orderedDocIds) == 0:
            bottle.redirect('http://" + IP_ADDRESS + "/urlNonExistent')
        titlesSorted = get_sorted_titles(orderedDocIds, docID_to_title,
                                         docID_to_url)
        docsSorted = get_sorted_urls(orderedDocIds, docID_to_url)

        descrByQuery = getDescription(firstWord, docID_to_description)
        descriptionsSorted = get_sorted_descriptions(orderedDocIds,
                                                     descrByQuery)

        getTweet(firstWord)
        newsArticles(firstWord)
        docsSorted = finder(firstWord)
        if (docsSorted == 0):
            bottle.redirect("http://" + IP_ADDRESS + "/urlNonExistent")
        if len(docsSorted) <= 5:
            print('lenghthidouevbibev is ', len(newsArticlesHeadlines))
            return template('index',
                            occurences=occurencesList,
                            firstWord=firstWord,
                            IP_ADDRESS=IP_ADDRESS,
                            picture=picture_name,
                            searchSentence=searchSentence,
                            urlsList=docsSorted,
                            userTweets=userTweets,
                            twitterUsers=twitterUsers,
                            newsArticlesHeadlines=newsArticlesHeadlines,
                            newsArticlesDescription=newsArticlesDescription,
                            newsArticlesPublishedAt=newsArticlesPublishedAt,
                            newsArticlesImage=newsArticlesImage,
                            descriptionList=descriptionsSorted,
                            titlesList=titlesSorted)
        else:
            remainder = len(docsSorted) % 5
            print('remainder is: ', remainder)
            counter = 0
            if remainder == 0:
                pagesNeeded = len(docsSorted) / 5
                print('yahan hun')
                print(pagesNeeded)
            else:
                print('ithe')
                pagesNeeded = (len(docsSorted) // 5) + 1
                print(pagesNeeded)
            newURl = "http://" + IP_ADDRESS + "/results/1"
            bottle.redirect(newURl)

    if (docsSorted == 0):
        bottle.redirect("http://" + IP_ADDRESS + "/urlNonExistent")

    return template('index',
                    IP_ADDRESS=IP_ADDRESS,
                    picture=picture_name,
                    urlsList=docsSorted)