import requests
import re


def extract_conceptnet(phrase):
    """Access ConceptNet API and read relational triples as well as their weight and simple example"""
    url_head = 'http://api.conceptnet.io/c/en/'  # access ConceptNet API
    raw_json = requests.get(url_head + phrase).json()
    edges = raw_json['edges']
    if not edges:  # if edges is empty, which means ConceptNet doesn't contain such concept or node
        return None
    concepts = []
    for edge in edges:
        triple = re.findall(r'/a/\[/r/(.*?)/.*?,/c/en/(.*?)/.*?,/c/en/(.*?)/.*?\]', edge['@id'])[0]  # ERE triple
        surface_text = re.sub(r'[\[\]]', '', '' if edge['surfaceText'] is None else edge['surfaceText'])  # example
        weight = edge['weight']  # weight
        concepts.append({'Triple': triple, 'weight': weight, 'example': surface_text})
    return concepts