<commit_before>import requests
from flask import Blueprint, jsonify, current_app, abort, request

network = Blueprint("network", __name__)


def get_studies():
    url = current_app.config["COPYRIGHT_EVIDENCE_API_URL"] + "/studies"
    try:
        response = requests.get(url)
        studies = response.json()["studies"]
        return assign_study_ids(studies)
    except requests.RequestException:
        abort(500)


def assign_study_ids(studies):
    for i, study in enumerate(studies):
        study["id"] = i
    return studies


def gen_nodes(studies, prop, equality, matches):
    return [gen_node(s, studies, prop, equality, matches) for s in studies]


def gen_node(study, studies, prop, equality, matches):
    return {
        "id": study["id"],
        "name": study["name"] if "name" in study else "",
        "title": get_title(study, prop),
        "edges": gen_edges(study, studies, prop, equality, matches),
        "color": "#4582ec"
    }


def get_title(study, prop):
    title = "<strong>Title: </strong>" + study["title"]
    if prop in study:
        title += "<br>"
        title += "<strong>" + prop + ":</strong>"
        title += "<br>"
        val = study[prop]
        if isinstance(val, list):
            for p in study[prop]:
                title += p + "<br>"
        else:
            title += val
    return title


def get_nodes(prop, equality, matches, filter_no_edges=True):
    studies = get_studies()
    nodes = gen_nodes(studies, prop, equality, matches)
    if filter_no_edges:
        return list(filter(lambda n: len(n["edges"]) > 0, nodes))
    return nodes


def gen_edges(study, studies, prop, equality, matches):
    if prop not in study:
        return []
    edges = []
    for other_study in studies:
        if study == other_study:
            continue
        if prop not in other_study:
            continue
        x = study[prop]
        y = other_study[prop]
        if equality:
            if x == y:
                edges.append({
                    "from": study["id"],
                    "to": other_study["id"]
                })<commit_msg>Do not allow intersection of non list types.<commit_after>import requests
from flask import Blueprint, jsonify, current_app, abort, request

network = Blueprint("network", __name__)


def get_studies():
    url = current_app.config["COPYRIGHT_EVIDENCE_API_URL"] + "/studies"
    try:
        response = requests.get(url)
        studies = response.json()["studies"]
        return assign_study_ids(studies)
    except requests.RequestException:
        abort(500)


def assign_study_ids(studies):
    for i, study in enumerate(studies):
        study["id"] = i
    return studies


def gen_nodes(studies, prop, equality, matches):
    return [gen_node(s, studies, prop, equality, matches) for s in studies]


def gen_node(study, studies, prop, equality, matches):
    return {
        "id": study["id"],
        "name": study["name"] if "name" in study else "",
        "title": get_title(study, prop),
        "edges": gen_edges(study, studies, prop, equality, matches),
        "color": "#4582ec"
    }


def get_title(study, prop):
    title = "<strong>Title: </strong>" + study["title"]
    if prop in study:
        title += "<br>"
        title += "<strong>" + prop + ":</strong>"
        title += "<br>"
        val = study[prop]
        if isinstance(val, list):
            for p in study[prop]:
                title += p + "<br>"
        else:
            title += val
    return title


def get_nodes(prop, equality, matches, filter_no_edges=True):
    studies = get_studies()
    nodes = gen_nodes(studies, prop, equality, matches)
    if filter_no_edges:
        return list(filter(lambda n: len(n["edges"]) > 0, nodes))
    return nodes


def gen_edges(study, studies, prop, equality, matches):
    if prop not in study:
        return []
    edges = []
    for other_study in studies:
        if study == other_study:
            continue
        if prop not in other_study:
            continue
        x = study[prop]
        y = other_study[prop]
        if equality or not isinstance(x, list):
            if x == y:
                edges.append({
                    "from": study["id"],
                    "to": other_study["id"]
                })
{'fraction_non_alphanumeric': 0.12511671335200747, 'fraction_numerical': 0.003734827264239029, 'mean_word_length': 2.3294483294483292, 'pattern_counts': {'":': 16, '<': 17, '<?xml version=': 0, '>': 19, '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}}
{'dir': 'git-commits-cleaned', 'id': '2940774', 'n_tokens_mistral': 1301, 'n_tokens_neox': 1242, 'n_words': 371}