This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
def prepro(pairs, filterout=None, hard_lmt=100000): | |
pairs['relation'] = 1 | |
G = nx.from_pandas_edgelist(pairs, 'subject', 'object', | |
create_using=nx.DiGraph()) | |
if filterout: | |
nodes = \ | |
list(set(pairs[~pairs.subject_type.isin(filterout)]['subject'].tolist() | |
+ pairs[~pairs.object_type.isin(filterout)]['object'].tolist())) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import networkx as nx | |
import matplotlib.pyplot as plt | |
def draw_kg(pairs): | |
k_graph = nx.from_pandas_edgelist(pairs, 'subject', 'object', | |
create_using=nx.MultiDiGraph()) | |
node_deg = nx.degree(k_graph) | |
layout = nx.spring_layout(k_graph, k=0.15, iterations=20) | |
plt.figure(num=None, figsize=(120, 90), dpi=80) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import re | |
import spacy | |
import neuralcoref | |
nlp = spacy.load('en_core_web_lg') | |
neuralcoref.add_to_pipe(nlp) | |
def get_entity_pairs(text, coref=True): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from collections import Counter | |
import tensorflow as tf | |
from tffm import TFFMRegressor | |
from sklearn.metrics import mean_squared_error | |
from sklearn.model_selection import train_test_split | |
import numpy as np | |
# Loading datasets' |