241 lines
6.5 KiB
Text
241 lines
6.5 KiB
Text
{
|
|
"cells": [
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Construct interaction graphs\n",
|
|
"\n",
|
|
"Constructing interaction graphs and counting the neighbour interactions for each user."
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Import the dataset and create user nodes dictionary."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import pandas as pd\n",
|
|
"\n",
|
|
"\n",
|
|
"case = '...'\n",
|
|
"casegraphs = '...'\n",
|
|
"\n",
|
|
"df_data = pd.read_csv(f'../sentiment-analysis/{case}.csv')\n",
|
|
"user_nodes = df_data.groupby('user')['id'].apply(list).to_dict()\n",
|
|
"\n",
|
|
"df_data.info()"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Construct and export nearest-neighbour graph and count nearest-neighbours in nearest-neighbour graph."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import networkx as nx\n",
|
|
"\n",
|
|
"# Define undirected graph\n",
|
|
"G = nx.Graph()\n",
|
|
"\n",
|
|
"# Construct nearest-neighbour graph\n",
|
|
"for _, row in df_data.iterrows():\n",
|
|
" G.add_node(row['id'], user=row['user'], sentiment=row['s'], text=row['text'])\n",
|
|
" if row['parentid'] != 'no_parent':\n",
|
|
" G.add_edge(row['id'], row['parentid'])\n",
|
|
"\n",
|
|
"# Export nearest-neighbour graph\n",
|
|
"nx.write_graphml(G, f'{casegraphs}-nn-graph.graphml')\n",
|
|
"\n",
|
|
"# Define nearest-neighbour interaction count dictionary\n",
|
|
"nn_count = {}\n",
|
|
"\n",
|
|
"# Count nearest neighbours for each user\n",
|
|
"for node in G.nodes(data=True):\n",
|
|
" node_id, attributes = node\n",
|
|
" user = attributes['user']\n",
|
|
"\n",
|
|
" count = len(set(G.neighbors(node_id)) - set(user_nodes[user]))\n",
|
|
"\n",
|
|
" if user in nn_count:\n",
|
|
" nn_count[user] += count\n",
|
|
" else:\n",
|
|
" nn_count[user] = count"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Construct and export transitive-closure graph and count nearest-neighbours in transitive-closure graph."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Construct transitive-closure graph\n",
|
|
"tc_G = nx.transitive_closure(G)\n",
|
|
"\n",
|
|
"# Export transitive-closure graph\n",
|
|
"nx.write_graphml(tc_G, f'{casegraphs}-tc-graph.graphml')\n",
|
|
"\n",
|
|
"# Define transitive-closure interaction count dictionary\n",
|
|
"tc_count = {}\n",
|
|
"\n",
|
|
"# Count neighbours for each user\n",
|
|
"for node in tc_G.nodes(data=True):\n",
|
|
" node_id, attributes = node\n",
|
|
" user = attributes['user']\n",
|
|
"\n",
|
|
" count = len(set(tc_G.neighbors(node_id)) - set(user_nodes[user]))\n",
|
|
"\n",
|
|
" if user in tc_count:\n",
|
|
" tc_count[user] += count\n",
|
|
" else:\n",
|
|
" tc_count[user] = count"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Construct and export directed-transitive-closure graph and count nearest-neighbours in directed-transitive-closure graph."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Define directed graph\n",
|
|
"dG = nx.DiGraph()\n",
|
|
"\n",
|
|
"# Construct nearest-neighbour directed graph\n",
|
|
"for _, row in df_data.iterrows():\n",
|
|
" dG.add_node(row['id'], user=row['user'], sentiment=row['s'], text=row['text'])\n",
|
|
" if row['parentid'] != 'no_parent':\n",
|
|
" dG.add_edge(row['id'], row['parentid'])\n",
|
|
"\n",
|
|
"nx.write_graphml(dG, f'{casegraphs}-nn-dgraph.graphml')\n",
|
|
"\n",
|
|
"# Construct directed-transitive-closure directed graph\n",
|
|
"dtc_dG = nx.transitive_closure(dG)\n",
|
|
"\n",
|
|
"# Export directed-transitive-closure directed graph\n",
|
|
"nx.write_graphml(dtc_dG, f'{casegraphs}-dtc-dgraph.graphml')\n",
|
|
"\n",
|
|
"# Convert to directed-transitive-closure undirected graph\n",
|
|
"dtc_G = dtc_dG.to_undirected()\n",
|
|
"\n",
|
|
"# Export directed-transitive-closure graph\n",
|
|
"nx.write_graphml(dtc_G, f'{casegraphs}-dtc-graph.graphml')\n",
|
|
"\n",
|
|
"# Define directed-transitive-closure interaction count dictionary\n",
|
|
"dtc_count = {}\n",
|
|
"\n",
|
|
"# Count neighbours for each user\n",
|
|
"for node in dtc_G.nodes(data=True):\n",
|
|
" node_id, attributes = node\n",
|
|
" user = attributes['user']\n",
|
|
"\n",
|
|
" count = len(set(dtc_G.neighbors(node_id)) - set(user_nodes[user]))\n",
|
|
"\n",
|
|
" if user in dtc_count:\n",
|
|
" dtc_count[user] += count\n",
|
|
" else:\n",
|
|
" dtc_count[user] = count"
|
|
]
|
|
},
|
|
{
|
|
"attachments": {},
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Construct and export user interaction count dataset."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"user_c = df_data.groupby('user')['id'].count().to_dict()\n",
|
|
"user_s = df_data.groupby('user')['s'].mean().to_dict()\n",
|
|
"user_stds = df_data.groupby('user')['s'].std().to_dict()\n",
|
|
"\n",
|
|
"df_users = pd.DataFrame({\n",
|
|
" 'nn-count': pd.Series(nn_count), \n",
|
|
" 'dtc-count': pd.Series(dtc_count), \n",
|
|
" 'tc-count': pd.Series(tc_count),\n",
|
|
" 'mean-sentiment': pd.Series(user_s),\n",
|
|
" 'std-sentiment': pd.Series(user_stds),\n",
|
|
" 'post-count': pd.Series(user_c)\n",
|
|
"})\n",
|
|
"\n",
|
|
"export_dataset = f'{case}-users.csv'\n",
|
|
"df_users.to_csv(export_dataset, index=False)\n",
|
|
"\n",
|
|
"df_users.info()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"df_users.describe()"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.14.6"
|
|
},
|
|
"vscode": {
|
|
"interpreter": {
|
|
"hash": "8c75c0fdd1a718867cdcb84b32adcfdbeaad00b3a4e00a59385211aeed084d4c"
|
|
}
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|