<commit_before>from django.http import JsonResponse
from data_2015_fall.models import *
import Queue
import simplejson

# ===================================================
#	Classes 
# ===================================================
'''
	A class represnet a paper with following fields 
	title: title of the paper[string]
	authors: array of authors[string]
	relevance: how relevant of this paper in terms of search quiteria[int] 
'''
class Paper(object):
    title = ""
    authors = []
    relevance = 0
    def __init__(self, title, authors, relevance):
        self.title = title
        self.authors = authors
        self.relevance = relevance

    def __hash__(self):
        return hash(self.title)

    def __cmp__(self, other): # for priority queue
        return self.relevance > other.relevance

    def toDict(self):
        return {
            "title": self.title,
            "authors": [a.toDict() for a in self.authors]
        }

# ===================================================
#	Functions 
# ===================================================
def getTopKRelevantPapersWithAuthorsByKeywords(request, keywords, k):
    print "call getTopKRelevantPapersWithAuthorsByKeywords()"
    keywordsList = keywords.lower().split("+")
    
    # calculate its relevance cnt and use priority queue to maintain status 
    pQ = Queue.PriorityQueue()
    for article in Article.nodes:
        lowerTitle = article.title.lower()
        matchCnt = 0
        for keyword in keywordsList:
            if keyword in lowerTitle:
    	       matchCnt += 1
    	if (matchCnt > 0):
            pQ.put(Paper(article.title, article.authors, matchCnt))
        if (pQ.qsize() > int(k) + 5): # remove some item if Queue is too large, 5 for margin because qsize is not precise 
            pQ.get()    

    # use list to store the result 
    papers = []
    while not pQ.empty():
        article = pQ.get()
        papers.append(article)
    papers.reverse()
    papers = papers[0:int(k)]
    ''' debug part 
    for p in papers:
        print p.title + " >> " + str(p.relevance) + ":  " +  simplejson.dumps(p.toDict())
    '''
    return JsonResponse({ "papers": [p.toDict() for p in papers] })   <commit_msg>Add more fields for Paper class<commit_after>from django.http import JsonResponse
from data_2015_fall.models import *
import Queue
import simplejson

# ===================================================
#	Classes 
# ===================================================
'''
	A class represnet a paper with following fields 
	title: title of the paper[string]
	authors: array of authors[string]
	relevance: how relevant of this paper in terms of search quiteria[int] 
'''
class Paper(object):
    title = ""
    authors = []
    relevance = 0
    journal = ""
    year = 1990
    volume = 1
    def __init__(self, title, authors, relevance, journal, year, volume):
        self.title = title
        self.authors = authors
        self.relevance = relevance
        self.journal = journal
        self.year = year
        self.volume = volume

    def __hash__(self):
        return hash(self.title)

    def __cmp__(self, other): # for priority queue
        return self.relevance > other.relevance

    def toDict(self):
        return {
            "title": self.title,
            "authors": [a.toDict() for a in self.authors],
            "journal": self.journal,
            "year": self.year,
            "volume": self.volume
        }

# ===================================================
#	Functions 
# ===================================================
def getTopKRelevantPapersWithAuthorsByKeywords(request, keywords, k):
    print "call getTopKRelevantPapersWithAuthorsByKeywords()"
    keywordsList = keywords.lower().split("+")
    
    # calculate its relevance cnt and use priority queue to maintain status 
    pQ = Queue.PriorityQueue()
    for article in Article.nodes:
        lowerTitle = article.title.lower()
        matchCnt = 0
        for keyword in keywordsList:
            if keyword in lowerTitle:
    	       matchCnt += 1
    	if (matchCnt > 0):
            pQ.put(Paper(article.title, article.authors, matchCnt, article.journal, article.year, article.volume))
        if (pQ.qsize() > int(k) + 5): # remove some item if Queue is too large, 5 for margin because qsize is not precise 
            pQ.get()    

    # use list to store the result 
    papers = []
    while not pQ.empty():
        article = pQ.get()
        papers.append(article)
    papers.reverse()
    papers = papers[0:int(k)]
    ''' debug part 
    for p in papers:
        print p.title + " >> " + str(p.relevance) + ":  " +  simplejson.dumps(p.toDict())
    '''
    return JsonResponse({ "papers": [p.toDict() for p in papers] })   
{'fraction_non_alphanumeric': 0.18153134202776608, 'fraction_numerical': 0.005679427850231384, 'mean_word_length': 2.782816229116945, 'pattern_counts': {'":': 11, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5385929', 'n_tokens_mistral': 1395, 'n_tokens_neox': 1299, 'n_words': 394}