concept_net.py 2.64 KB
Newer Older
1
2
3
import requests
import threading
from anytree import Node
4
import sys
5
6
7
8
9
10

class ConceptNet:

    url = 'http://api.conceptnet.io'
    limit = 5

11
    def find_related(self, feature, rel):
12
13
14
15
16
        uri = '/query?node=/c/en/{feature}&other=/c/en&rel=/r/{rel}&limit={limit}'.format(feature=feature, rel=rel, limit=self.limit)
        obj = requests.get(self.url + uri).json()
        unique = set([obj['edges'][i]['end']['label'] for i in range(len(obj['edges']))])
        return unique

17
18
19
20
21
    def find_relations(self, f1, f2):
        uri = '/query?node=/c/en/{f1}&other=/c/en/{f2}'.format(f1=f1, f2=f2)
        obj = requests.get(self.url + uri).json()
        return obj

22
23
24
25
26
    def get_relatedness(self, f1, f2):
        uri = '/relatedness?node1=/c/en/{f1}&node2=/c/en/{f2}'.format(f1=f1.replace(' ','_'), f2=f2.replace(' ','_'))
        obj = requests.get(self.url + uri).json()
        return obj['value']

27
    def append_result(self, feature, rel, result_set, lock):
28
        rels = self.find_related(feature, rel)
29
        lock.acquire()
30
        result_set.update(rels)
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
        lock.release()

    def parent_check(self, node, parent, synonyms):
        if parent == None:
            return
        min_r = 0.1
        ratio = 1.2 # relatedness for child has to be at least twice as high as for parent
        rm = set()
        for s in synonyms:
            r_child = self.get_relatedness(node.name, s)
            r_parent = self.get_relatedness(parent.name, s)
            if (r_child < min_r) or (r_parent < min_r) or (r_parent != 0 and r_child / r_parent < ratio):
                rm.add(s)
        synonyms.difference_update(rm)
        self.parent_check(node, parent.parent, synonyms)

47
    def sem_synonyms_for_node(self, node):
48
        rels = ['DefinedAs', 'Synonym', 'IsA', 'RelatedTo']  # SimilarTo? FormOf?
49
50
51
52
53
54

        synonyms = set()
        lock = threading.Lock()

        threads = []
        for rel in rels:
55
            t = threading.Thread(target=self.append_result, args=(node.name, rel, synonyms, lock))
56
57
58
59
60
61
62
            t.start()
            threads.append(t)
        for t in threads:
            t.join()

        self.parent_check(node, node.parent, synonyms)

63
        synonyms.add(node.name)
64
65
        return synonyms

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    def sub_features_for_node(self, node):
        rels = ['UsedFor', 'HasA', 'CapableOf', 'Causes', 'HasSubevent', 'HasProperty', 'MadeOf']

        features = set()
        lock = threading.Lock()

        threads = []
        for rel in rels:
            t = threading.Thread(target=self.append_result, args=(node.name, rel, features, lock))
            t.start()
            threads.append(t)
        for t in threads:
            t.join()

        return features