def __init__(self): self.root = ArticleNode(0) self.Facebook = ArticleNode(-1) self.Reddit = ArticleNode(-2) self.Twitter = ArticleNode(-3) self.nodeDictionary = { -3: self.Twitter, -2: self.Reddit, -1: self.Facebook, 0: self.root } self.keywordDictionary = {"Twitter": -3, "Reddit": -2, "Facebook": -1} # Initialize root node and its relationships ( this needs to be automated based on keywords) self.root.name = "Root" self.Facebook.name = "Facebook" self.Reddit.name = "Reddit" self.Twitter.name = "Twitter" self.root.references = [ self.Facebook.articleID, self.Reddit.articleID, self.Twitter.articleID ] self.root.successors = [self.Facebook, self.Reddit, self.Twitter] self.Facebook.predecessors = self.root self.Reddit.predecessors = self.root self.Twitter.predecessors = self.root # Tree Variables self.numberOfSubtrees = len(self.root.successors)
def createArticleNode(self, aid, title): if self.articleList.get(aid): article = self.articleList.get(aid) else: article = ArticleNode(aid, title) article.name = title return article
def addSubcategoryNodes(self): for subcategory in self.subcategories: self.nodeDictionary[subcategory] = ArticleNode( -self.numberOfSubtrees, subcategory) self.nodeDictionary[subcategory].predecessors = self.root self.root.successors.append(self.nodeDictionary[subcategory]) self.numberOfSubtrees += 1
def addCategoryNodes(self): for category in self.categories: self.nodeDictionary[category] = ArticleNode( aid=-self.numberOfSubtrees, name=category) self.nodeDictionary[category].predecessors = self.root self.root.successors.append(self.nodeDictionary[category]) self.numberOfSubtrees += 1
def getCitedBy(self, id): try: connection = psycopg2.connect(user="******", password="******", host="localhost", port="5434", database="stephen") cursor = connection.cursor() postgreSQL_select_Query = "SELECT * FROM public.cited_by WHERE article_id = " postgreSQL_select_Query = postgreSQL_select_Query + str(id) # print (postgreSQL_select_Query) cursor.execute(postgreSQL_select_Query) # use fetchall() to get all articles, use fetchmany(x) to get x number of articles" # artTemp = cursor.fetchall() artTemp = cursor.fetchall() retCites = [] newNode = ArticleNode() for row in artTemp: print("here") newNode = self.getInfoFromId(row[1]) retCites.append(newNode) print("CITED BY =", retCites) return retCites except (Exception, psycopg2.Error) as error: print("Error while fetching data from PostgreSQL", error) return None
def generateDummyNodes(self): nodeList = [] for i in range(1, (self.numberOfNodes + 1)): newNode = ArticleNode(i, "a") nodeList.append(newNode) self.assignDummyKeyWords(newNode) self.assignDummyReferences(newNode) return nodeList
def updateCitedBy(self, aid, title, citedByID): self.aDict = self.addArticleToDictionary(citedByID, title, False) #if aid == 17326: testNode = ArticleNode(0,"") testNode = self.aDict[aid] self.aDict[aid].citedBy.append(int(citedByID)) return self.aDict
def generateKeywordGraph(self, searchResult, kw): art = ArticleNode() dummyThiccGraph = {'nodes': [], 'edges': []} nodeList = [] edgelist = [] xie = 0 yie = 0 #print("graphGenerator: " + str(searchResult)) for x in searchResult: xie += 1 yie += 1 randColor = "#{:06x}".format(random.randint(0, 0xFFFFFF)) testNode = node(str(x), str(x), 2, random.randint(-100, 100), random.randint(-100, 100), randColor) nodeList.append(testNode) for z in searchResult[x]: if (x == "Undefined"): articleNode = node(str(z), str(z), 1, random.randint(-100, 100), random.randint(-100, 100), randColor) nodeList.append(articleNode) articleEdge = edge(str(z), str(x)) edgelist.append(articleEdge) print("Category: " + str(x) + " article: " + str(z)) else: label = "Title: " + str( z.getTitle()) + " ArticleID: " + str( z.getInfo()) + " SubCategory: " + str( z.getSubCat()) articleNode = node(str(z.getInfo()), label, 1, random.randint(-100, 100), random.randint(-100, 100), randColor) nodeList.append(articleNode) articleEdge = edge(str(z.getInfo()), str(x)) edgelist.append(articleEdge) print("Category: " + str(x) + " article: " + str(z.getInfo())) randColor0 = "#{:06x}".format(random.randint(0, 0xFFFFFF)) nodObj = node(kw, kw, 3, 0, 0, randColor0) nodeList.insert(0, nodObj) for x in searchResult: testEdge = edge(str(x), kw) edgelist.append(testEdge) dummyThiccGraph['nodes'] = [node.__dict__ for node in nodeList] dummyThiccGraph['edges'] = [edge.__dict__ for edge in edgelist] output = dummyThiccGraph print(dummyThiccGraph['nodes']) with open(os.path.join('static', 'testjson.json'), 'w') as outfile: outfile.write(json.dumps(output, indent=4)) return output
def __init__(self, nodeList): self.root = ArticleNode(0, "Root") self.nodeDictionary = {0: self.root} self.authorDictionary = {} self.numberOfSubtrees = 0 self.categoryDict, self.categories, self.subcategories = self.createCategoryDict( ) self.keywords = self.categories + self.subcategories self.nodeList = nodeList self.addCategoryNodes() self.addSubcategoryNodes() self.initializeNodeDictionary() self.assignSubTrees() self.establishNodeRelationships()
def getIdsFromAuthor(author): try: connection = psycopg2.connect(user="******", password="******", host="localhost", port="5434", database="stephen") cursor = connection.cursor() postgreSQL_select_Query = "SELECT * FROM public.sma WHERE abstract ILIKE '%social media%'" #print (postgreSQL_select_Query) cursor.execute(postgreSQL_select_Query) #use fetchall() to get all articles, use fetchmany(x) to get x number of articles" artTemp = cursor.fetchall() authorList = [] for row in artTemp: postgreSQL_select_Query = "SELECT * FROM public.article_authors WHERE author_name LIKE " postgreSQL_select_Query = postgreSQL_select_Query + "'%" + author + "%' AND article_id = " + str( row[0]) #print (postgreSQL_select_Query) cursor.execute(postgreSQL_select_Query) authors = cursor.fetchall() newNode = ArticleNode() for a in authors: newNode.author = a[1] newNode.articleId = a[0] newNode.abstract = row[3] newNode.title = row[6] newNode.journal = row[7] #print(newNode.author) authorList.append(newNode) except (Exception, psycopg2.Error) as error: print("Error while fetching data from PostgreSQL", error) return authorList
def initializeNodeDictionary(self): #Create Category and subCategory Nodes for node in self.nodeList: self.nodeDictionary[node.articleID] = node #Authors Initialized in self.authorDictionary node.authorList = node.authorList.lower() authors = node.authorList # no need to check if author already exists or not because dict[a] is created if not already present. for author in authors: self.nodeDictionary[author].append(node) for keyword in node.keywordList: if (keyword not in self.keywords): self.numberOfSubtrees = self.numberOfSubtrees + 1 self.keywords.append(keyword) self.nodeDictionary[keyword] = ArticleNode( -self.numberOfSubtrees, keyword) self.nodeDictionary[keyword].predecessors = self.root self.root.successors.append(self.nodeDictionary[keyword])
def getIdsFromKeyword(kw): try: connection = psycopg2.connect(user="******", password="******", host="localhost", port="5434", database="stephen") cursor = connection.cursor() postgreSQL_select_Query = "SELECT * FROM public.sma WHERE abstract ILIKE '%social media%' AND abstract ILIKE " postgreSQL_select_Query = postgreSQL_select_Query + "'%" + kw + "%'" print(postgreSQL_select_Query) cursor.execute(postgreSQL_select_Query) #use fetchall() to get all articles, use fetchmany(x) to get x number of articles" #artTemp = cursor.fetchall() artTemp = cursor.fetchall() keywordList = [] i = 0 for row in artTemp: print() print(row[0]) print(row[3]) #print(len(artTemp)) newNode = ArticleNode() newNode.articleId = row[0] newNode.abstract = row[3] newNode.title = row[6] newNode.journal = row[7] print("art id added: ", newNode.articleId) #print("Journal added: ", newNode.journal) i += 1 keywordList.append(newNode) except (Exception, psycopg2.Error) as error: print("Error while fetching data from PostgreSQL", error) print("total count: ", i) return keywordList
def findPhrases(root, text, aList): global articleArray node = root foundPhrases = [] # Traverses file without saving items to memory aid = -1 # article ID starts at 1 with open(text, 'r') as file: for line in file: aid += 1 print "article: " + str(aid) for word in line.split(): flag = False childIndex = -1 for child in node.children: childIndex += 1 if word == child.phrase: flag = True break if flag: node = node.children[childIndex] if node.word_finished: foundPhrases.append(node.phraseID) #create an article node # add articleID to list if it doesn't already exist articleFound = False articleIndex = 0 while articleIndex < len(aList): if aid == aList[articleIndex].articleID: articleFound = True break articleIndex += 1 if articleFound == False: article = ArticleNode(aid) aList.append(article) # add keyword phrase to current article # find article articleIndex = 0 keyFound = False keyIndex = 0 while keyIndex < len(article.keywords): if node.phraseID == article.keywords[keyIndex]: keyFound = True break keyIndex += 1 #print "keyword loop" if keyFound == False: article.keywords.append(node.phraseID) #reset node iterator to root node = root continue # i += 1 else: if node == root: continue # i += 1 else: node = root
def addArticleToDictionary(self, aid, title): if aid not in self.aDict: newArticle = ArticleNode(aid, title) self.aDict[aid] = newArticle return self.aDict
def addArticleToDictionary(self, aid, title, contains_social_media): if aid not in self.aDict: newArticle = ArticleNode(int(aid), title) newArticle.contains_social_media = contains_social_media self.aDict[aid] = newArticle return self.aDict
def addArticle(articleID, aList): # add articleID to list if it doesn't already exist if articleID not in aList: article = ArticleNode(articleID)
def generateDummyNodes(n): nodeList = [] for i in range(1, n + 1): newNode = ArticleNode(i) nodeList.append(newNode) return nodeList