Skip to content
Snippets Groups Projects
Commit 4f49f136 authored by Liam Byrne's avatar Liam Byrne
Browse files

started graph embedding module

parent d1693114
No related branches found
No related tags found
No related merge requests found
No preview for this file type
import sqlite3
import pandas as pd
class DBHandler:
def __init__(self, file_name: str):
self._db = sqlite3.connect(file_name)
def fetch_questions_by_user_id(self, user_id: int):
questions = pd.read_sql_query(f"""
SELECT *
FROM Post
WHERE Tags LIKE '%python%' AND (PostTypeId = 1) AND ((LastEditorUserId = {user_id}) OR (OwnerUserId = {user_id}))
""", self._db)
questions.set_index('PostId', inplace=True)
def fetch_answers_by_user_id(self, user_id: int):
answers = pd.read_sql_query(f"""
SELECT A.Tags, B.*
FROM Post A
INNER JOIN Post B ON (B.ParentId = A.PostId) AND (B.ParentId IS NOT NULL)
WHERE A.Tags LIKE '%python%' AND (B.PostTypeId = 2) AND ((B.LastEditorUserId = {user_id}) OR (B.OwnerUserId = {user_id}))
""", self._db)
# Only use the first of the two 'Tags' columns
answers = answers.loc[:, ~answers.columns.duplicated()].copy()
answers.set_index('PostId', inplace=True)
return answers
def fetch_comments_on_questions_by_user_id(self, user_id: int):
comments_on_questions = pd.read_sql_query(f"""
SELECT A.Tags, B.*
FROM Post A
INNER JOIN Comment B ON (B.PostId = A.PostId)
WHERE A.Tags LIKE '%python%' AND (B.UserId = {user_id}) AND (A.PostTypeId = 1)
""", self._db)
comments_on_questions.set_index('CommentId', inplace=True)
return comments_on_questions
def fetch_comments_on_answers_by_user_id(self, user_id: int):
comments_on_answers = pd.read_sql_query(f"""
SELECT A.Tags, C.*
FROM Post A
INNER JOIN Post B ON (B.ParentId = A.PostId) AND (B.ParentId IS NOT NULL)
INNER JOIN Comment C ON (B.PostId = C.PostId)
WHERE A.Tags LIKE '%python%' AND (C.UserId = {user_id}) AND (B.PostTypeId = 2)
""", self._db)
comments_on_answers.set_index('CommentId', inplace=True)
return comments_on_answers
This diff is collapsed.
from abc import ABC
import networkx as nx
import pandas as pd
from graph4nlp.pytorch.data import GraphData
from graph4nlp.pytorch.modules.graph_construction import DependencyBasedGraphConstruction
from graph4nlp.pytorch.modules.graph_construction.base import StaticGraphConstructionBase
from matplotlib import pyplot as plt
class StaticUserGraphConstruction:
"""Class for StackOverflow user activity graph construction"""
def __init__(self):
super(StaticUserGraphConstruction, self).__init__()
def static_topology(cls, questions: pd.DataFrame, answers: pd.DataFrame, comments: pd.DataFrame) -> GraphData:
cls._construct_static_graph()
@classmethod
def _construct_static_graph(cls, questions: pd.DataFrame, answers: pd.DataFrame, comments: pd.DataFrame):
user_graph = GraphData()
next_node = 0
color_map = []
node_features = []
tag_dict = {} # tag name: node id
module_dict = {}
edges_src = []
edges_dest = []
@classmethod
def display_graph(cls, g: GraphData, color_map=None) -> None:
plt.figure(figsize=(40, 40))
dgl_ug = g.to_dgl()
nx_ug_graph = dgl_ug.to_networkx()
pos_ug = nx.spring_layout(nx_ug_graph) # , k=0.15, iterations=20)
if color_map is not None:
nx.draw(nx_ug_graph, pos_ug, with_labels=True, node_color=color_map)
else:
nx.draw(nx_ug_graph, pos_ug, with_labels=True, node_color=[[.7, .7, .7]])
if __name__ == '__main__':
t = DependencyBasedGraphConstruction(None)
t(None)
#graph_topology = StaticUserGraphConstruction()
#graph_topology(GraphData())
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment