def home():
    return render_template('home.html', languages=get_sorted_languages())
Beispiel #2
0
import itertools
import os
import time
import urllib2, urllib
import json
import re
from flask import Flask
from flask import redirect
from flask import render_template
from flask import request
from flask import send_from_directory
from flask import url_for
from conceptnet5.nodes import make_concept_uri
from conceptnet5.web_interface.utils import data_url, uri2name, get_sorted_languages

LANGUAGES = get_sorted_languages()

########################
# Set this flag to True when developing, False otherwise! -JVen
#
DEVELOPMENT = False
#
########################

app = Flask(__name__)

if DEVELOPMENT:
  site = 'http://new-caledonia.media.mit.edu:8080'
  web_root = ''
else:
  site = 'http://conceptnet5.media.mit.edu'
Beispiel #3
0
def home():
    return render_template('home.html', languages=get_sorted_languages())
Beispiel #4
0
def get_data(uri):
    uri = '/%s' % uri
    node = conceptnet.get_node(uri)
    # If node doesn't exist, say so.
    if node is None:
        return render_template('not_found.html', uri=uri)
    # Node exists, show stuff.
    incoming_edges = conceptnet.get_incoming_edges(node, result_limit=100)

    assertions = []
    frames = []
    normalizations = []

    count = 0
    edge_generator = itertools.chain(
        conceptnet.get_incoming_edges(uri, 'relation', result_limit=100),
        conceptnet.get_incoming_edges(uri, 'arg', result_limit=100)
    )

    timer = None
    for edge, assertion_uri in edge_generator:
        count += 1

        # Get 10 concepts, then however many concepts you can
        # retrieve in 1/5 more second, or at most 200.
        if count >= 10 and timer is None:
            timer = time.time()
        if (timer is not None and time.time() - timer > 0.2):
            break

        # Determine the relation and arguments from the URI.
        relargs = conceptnet.get_rel_and_args(assertion_uri)
        relation = relargs[0]
        args = relargs[1:]

        # skip n-ary relations for now; I'm tired and we didn't implement them
        # successfully anyway. -- Rob
        if len(args) != 2:
            continue

        linked_args = ['<a href="%s">%s</a>' % (data_url(arg), uri2name(arg))
                       for arg in args]
        readable_args = [uri2name(arg) for arg in args]
        if relation.startswith('/frame'):
            rendered_frame = uri2name(relation).replace('{%}', '').replace(
                '{1}', linked_args[0]).replace('{2}', linked_args[1])
            frames.append(rendered_frame)
        else:
            position = edge.get('position')
            thisNode = node['uri']
            thisNodeName = '...'
            if edge['type'] == 'relation':
                thisNode = args[0]
                otherNode = args[1]
                thisNodeName = readable_args[0]
                otherNodeName = readable_args[1]
            elif position == 1:
                otherNode = args[1]
                otherNodeName = readable_args[1]
            elif position == 2:
                otherNode = args[0]
                otherNodeName = readable_args[0]
            else:
                raise ValueError
            assertions.append({
                'assertion_uri':assertion_uri,
                'position': position,
                'relation_url': data_url(relation),
                'relation': uri2name(relation),
                'relkey': "%s/%s" % (relation, position),
                'source_uri': thisNode,
                'source_url': data_url(thisNode),
                'source_text': thisNodeName,
                'target_uri': otherNode,
                'target_url': data_url(otherNode),
                'target_text': otherNodeName,
                'score': edge['score']
            })

    normalized = None

    for edge, norm_uri in conceptnet.get_outgoing_edges(uri, 'normalized'):
        name = uri2name(norm_uri)
        # cheap trick to correct for erroneous normalized edges:
        # make sure an appropriate 4 letters are the same
        for word in name.split():
            for offset in xrange(0, 5):
                if name[:4] == node['name'][offset:offset+4]:
                    normalized = {
                        'uri': norm_uri,
                        'url': data_url(norm_uri),
                        'name': uri2name(norm_uri)
                    }
                    break
    sense_list = list(conceptnet.get_incoming_edges(uri, 'senseOf',
    result_limit=100)) + list(conceptnet.get_outgoing_edges(uri, 'senseOf',
    result_limit=100))
    senses = []
    for edge, sense_uri in sense_list:
        name = uri2name(sense_uri)
        senses.append({
            'uri': sense_uri,
            'url': data_url(sense_uri),
            'name': sense_uri.split('/')[-1].replace('_', ' ')
        })

    return render_template('data.html', node=node, assertions=assertions,
            frames=frames, normalized=normalized, senses=senses, languages=get_sorted_languages())