<jupyter_start><jupyter_text>We start by importing all of the libaries and functions we'll need<jupyter_code># System tools
import os

# Data analysis
import pandas as pd
import itertools
from collections import Counter
from itertools import combinations 
from tqdm import tqdm

# NLP
import spacy
nlp = spacy.load("en_core_web_sm")

# Network analysis tools
import networkx as nx
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (20,20)<jupyter_output><empty_output><jupyter_text>Define input file and read data to dataframe<jupyter_code>filepath = os.path.join('..', '..', '..', 'CDS-LANG', 'tabular_examples', 'fake_or_real_news.csv')
data = pd.read_csv(filepath)<jupyter_output><empty_output><jupyter_text>Extract all named individualsWe use ```spaCy``` to extract only mentions of PERSONS or LOC in the texts.NB: See final comment below under ```Problems```!<jupyter_code>real_text = data[data.label == 'REAL']['text'] # filter one select the real news, filter two selects to only keep the text column
real_text
docs = list(nlp.pipe(real_text, batch_size = 500))
person_list = []

for doc in docs:
    in_list = []
    for token in doc.ents:
        if token.label_ == 'PERSON':
            in_list.append(token.text)
    
    person_list.append(in_list)
len(loc_list)
loc_list = []

for doc in docs:
    in_list = []
    for token in doc.ents:
        if token.label_ == 'LOC':
            in_list.append(token.text)
    
    loc_list.append(in_list)<jupyter_output><empty_output><jupyter_text>Create edgelist using ```itertools.combinations()```<jupyter_code>edgelist = []

for sublist in person_list:
    edges = list(combinations(sublist, 2))
    for edge in edges:
        if edge[0] == edge[1]:
            pass
        else:
            edgelist.append(edge)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11233480176211454, 'fraction_numerical': 0.005506607929515419, 'mean_word_length': 4.206303724928366, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '599477', 'license': 'MIT', 'n_tokens_mistral': 636, 'n_tokens_neox': 615, 'n_words': 162, 'path': 'notebooks/session6._inclass_sha.ipynb', 'repo_name': 'sarah-hvid/cds-language', 'size': '1816'}