├── .gitignore
├── README.md
├── build_index.py
├── build_knowledge.py
├── build_wordvec.py
├── crawl_wiki.py
├── create_pos_patterns.py
├── graphic
├── graph.png
├── index-0.png
├── index-1.png
├── index-2.png
└── vor.png
├── html
├── graph-index.html
├── graph-universe.html
└── sigma.min.js
├── jslib
└── knowledge.js
├── package.json
├── pos-patterns
├── pos-stopwords
├── pylib
├── jobmq
│ └── rabbit.py
├── knowledge
│ ├── __init__.py
│ ├── datasource.py
│ └── graph.py
├── spider
│ ├── __init__.py
│ ├── crawler.py
│ └── wiki.py
└── text
│ ├── __init__.py
│ ├── cleanser.py
│ ├── intent.py
│ ├── pos_tree.py
│ ├── structure.py
│ └── texthasher.py
├── repl_word2vec.py
├── requirements.txt
├── test
├── __pycache__
│ └── crawler.cpython-34.pyc
└── crawler.py
└── visualise.js
/.gitignore:
--------------------------------------------------------------------------------
1 | html/graph-data.js
2 | html/graph-index.js
3 | mine.txt
4 | models/
5 | node_modules/
6 | pylib/knowledge/__pycache__/
7 | pylib/spider/__pycache__/
8 | pylib/text/__pycache__/
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Project vör : Open Knowledge modeling
2 |
3 | ---
4 |
5 | 
6 | 
7 |
8 | ---
9 |
10 | ## Synopsis
11 |
12 | The project is initiated as a dirty hack for crawling and modeling
13 | a large volume of open knowledge out there in Wikipedia. Thus, we
14 | have a "nearly" complete graph of those knowledge, also obtain an
15 | ability to traverse the relations between knowledge topics.
16 |
17 | ---
18 |
19 | ## Infrastructure / Prerequisites
20 |
21 | To build and run the knowledge graph engine with vör,
22 | you need the following software for the infrastructure.
23 |
24 | - [x] [OrientDB](http://orientdb.com/download/)
25 | - [x] [MongoDB](https://www.mongodb.com/download-center#community)
26 |
27 | ---
28 |
29 | ## Setup
30 |
31 | Install python 3.x requirements by:
32 |
33 | ```bash
34 | $ pip3 install -r -U requirements.txt
35 | ```
36 |
37 | Install Node.js modules required by the graph visualiser.
38 | You may ignore these steps if you are not interested in
39 | visualisation.
40 |
41 | ```bash
42 | $ npm install
43 | ```
44 |
45 | Other than registered NPM packages, you also need to [install Sigma.js
46 | for visualisation](https://github.com/jacomyal/sigma.js/wiki#getting-started). The module is not bundled within this repository.
47 |
48 | ---
49 |
50 | ## 1) Download (crawl) wikipedia pages
51 |
52 | Execute:
53 |
54 | ```bash
55 | $ python3 crawl_wiki.py --verbose
56 | ```
57 |
58 | The script continuously and endlessly crawls the knowledge topic
59 | from Wikipedia starting from the seeding page. You may change
60 | the initial topic within the script to what best suits you.
61 | To stop the process, just terminate is fine. It won't leave
62 | anything at dirty stat so you can re-execute the script again
63 | at any time.
64 |
65 | >**[NOTE]** The script keeps continuously crawling
66 | and downloading the related knowledge through link traveral.
67 | **The script never ends unless you terminate it.**
68 |
69 | ---
70 |
71 | ## 2) Build the knowledge graph
72 |
73 | Execute:
74 |
75 | ```bash
76 | $ python3 build_knowledge.py --verbose --root {PASSWORD} --limit {NUM}
77 | ```
78 |
79 | Where `{PASSWORD}` represents your `root` password of **OrientDB**.
80 | And `{NUM}` represents the number of wikipedia topics to process.
81 |
82 | What the script does is simply imports the entire raw hefty text
83 | knowledge from **MongoDB** to **OrientDB** as a big graph.
84 | The output graph in OrientDB is built from the following components:
85 |
86 | - [1] **Vertices** : Represent topic / keyword
87 | - [2] **Edges** : Represent relations between topic-keyword or keyword-keyword.
88 |
89 | > **[NOTE]** The script processes the entire data in the collection
90 | all the way to the end. This will definitely take large amount of
91 | time if you have large data in your collection.
92 |
93 | ---
94 |
95 | ## 3) Visualise the knowledge graph
96 |
97 | Execute:
98 |
99 | ```bash
100 | $ node visualise {PASSWORD}
101 | ```
102 |
103 | Where `{PASSWORD}` is your OrientDB root's password. The script
104 | downloads the graph data from OrientDB, renders it with appropriate
105 | visual figure. After it's done, you can view the graphs as follows.
106 |
107 | - [1] Universe of topics graph [`html/graph-universe.html`].
108 | - [2] Index graph [`html/graph-index.html`.]
109 |
110 |
111 | ---
112 |
113 | ## 4) Build Word2Vec model over the crawled data
114 |
115 | Execute:
116 |
117 | ```bash
118 | $ python3 build_wordvec.py --limit {LIMIT} --out {PATH_TO_MODEL}
119 | ```
120 |
121 | There should be sufficient amount of the downloaded wikipedia
122 | in MongoDB which is done by running `crawl_wiki.py`. The output
123 | is a binary file.
124 |
125 | ---
126 |
127 | ## 5) Create topic index
128 |
129 | Execute:
130 |
131 | ```bash
132 | $ python3 build_index.py --limit {LIMIT} --root {PASSWORD}
133 | ```
134 |
135 | The script generates another OrientDB collection `vorindex`
136 | which contains all invert-index of the topics and their
137 | corresponding keywords. Weights of the edges are calculated
138 | by how frequent the word appear in each of the topics.
139 |
140 | 
141 | 
142 |
143 | ---
144 |
145 | ## Licence
146 |
147 | The project is licenced under [GNU 3 public licence](https://www.gnu.org/licenses/gpl-3.0.en.html). All third party libraries are redistributed
148 | under their own licences.
149 |
150 | ---
151 |
--------------------------------------------------------------------------------
/build_index.py:
--------------------------------------------------------------------------------
1 | """
2 | Knowledge index maker
3 | @author Tao PR (github.com/starcolon)
4 | """
5 |
6 | import numpy as np
7 | import os
8 | import sys
9 | import argparse
10 | import word2vec
11 | from termcolor import colored
12 | from collections import Counter
13 | from pylib.knowledge.graph import Knowledge
14 | from pylib.knowledge.datasource import MineDB
15 | from nltk.tokenize.punkt import PunktSentenceTokenizer
16 |
17 | arguments = argparse.ArgumentParser()
18 | arguments.add_argument('--verbose', dest='verbose', action='store_true', help='Turn verbose output on.')
19 | arguments.add_argument('--limit', type=int, default=100, help='Maximum number of topics we want to build index')
20 | arguments.add_argument('--root', type=str, default=None, help='Supply the OrientDB password for root account.')
21 | arguments.add_argument('--modelpath', type=str, default='./models/word2vec.bin', help='Path of the word2vec binary model.')
22 | args = vars(arguments.parse_args(sys.argv[1:]))
23 |
24 | def collect_wordbag(kb, model):
25 | print(colored('Iterating through topics...','cyan'))
26 | n = 0
27 | for topic in kb:
28 | n += 1
29 | kws = list(kb.keywords_in_topic(topic.title, with_edge_count=True))
30 |
31 | # Frequency of [w] in the current topic
32 | cnt = Counter([kw.w for kw in kws])
33 | # Normalise with global frequency
34 | for word in kws:
35 | cnt[word.w] /= word.freq
36 | # Normalise topic counter
37 | norm = np.linalg.norm(list(cnt.values()))
38 | cnt0 = {k:v/norm for k,v in cnt.items()}
39 |
40 | # Generate similar words with word2vec
41 | cnt = {}
42 | for word, freq in cnt0.items():
43 | cnt[word] = freq
44 | try:
45 | indexes, metrics = model.cosine(word)
46 | synnonyms = model.generate_response(indexes, metrics).tolist()
47 | for syn, confidence in synnonyms:
48 | if confidence < 0.85: break
49 | cnt[syn] = confidence * freq
50 | except:
51 | pass
52 |
53 | yield (n,topic,cnt)
54 | if n>=args['limit']: break
55 |
56 | def load_word2vec_model(path):
57 | if not os.path.isfile(model_path):
58 | print(colored('[ERROR] word2vec model does not exist.','red'))
59 | raise RuntimeError('Model does not exist')
60 | print(colored('[Model] loading binary model.','cyan'))
61 | return word2vec.WordVectors.from_binary(model_path, encoding='ISO-8859-1')
62 |
63 | def add_to_index(index,bag):
64 | print('------------------------------------')
65 | n, topic, cnt = bag
66 | print('...Constructing : {}'.format(colored(topic.title,'magenta')))
67 | print('...#{} {}'.format(n, cnt))
68 |
69 | words, weights = zip(*[(w,weight) for w,weight in cnt.items()])
70 | index.add(topic.title, words, weights, verbose=False)
71 |
72 | if __name__ == '__main__':
73 | # Load word2vec model
74 | model_path = os.path.realpath(args['modelpath'])
75 | model = load_word2vec_model(model_path)
76 |
77 | # Initialise a knowledge database
78 | print(colored('Initialising knowledge graph database...','cyan'))
79 | kb = Knowledge('localhost','vor','root',args['root'])
80 |
81 | # Collect topic wordbag
82 | wb = collect_wordbag(kb, model)
83 |
84 | # Create knowledge index
85 | index = Knowledge('localhost','vorindex','root',args['root'])
86 | index.clear()
87 | for bag in wb:
88 | add_to_index(index, bag)
89 |
90 | print(colored('[DONE] all process ended','green'))
--------------------------------------------------------------------------------
/build_knowledge.py:
--------------------------------------------------------------------------------
1 | """
2 | Knowledge graph builder
3 | @author TaoPR (github.com/starcolon)
4 | ---
5 | Process the entire bulk of the crawled dataset (MongoDB)
6 | and potentially create a knowledge graph (OrientDB)
7 | """
8 |
9 | import re
10 | import sys
11 | import argparse
12 | from termcolor import colored
13 | from nltk.tokenize.punkt import PunktSentenceTokenizer
14 | from pylib.knowledge.graph import Knowledge
15 | from pylib.text import structure as TextStructure
16 | from pylib.text.pos_tree import PatternCapture
17 | from pylib.knowledge.datasource import MineDB
18 | from pybloom_live import ScalableBloomFilter
19 |
20 | arguments = argparse.ArgumentParser()
21 | arguments.add_argument('--verbose', dest='verbose', action='store_true', help='Turn verbose output on.')
22 | arguments.add_argument('--start', type=int, default=0, help='Starting index of the crawling record to annotate.')
23 | arguments.add_argument('--root', type=str, default=None, help='Supply the OrientDB password for root account.')
24 | arguments.add_argument('--limit', type=int, default=100, help='Maximum number of topics we want to import')
25 | args = vars(arguments.parse_args(sys.argv[1:]))
26 |
27 | """
28 | Initialise a lazy connection to the crawling record collection
29 | """
30 | def init_crawl_collection():
31 | crawl_collection = MineDB('localhost','vor','crawl')
32 | return crawl_collection
33 |
34 | def iter_topic(crawl_collection,start):
35 |
36 | # Prepare a naive sentence tokeniser utility
37 | pst = PunktSentenceTokenizer()
38 |
39 | n = 0
40 |
41 | for wiki in crawl_collection.query({'downloaded': True},field=None,skip=start):
42 |
43 | # Skip empty content or the added one
44 | if wiki['content'] is None or 'added_to_graph' in wiki:
45 | continue
46 |
47 | m = 0
48 | content = wiki['content']
49 |
50 | if args['verbose']:
51 | print(colored('[Extracting wiki] : ','cyan'), content['title'])
52 |
53 | # A wiki page may probably comprise of multiple content
54 | for c in content['contents']:
55 | # Explode a long topic into list of sentences
56 | sentences = pst.sentences_from_text(c)
57 | for s in sentences:
58 | m += 1
59 | yield (content['title'],s.split(' '))
60 |
61 | # After all sentences are processed,
62 | # mark the current wiki record as 'processed'
63 | crit = {'_id': wiki['_id']}
64 | crawl_collection.update(crit, {'$set':{'added_to_graph':True}})
65 |
66 | n += 1
67 | if args['verbose']:
68 | print(content['title'] + " processed with {0} nodes.".format(m))
69 | print(colored("{0} wiki documents processed so far...".format(n),'blue'))
70 |
71 | """
72 | Remove stopwords & ensure text encoder
73 | """
74 | def ensure_viable(ns,stopwords):
75 | def clean(a):
76 | # Strip non-alphanumeric symbols (unicode symbols reserved)
77 | a = re.sub("[\x00-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F\(\)]+", "", a)
78 | for s in stopwords:
79 | a.replace(s,'')
80 | return a.strip()
81 | ns = set(clean(n) for n in ns)
82 | ns = [n for n in ns if len(n)>2]
83 | return list(ns)
84 |
85 |
86 |
87 | if __name__ == '__main__':
88 |
89 | # Initialise a knowledge database
90 | print(colored('Initialising knowledge graph database...','cyan'))
91 | kb = Knowledge('localhost','vor','root',args['root'])
92 | kb.clear()
93 |
94 | # Load existing pos patterns
95 | print(colored('Loading POS patterns...','cyan'))
96 | patterns = PatternCapture()
97 | patterns.load('./pos-patterns')
98 |
99 | # Load list of stopwords
100 | print(colored('Loading stopwords...','cyan'))
101 | stopwords = []
102 | with open('./pos-stopwords') as f:
103 | stopwords = list(f.readlines())
104 |
105 | # Initialise a crawling dataset connection
106 | print(colored('Initialising wikipedia crawling collection...','cyan'))
107 | crawl_collection = init_crawl_collection()
108 |
109 | # Iterate through the crawling database
110 | n = 0
111 | print(colored('Iterating over crawling database...','cyan'))
112 | bf = ScalableBloomFilter(mode=ScalableBloomFilter.SMALL_SET_GROWTH)
113 | for topic,sentence in iter_topic(crawl_collection,args['start']):
114 |
115 | # Clean topic string
116 | topic = topic.replace("'",'').replace('\n','')
117 |
118 | # Check if the number of processed topic exceed the limit?
119 | if topic not in bf:
120 | bf.add(topic)
121 | if len(bf) > args['limit']:
122 | print(colored('[Topics limit reached] ... BYE','cyan'))
123 | sys.exit(0)
124 |
125 | # Break the sentence into knowledge nodes
126 | pos = TextStructure.pos_tag(sentence)
127 | kb_nodes = patterns.capture(pos)
128 |
129 | # Clean up each of the nodes
130 | # a) Remove stopwords
131 | # b) Remove duplicates
132 | # c) Ensure supported encoding
133 | kb_nodes = ensure_viable(kb_nodes, stopwords)
134 |
135 | if args['verbose']:
136 | print(kb_nodes)
137 |
138 | # Create a set of knowledge links
139 | kb.add(topic,kb_nodes,None,args['verbose'])
140 |
141 | n += 1
142 | if n%100 == 0 and n>0:
143 | print('... {} topics done so far.'.format(n))
144 |
145 |
146 |
--------------------------------------------------------------------------------
/build_wordvec.py:
--------------------------------------------------------------------------------
1 | """
2 | Create word vector space from the crawled dataset
3 | @author TaoPR (github.com/starcolon)
4 | """
5 |
6 | import os
7 | import sys
8 | import codecs
9 | import argparse
10 | import word2vec
11 | from termcolor import colored
12 | from nltk.tokenize.punkt import PunktSentenceTokenizer
13 | from pylib.knowledge.datasource import MineDB
14 | from pylib.text.cleanser import *
15 |
16 | arguments = argparse.ArgumentParser()
17 | arguments.add_argument('--verbose', dest='verbose', action='store_true', help='Turn verbose output on.')
18 | arguments.add_argument('--limit', type=int, default=100, help='Maximum number of topics we want to import')
19 | arguments.add_argument('--out', type=str, default='./models/word2vec.bin', help='Output path of the word2vec binary model.')
20 | args = vars(arguments.parse_args(sys.argv[1:]))
21 |
22 | def model_from_crawl_collection(mineDB, output_path):
23 | # Dump sentences out of the DB
24 | print(colored('Exporting crawled data to text file...','cyan'))
25 | text_path = export_crawl_to_text(mineDB)
26 | print(colored('[Done]','green'))
27 |
28 | # Train word2vec model
29 | print(colored('Training word2vec...','cyan'))
30 | model = create_model(text_path, output_path)
31 | print(colored('[Done]','green'))
32 | print('Word2Vec model is saved at : {}'.format(output_path))
33 |
34 | return model
35 |
36 | def export_crawl_to_text(mineDB):
37 |
38 | # Prepare a naive sentence tokeniser utility
39 | pst = PunktSentenceTokenizer()
40 |
41 | text_path = os.path.realpath('./mine.txt')
42 |
43 | with codecs.open(text_path, 'w', 'utf-8') as f:
44 | m = 0
45 | for wiki in mineDB.query({'downloaded': True},field=None):
46 |
47 | # Skip empty content or the added one
48 | if wiki['content'] is None or 'added_to_graph' in wiki:
49 | continue
50 |
51 | content = wiki['content']
52 |
53 | # A wiki page may probably comprise of multiple content
54 | for c in content['contents']:
55 | # Explode content into sentences
56 | sentences = pst.sentences_from_text(c)
57 | print('... content #{} ==> {} sentences extracted.'.format(m, len(sentences)))
58 |
59 | for s in sentences:
60 | # Cleanse the sentence
61 | s_ = cleanse(s)
62 | # Filter out noise by length
63 | if len(s_)<5 or len(s_.split(' '))<3:
64 | continue
65 | f.write(s_.lower() + '\n')
66 |
67 | m += 1
68 |
69 | if m>=args['limit']:
70 | print(colored('[Ending] Maximum number of topics reached.','yellow'))
71 | break
72 |
73 | return text_path
74 |
75 | def create_model(input_path, output_path):
76 | word2vec.word2vec(\
77 | input_path, \
78 | output_path, \
79 | size=10, binary=1, verbose=True)
80 | assert(os.path.isfile(output_path))
81 | #return word2vec.load(output_path)
82 | return word2vec.WordVectors.from_binary(output_path, encoding='ISO-8859-1')
83 |
84 | def repl(model):
85 | while True:
86 | w = input('Enter a word to try: ')
87 | indexes, metrics = model.cosine(w)
88 | print('... Similar words : {}', model.vocab[indexes])
89 |
90 | if __name__ == '__main__':
91 | mineDB = crawl_collection = MineDB('localhost','vor','crawl')
92 | model = model_from_crawl_collection(mineDB, os.path.realpath(args['out']))
93 |
94 | # Examine the model properties
95 | if model is None:
96 | print(colored('[ERROR] Model is empty.','red'))
97 | else:
98 | print(colored('[Word2Vec model spec]','cyan'))
99 | print('... Model shape : {}'.format(model.vectors.shape))
100 | # Execute a playground REPL
101 | print(colored('[Word2Vec REPL]:','cyan'))
102 | repl(model)
--------------------------------------------------------------------------------
/crawl_wiki.py:
--------------------------------------------------------------------------------
1 | """
2 | Wikipedia crawler service (downloader)
3 | @author TaoPR (github.com/starcolon)
4 | ---
5 | The service runs in background and keeps crawling raw knowledge data.
6 | The downloaded data from Wikipedia is senquentially pushed to
7 | the specified RabbitMQ.
8 | """
9 |
10 | import sys
11 | import asyncio
12 | import argparse
13 | from functools import reduce
14 | from termcolor import colored
15 | from pylib.spider import wiki as Wiki
16 | from pylib.knowledge.datasource import MineDB
17 |
18 | arguments = argparse.ArgumentParser()
19 | arguments.add_argument('--verbose', dest='verbose', action='store_true', help='Turn verbose output on.')
20 | arguments.add_argument('--depth', type=int, default=4, help='Indicate maximum depth of crawling level')
21 | args = vars(arguments.parse_args(sys.argv[1:]))
22 |
23 | """
24 | Initialise a lazy connection to the crawling record collection
25 | """
26 | def init_crawl_collection():
27 | crawl_collection = MineDB('localhost','vor','crawl')
28 | return crawl_collection
29 |
30 | """
31 | Save the content of the wikipedia page
32 | """
33 | def save_content(crawl_collection,title,content):
34 | if crawl_collection.count({'title': title})>0:
35 | crawl_collection.update(
36 | {'title': title},
37 | {'$set':{'downloaded':True, 'content': content}}
38 | )
39 | else:
40 | crawl_collection.insert({'title': title, 'downloaded': True, 'content':content})
41 |
42 | """
43 | Check whether a wiki has been downloaded
44 | """
45 | def is_downloaded(crawl_collection,title):
46 | return crawl_collection.count({'title': title, 'downloaded': True})>0
47 |
48 | """
49 | Mark a wiki page as downloaded
50 | """
51 | def mark_as_downloaded(crawl_collection,title):
52 | if crawl_collection.count({'title': title})>0:
53 | crawl_collection.update({'title': title},{'$set':{'downloaded':True}})
54 | else:
55 | crawl_collection.insert({'title': title, 'downloaded': True, 'content':None})
56 |
57 |
58 | def list_crawl_pending(crawl_collection,max_samples):
59 |
60 | n = 0
61 |
62 | # Major pending list
63 | majors = [t['title'] for t in crawl_collection.query({'downloaded': False})]
64 |
65 | # If fresh new crawl, no pending downloads,
66 | # initialise with a seed
67 | if len(majors)==0:
68 | print(colored('Fresh new crawling...','yellow'))
69 | yield '/wiki/Graph_theory'
70 |
71 | for m in majors:
72 | n += 1
73 | yield m
74 |
75 | # List rels of downloaded major pages
76 | # which are not yet downloaded
77 | for t in crawl_collection.query({'downloaded': True}):
78 | content = t['content']
79 | rels = content['rels']
80 | for r in rels:
81 | # Skip the downloaded links
82 | if not is_downloaded(crawl_collection,r):
83 | # Short circuit if maximum number of samples to generate exceeded
84 | if n>max_samples:
85 | return
86 | n += 1
87 | yield r
88 |
89 | """
90 | Add a fresh new wiki page as yet to be downloaded
91 | """
92 | def add_pending(crawl_collection,title):
93 | if crawl_collection.count({'title': title})==0:
94 | crawl_collection.insert({'title': title, 'downloaded': False, 'content':None})
95 |
96 | """
97 | Execute a crawling subprocess on the destination wiki page title
98 | """
99 | @asyncio.coroutine
100 | def crawl(crawl_collection,title,depth,verbose):
101 | loop = asyncio.get_event_loop()
102 |
103 | # Crawl the content
104 | add_pending(crawl_collection, title)
105 |
106 | # Skip if downloaded or depth recursion exceeded
107 | if depth>0 and not is_downloaded(crawl_collection, title):
108 | content = Wiki.download_wiki('https://en.wikipedia.org' + title, verbose)
109 |
110 | # Store the downloaded content in MongoDB
111 | save_content(crawl_collection, title, content)
112 |
113 | # Now recursively download the related links
114 | subtasks = []
115 | for rel in content['rels']:
116 | subtasks.append(asyncio.async(
117 | crawl(crawl_collection, rel, depth-1, verbose)
118 | ))
119 |
120 | loop.run_until_complete(asyncio.wait(subtasks))
121 |
122 |
123 |
124 | if __name__ == '__main__':
125 |
126 | depth = args['depth']
127 | print(colored('# Max depth to run: ','cyan'), depth)
128 | loop = asyncio.get_event_loop()
129 |
130 | # Prepare the crawling record handler
131 | crawl_collection = init_crawl_collection()
132 |
133 | # Load all list of pending wiki pages
134 | pendings = list_crawl_pending(crawl_collection,max_samples=32)
135 |
136 | # For each of the pending list, spawns a new crawler subprocess
137 | # to download those data
138 | tasks = []
139 | for title in pendings:
140 | print(colored('Pending for crawling: ','green'), title)
141 | tasks.append(asyncio.async(
142 | crawl(crawl_collection, title, depth, args['verbose'])
143 | ))
144 |
145 | # Wait until all top-level async crawling tasks end
146 | loop.run_until_complete(asyncio.wait(tasks))
147 | loop.close()
148 |
149 |
150 |
--------------------------------------------------------------------------------
/create_pos_patterns.py:
--------------------------------------------------------------------------------
1 | """
2 | Make pos patterns which represent knowledge node
3 | from the downloaded wikipedia topics.
4 | @author TaoPR (github.com/starcolon)
5 | """
6 |
7 | import sys
8 | import argparse
9 | from termcolor import colored
10 | from pylib.text import structure as TextStructure
11 | from pylib.text.pos_tree import PatternCapture
12 | from pylib.knowledge.datasource import MineDB
13 | from nltk.tokenize.punkt import PunktSentenceTokenizer
14 |
15 | arguments = argparse.ArgumentParser()
16 | arguments.add_argument('--verbose', dest='verbose', action='store_true', help='Turn verbose output on.')
17 | arguments.add_argument('--start', type=int, default=0, help='Starting index of the crawling record to annotate.')
18 | args = vars(arguments.parse_args(sys.argv[1:]))
19 |
20 | """
21 | Initialise a lazy connection to the crawling record collection
22 | """
23 | def init_crawl_collection():
24 | crawl_collection = MineDB('localhost','vor','crawl')
25 | return crawl_collection
26 |
27 | """
28 | Iterate through the unannotated recordset in the crawled collection,
29 | and generate each of the sentence from the topic.
30 | """
31 | def raw_records(crawl_collection,start):
32 |
33 | # Prepare a naive sentence tokeniser utility
34 | pst = PunktSentenceTokenizer()
35 |
36 | for rec in crawl_collection.query({'downloaded': True},field=None,skip=start):
37 | _id = rec['_id']
38 | if rec['content'] is None:
39 | continue
40 | content = rec['content']['contents']
41 | # A wiki page may probably comprise of multiple content
42 | for c in content:
43 | # Explode a long topic into list of sentences
44 | sentences = pst.sentences_from_text(c)
45 | for s in sentences:
46 | yield (_id,s)
47 |
48 |
49 | """
50 | Prompt the user to annotate the given text sentence
51 | """
52 | def cli_annotate(crawl_collection):
53 | # Load existing pos patterns
54 | patterns = PatternCapture()
55 | patterns.load('./pos-patterns')
56 |
57 | print(colored('Existing patterns :','green'))
58 | print(patterns.join(' , '))
59 |
60 | def annotate(_id,text):
61 | # Analyse the POS structure of the sentence
62 | tokens = text.split(' ')
63 | pos = TextStructure.pos_tag(tokens)
64 | TextStructure.tag_with_color(tokens)
65 |
66 | # Test POS pattern parsing and show the result
67 | print(patterns.capture(pos))
68 |
69 | # Extract the pure list of POS
70 | pos_ = [tag for t,tag in pos]
71 |
72 | # POS token sample form: NN-JJ,NN,NN-NNS
73 | nodes = input(colored("POS token patterns: ","cyan"))
74 |
75 | if len(nodes)>0:
76 | # Add patterns to the registry if not yet
77 | nodes = [n.strip() for n in nodes.split(',')]
78 | for n in nodes:
79 | if n not in patterns:
80 | print(colored('New pattern added: ','green'), n)
81 | patterns.append(n)
82 |
83 | # Save right away
84 | patterns.save('./pos-patterns')
85 | print("Patterns saved!")
86 |
87 | return annotate
88 |
89 | if __name__ == '__main__':
90 |
91 | # Prepare a connection to the crawled dataset
92 | # and the annotation collection respectively
93 | crawl_collection = init_crawl_collection()
94 |
95 | # Make an annotator function
96 | annotate = cli_annotate(crawl_collection)
97 |
98 | # Iterate through each unannotated sentence
99 | [annotate(_id,t) for (_id,t) in raw_records(crawl_collection,args['start'])]
100 |
--------------------------------------------------------------------------------
/graphic/graph.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tao-pr/vor-knowledge-graph/7c7140b682796b997871751090ac3a1b3ea36eee/graphic/graph.png
--------------------------------------------------------------------------------
/graphic/index-0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tao-pr/vor-knowledge-graph/7c7140b682796b997871751090ac3a1b3ea36eee/graphic/index-0.png
--------------------------------------------------------------------------------
/graphic/index-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tao-pr/vor-knowledge-graph/7c7140b682796b997871751090ac3a1b3ea36eee/graphic/index-1.png
--------------------------------------------------------------------------------
/graphic/index-2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tao-pr/vor-knowledge-graph/7c7140b682796b997871751090ac3a1b3ea36eee/graphic/index-2.png
--------------------------------------------------------------------------------
/graphic/vor.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tao-pr/vor-knowledge-graph/7c7140b682796b997871751090ac3a1b3ea36eee/graphic/vor.png
--------------------------------------------------------------------------------
/html/graph-index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
64 |
65 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/html/graph-universe.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
--------------------------------------------------------------------------------
/html/sigma.min.js:
--------------------------------------------------------------------------------
1 | /* sigma.js - A JavaScript library dedicated to graph drawing. - Version: 1.1.0 - Author: Alexis Jacomy, Sciences-Po Médialab - License: MIT */
2 | (function(a){"use strict";var b={},c=function(a){var d,e,f,g,h;c.classes.dispatcher.extend(this);var i=this,j=a||{};if("string"==typeof j||j instanceof HTMLElement?j={renderers:[j]}:"[object Array]"===Object.prototype.toString.call(j)&&(j={renderers:j}),g=j.renderers||j.renderer||j.container,j.renderers&&0!==j.renderers.length||("string"==typeof g||g instanceof HTMLElement||"object"==typeof g&&"container"in g)&&(j.renderers=[g]),j.id){if(b[j.id])throw'sigma: Instance "'+j.id+'" already exists.';Object.defineProperty(this,"id",{value:j.id})}else{for(h=0;b[h];)h++;Object.defineProperty(this,"id",{value:""+h})}for(b[this.id]=this,this.settings=new c.classes.configurable(c.settings,j.settings||{}),Object.defineProperty(this,"graph",{value:new c.classes.graph(this.settings),configurable:!0}),Object.defineProperty(this,"middlewares",{value:[],configurable:!0}),Object.defineProperty(this,"cameras",{value:{},configurable:!0}),Object.defineProperty(this,"renderers",{value:{},configurable:!0}),Object.defineProperty(this,"renderersPerCamera",{value:{},configurable:!0}),Object.defineProperty(this,"cameraFrames",{value:{},configurable:!0}),Object.defineProperty(this,"camera",{get:function(){return this.cameras[0]}}),Object.defineProperty(this,"events",{value:["click","rightClick","clickStage","doubleClickStage","rightClickStage","clickNode","clickNodes","doubleClickNode","doubleClickNodes","rightClickNode","rightClickNodes","overNode","overNodes","outNode","outNodes","downNode","downNodes","upNode","upNodes"],configurable:!0}),this._handler=function(a){var b,c={};for(b in a.data)c[b]=a.data[b];c.renderer=a.target,this.dispatchEvent(a.type,c)}.bind(this),f=j.renderers||[],d=0,e=f.length;e>d;d++)this.addRenderer(f[d]);for(f=j.middlewares||[],d=0,e=f.length;e>d;d++)this.middlewares.push("string"==typeof f[d]?c.middlewares[f[d]]:f[d]);"object"==typeof j.graph&&j.graph&&(this.graph.read(j.graph),this.refresh()),window.addEventListener("resize",function(){i.settings&&i.refresh()})};if(c.prototype.addCamera=function(b){var d,e=this;if(!arguments.length){for(b=0;this.cameras[""+b];)b++;b=""+b}if(this.cameras[b])throw'sigma.addCamera: The camera "'+b+'" already exists.';return d=new c.classes.camera(b,this.graph,this.settings),this.cameras[b]=d,d.quadtree=new c.classes.quad,c.classes.edgequad!==a&&(d.edgequadtree=new c.classes.edgequad),d.bind("coordinatesUpdated",function(){e.renderCamera(d,d.isAnimated)}),this.renderersPerCamera[b]=[],d},c.prototype.killCamera=function(a){if(a="string"==typeof a?this.cameras[a]:a,!a)throw"sigma.killCamera: The camera is undefined.";var b,c,d=this.renderersPerCamera[a.id];for(c=d.length,b=c-1;b>=0;b--)this.killRenderer(d[b]);return delete this.renderersPerCamera[a.id],delete this.cameraFrames[a.id],delete this.cameras[a.id],a.kill&&a.kill(),this},c.prototype.addRenderer=function(a){var b,d,e,f,g=a||{};if("string"==typeof g?g={container:document.getElementById(g)}:g instanceof HTMLElement&&(g={container:g}),"string"==typeof g.container&&(g.container=document.getElementById(g.container)),"id"in g)b=g.id;else{for(b=0;this.renderers[""+b];)b++;b=""+b}if(this.renderers[b])throw'sigma.addRenderer: The renderer "'+b+'" already exists.';if(d="function"==typeof g.type?g.type:c.renderers[g.type],d=d||c.renderers.def,e="camera"in g?g.camera instanceof c.classes.camera?g.camera:this.cameras[g.camera]||this.addCamera(g.camera):this.addCamera(),this.cameras[e.id]!==e)throw"sigma.addRenderer: The camera is not properly referenced.";return f=new d(this.graph,e,this.settings,g),this.renderers[b]=f,Object.defineProperty(f,"id",{value:b}),f.bind&&f.bind(["click","rightClick","clickStage","doubleClickStage","rightClickStage","clickNode","clickNodes","clickEdge","clickEdges","doubleClickNode","doubleClickNodes","doubleClickEdge","doubleClickEdges","rightClickNode","rightClickNodes","rightClickEdge","rightClickEdges","overNode","overNodes","overEdge","overEdges","outNode","outNodes","outEdge","outEdges","downNode","downNodes","downEdge","downEdges","upNode","upNodes","upEdge","upEdges"],this._handler),this.renderersPerCamera[e.id].push(f),f},c.prototype.killRenderer=function(a){if(a="string"==typeof a?this.renderers[a]:a,!a)throw"sigma.killRenderer: The renderer is undefined.";var b=this.renderersPerCamera[a.camera.id],c=b.indexOf(a);return c>=0&&b.splice(c,1),a.kill&&a.kill(),delete this.renderers[a.id],this},c.prototype.refresh=function(b){var d,e,f,g,h,i,j=0;for(b=b||{},g=this.middlewares||[],d=0,e=g.length;e>d;d++)g[d].call(this,0===d?"":"tmp"+j+":",d===e-1?"ready:":"tmp"+ ++j+":");for(f in this.cameras)h=this.cameras[f],h.settings("autoRescale")&&this.renderersPerCamera[h.id]&&this.renderersPerCamera[h.id].length?c.middlewares.rescale.call(this,g.length?"ready:":"",h.readPrefix,{width:this.renderersPerCamera[h.id][0].width,height:this.renderersPerCamera[h.id][0].height}):c.middlewares.copy.call(this,g.length?"ready:":"",h.readPrefix),b.skipIndexation||(i=c.utils.getBoundaries(this.graph,h.readPrefix),h.quadtree.index(this.graph.nodes(),{prefix:h.readPrefix,bounds:{x:i.minX,y:i.minY,width:i.maxX-i.minX,height:i.maxY-i.minY}}),h.edgequadtree!==a&&h.settings("drawEdges")&&h.settings("enableEdgeHovering")&&h.edgequadtree.index(this.graph,{prefix:h.readPrefix,bounds:{x:i.minX,y:i.minY,width:i.maxX-i.minX,height:i.maxY-i.minY}}));for(g=Object.keys(this.renderers),d=0,e=g.length;e>d;d++)if(this.renderers[g[d]].process)if(this.settings("skipErrors"))try{this.renderers[g[d]].process()}catch(k){console.log('Warning: The renderer "'+g[d]+'" crashed on ".process()"')}else this.renderers[g[d]].process();return this.render(),this},c.prototype.render=function(){var a,b,c;for(c=Object.keys(this.renderers),a=0,b=c.length;b>a;a++)if(this.settings("skipErrors"))try{this.renderers[c[a]].render()}catch(d){this.settings("verbose")&&console.log('Warning: The renderer "'+c[a]+'" crashed on ".render()"')}else this.renderers[c[a]].render();return this},c.prototype.renderCamera=function(a,b){var c,d,e,f=this;if(b)for(e=this.renderersPerCamera[a.id],c=0,d=e.length;d>c;c++)if(this.settings("skipErrors"))try{e[c].render()}catch(g){this.settings("verbose")&&console.log('Warning: The renderer "'+e[c].id+'" crashed on ".render()"')}else e[c].render();else if(!this.cameraFrames[a.id]){for(e=this.renderersPerCamera[a.id],c=0,d=e.length;d>c;c++)if(this.settings("skipErrors"))try{e[c].render()}catch(g){this.settings("verbose")&&console.log('Warning: The renderer "'+e[c].id+'" crashed on ".render()"')}else e[c].render();this.cameraFrames[a.id]=requestAnimationFrame(function(){delete f.cameraFrames[a.id]})}return this},c.prototype.kill=function(){var a;this.dispatchEvent("kill"),this.graph.kill(),delete this.middlewares;for(a in this.renderers)this.killRenderer(this.renderers[a]);for(a in this.cameras)this.killCamera(this.cameras[a]);delete this.renderers,delete this.cameras;for(a in this)this.hasOwnProperty(a)&&delete this[a];delete b[this.id]},c.instances=function(a){return arguments.length?b[a]:c.utils.extend({},b)},c.version="1.1.0","undefined"!=typeof this.sigma)throw"An object called sigma is already in the global scope.";this.sigma=c}).call(this),function(a){"use strict";function b(a,c){var d,e,f,g;if(arguments.length)if(1===arguments.length&&Object(arguments[0])===arguments[0])for(a in arguments[0])b(a,arguments[0][a]);else if(arguments.length>1)for(g=Array.isArray(a)?a:a.split(/ /),d=0,e=g.length;d!==e;d+=1)f=g[d],C[f]||(C[f]=[]),C[f].push({handler:c})}function c(a,b){var c,d,e,f,g,h,i=Array.isArray(a)?a:a.split(/ /);if(arguments.length)if(b)for(c=0,d=i.length;c!==d;c+=1){if(h=i[c],C[h]){for(g=[],e=0,f=C[h].length;e!==f;e+=1)C[h][e].handler!==b&&g.push(C[h][e]);C[h]=g}C[h]&&0===C[h].length&&delete C[h]}else for(c=0,d=i.length;c!==d;c+=1)delete C[i[c]];else C=Object.create(null)}function d(a,b){var c,d,e,f,g,h,i=Array.isArray(a)?a:a.split(/ /);for(b=void 0===b?{}:b,c=0,e=i.length;c!==e;c+=1)if(h=i[c],C[h])for(g={type:h,data:b||{}},d=0,f=C[h].length;d!==f;d+=1)try{C[h][d].handler(g)}catch(j){}}function e(){var a,b,c,d,e=!1,f=s(),g=x.shift();if(c=g.job(),f=s()-f,g.done++,g.time+=f,g.currentTime+=f,g.weightTime=g.currentTime/(g.weight||1),g.averageTime=g.time/g.done,d=g.count?g.count<=g.done:!c,!d){for(a=0,b=x.length;b>a;a++)if(x[a].weightTime>g.weightTime){x.splice(a,0,g),e=!0;break}e||x.push(g)}return d?g:null}function f(a){var b=x.length;w[a.id]=a,a.status="running",b&&(a.weightTime=x[b-1].weightTime,a.currentTime=a.weightTime*(a.weight||1)),a.startTime=s(),d("jobStarted",q(a)),x.push(a)}function g(){var a,b,c;for(a in v)b=v[a],b.after?y[a]=b:f(b),delete v[a];for(u=!!x.length;x.length&&s()-t
c;c++)h(a[c].id,p(a[c],b));A=!1,u||(t=s(),d("start"),g())}else if("object"==typeof a)if("string"==typeof a.id)h(a.id,a);else{A=!0;for(c in a)"function"==typeof a[c]?h(c,p({job:a[c]},b)):h(c,p(a[c],b));A=!1,u||(t=s(),d("start"),g())}else{if("string"!=typeof a)throw new Error("[conrad.addJob] Wrong arguments.");if(k(a))throw new Error('[conrad.addJob] Job with id "'+a+'" already exists.');if("function"==typeof b)f={id:a,done:0,time:0,status:"waiting",currentTime:0,averageTime:0,weightTime:0,job:b};else{if("object"!=typeof b)throw new Error("[conrad.addJob] Wrong arguments.");f=p({id:a,done:0,time:0,status:"waiting",currentTime:0,averageTime:0,weightTime:0},b)}v[a]=f,d("jobAdded",q(f)),u||A||(t=s(),d("start"),g())}return this}function i(a){var b,c,e,f,g=!1;if(Array.isArray(a))for(b=0,c=a.length;c>b;b++)i(a[b]);else{if("string"!=typeof a)throw new Error("[conrad.killJob] Wrong arguments.");for(e=[w,y,v],b=0,c=e.length;c>b;b++)a in e[b]&&(f=e[b][a],B.history&&(f.status="done",z.push(f)),d("jobEnded",q(f)),delete e[b][a],"function"==typeof f.end&&f.end(),g=!0);for(e=x,b=0,c=e.length;c>b;b++)if(e[b].id===a){e.splice(b,1);break}if(!g)throw new Error('[conrad.killJob] Job "'+a+'" not found.')}return this}function j(){var a,b=p(v,w,y);if(B.history)for(a in b)b[a].status="done",z.push(b[a]),"function"==typeof b[a].end&&b[a].end();return v={},y={},w={},x=[],u=!1,this}function k(a){var b=v[a]||w[a]||y[a];return b?p(b):null}function l(){var a;if("string"==typeof a1&&1===arguments.length)return B[a1];a="object"==typeof a1&&1===arguments.length?a1||{}:{},"string"==typeof a1&&(a[a1]=a2);for(var b in a)void 0!==a[b]?B[b]=a[b]:delete B[b];return this}function m(){return u}function n(){return z=[],this}function o(a,b){var c,d,e,f,g,h,i;if(!arguments.length){g=[];for(d in v)g.push(v[d]);for(d in y)g.push(y[d]);for(d in w)g.push(w[d]);g=g.concat(z)}if("string"==typeof a)switch(a){case"waiting":g=r(y);break;case"running":g=r(w);break;case"done":g=z;break;default:h=a}if(a instanceof RegExp&&(h=a),!h&&("string"==typeof b||b instanceof RegExp)&&(h=b),h){if(i="string"==typeof h,g instanceof Array)c=g;else if("object"==typeof g){c=[];for(d in g)c=c.concat(g[d])}else{c=[];for(d in v)c.push(v[d]);for(d in y)c.push(y[d]);for(d in w)c.push(w[d]);c=c.concat(z)}for(g=[],e=0,f=c.length;f>e;e++)(i?c[e].id===h:c[e].id.match(h))&&g.push(c[e])}return q(g)}function p(){var a,b,c={},d=arguments.length;for(a=d-1;a>=0;a--)for(b in arguments[a])c[b]=arguments[a][b];return c}function q(a){var b,c,d;if(!a)return a;if(Array.isArray(a))for(b=[],c=0,d=a.length;d>c;c++)b.push(q(a[c]));else if("object"==typeof a){b={};for(c in a)b[c]=q(a[c])}else b=a;return b}function r(a){var b,c=[];for(b in a)c.push(a[b]);return c}function s(){return Date.now?Date.now():(new Date).getTime()}if(a.conrad)throw new Error("conrad already exists");var t,u=!1,v={},w={},x=[],y={},z=[],A=!1,B={frameDuration:20,history:!0},C=Object.create(null);Array.isArray||(Array.isArray=function(a){return"[object Array]"===Object.prototype.toString.call(a)});var D={hasJob:k,addJob:h,killJob:i,killAll:j,settings:l,getStats:o,isRunning:m,clearHistory:n,bind:b,unbind:c,version:"0.1.0"};"undefined"!=typeof exports&&("undefined"!=typeof module&&module.exports&&(exports=module.exports=D),exports.conrad=D),a.conrad=D}(this),function(a){"use strict";if("undefined"==typeof sigma)throw"sigma is not declared";var b=this;sigma.utils=sigma.utils||{},sigma.utils.extend=function(){var a,b,c={},d=arguments.length;for(a=d-1;a>=0;a--)for(b in arguments[a])c[b]=arguments[a][b];return c},sigma.utils.dateNow=function(){return Date.now?Date.now():(new Date).getTime()},sigma.utils.pkg=function(a){return(a||"").split(".").reduce(function(a,b){return b in a?a[b]:a[b]={}},b)},sigma.utils.id=function(){var a=0;return function(){return++a}}();var c={};sigma.utils.floatColor=function(a){if(c[a])return c[a];var b=a,d=0,e=0,f=0;"#"===a[0]?(a=a.slice(1),3===a.length?(d=parseInt(a.charAt(0)+a.charAt(0),16),e=parseInt(a.charAt(1)+a.charAt(1),16),f=parseInt(a.charAt(2)+a.charAt(2),16)):(d=parseInt(a.charAt(0)+a.charAt(1),16),e=parseInt(a.charAt(2)+a.charAt(3),16),f=parseInt(a.charAt(4)+a.charAt(5),16))):a.match(/^ *rgba? *\(/)&&(a=a.match(/^ *rgba? *\( *([0-9]*) *, *([0-9]*) *, *([0-9]*) *(,.*)?\) *$/),d=+a[1],e=+a[2],f=+a[3]);var g=256*d*256+256*e+f;return c[b]=g,g},sigma.utils.zoomTo=function(a,b,c,d,e){var f,g,h,i=a.settings;g=Math.max(i("zoomMin"),Math.min(i("zoomMax"),a.ratio*d)),g!==a.ratio&&(d=g/a.ratio,h={x:b*(1-d)+a.x,y:c*(1-d)+a.y,ratio:g},e&&e.duration?(f=sigma.misc.animation.killAll(a),e=sigma.utils.extend(e,{easing:f?"quadraticOut":"quadraticInOut"}),sigma.misc.animation.camera(a,h,e)):(a.goTo(h),e&&e.onComplete&&e.onComplete()))},sigma.utils.getQuadraticControlPoint=function(a,b,c,d){return{x:(a+c)/2+(d-b)/4,y:(b+d)/2+(a-c)/4}},sigma.utils.getPointOnQuadraticCurve=function(a,b,c,d,e,f,g){return{x:Math.pow(1-a,2)*b+2*(1-a)*a*f+Math.pow(a,2)*d,y:Math.pow(1-a,2)*c+2*(1-a)*a*g+Math.pow(a,2)*e}},sigma.utils.getPointOnBezierCurve=function(a,b,c,d,e,f,g,h,i){var j=Math.pow(1-a,3),k=3*a*Math.pow(1-a,2),l=3*Math.pow(a,2)*(1-a),m=Math.pow(a,3);return{x:j*b+k*f+l*h+m*d,y:j*c+k*g+l*i+m*e}},sigma.utils.getSelfLoopControlPoints=function(a,b,c){return{x1:a-7*c,y1:b,x2:a,y2:b+7*c}},sigma.utils.getDistance=function(a,b,c,d){return Math.sqrt(Math.pow(c-a,2)+Math.pow(d-b,2))},sigma.utils.getCircleIntersection=function(a,b,c,d,e,f){var g,h,i,j,k,l,m,n,o;if(h=d-a,i=e-b,j=Math.sqrt(i*i+h*h),j>c+f)return!1;if(jj&&Math.min(c,e)<=a&&a<=Math.max(c,e)&&Math.min(d,f)<=b&&b<=Math.max(d,f)},sigma.utils.isPointOnQuadraticCurve=function(a,b,c,d,e,f,g,h,i){var j=sigma.utils.getDistance(c,d,e,f);if(Math.abs(a-c)>j||Math.abs(b-d)>j)return!1;for(var k,l=sigma.utils.getDistance(a,b,c,d),m=sigma.utils.getDistance(a,b,e,f),n=.5,o=m>l?-.01:.01,p=.001,q=100,r=sigma.utils.getPointOnQuadraticCurve(n,c,d,e,f,g,h),s=sigma.utils.getDistance(a,b,r.x,r.y);q-->0&&n>=0&&1>=n&&s>i&&(o>p||-p>o);)k=s,r=sigma.utils.getPointOnQuadraticCurve(n,c,d,e,f,g,h),s=sigma.utils.getDistance(a,b,r.x,r.y),s>k?(o=-o/2,n+=o):0>n+o||n+o>1?(o/=2,s=k):n+=o;return i>s},sigma.utils.isPointOnBezierCurve=function(a,b,c,d,e,f,g,h,i,j,k){var l=sigma.utils.getDistance(c,d,g,h);if(Math.abs(a-c)>l||Math.abs(b-d)>l)return!1;for(var m,n=sigma.utils.getDistance(a,b,c,d),o=sigma.utils.getDistance(a,b,e,f),p=.5,q=o>n?-.01:.01,r=.001,s=100,t=sigma.utils.getPointOnBezierCurve(p,c,d,e,f,g,h,i,j),u=sigma.utils.getDistance(a,b,t.x,t.y);s-->0&&p>=0&&1>=p&&u>k&&(q>r||-r>q);)m=u,t=sigma.utils.getPointOnBezierCurve(p,c,d,e,f,g,h,i,j),u=sigma.utils.getDistance(a,b,t.x,t.y),u>m?(q=-q/2,p+=q):0>p+q||p+q>1?(q/=2,u=m):p+=q;return k>u},sigma.utils.getX=function(b){return b.offsetX!==a&&b.offsetX||b.layerX!==a&&b.layerX||b.clientX!==a&&b.clientX},sigma.utils.getY=function(b){return b.offsetY!==a&&b.offsetY||b.layerY!==a&&b.layerY||b.clientY!==a&&b.clientY},sigma.utils.getPixelRatio=function(){var b=1;return window.screen.deviceXDPI!==a&&window.screen.logicalXDPI!==a&&window.screen.deviceXDPI>window.screen.logicalXDPI?b=window.screen.systemXDPI/window.screen.logicalXDPI:window.devicePixelRatio!==a&&(b=window.devicePixelRatio),b},sigma.utils.getWidth=function(b){var c=b.target.ownerSVGElement?b.target.ownerSVGElement.width:b.target.width;return"number"==typeof c&&c||c!==a&&c.baseVal!==a&&c.baseVal.value},sigma.utils.getCenter=function(a){var b=-1!==a.target.namespaceURI.indexOf("svg")?1:sigma.utils.getPixelRatio();return{x:sigma.utils.getWidth(a)/(2*b),y:sigma.utils.getHeight(a)/(2*b)}},sigma.utils.mouseCoords=function(a,b,c){return b=b||sigma.utils.getX(a),c=c||sigma.utils.getY(a),{x:b-sigma.utils.getCenter(a).x,y:c-sigma.utils.getCenter(a).y,clientX:a.clientX,clientY:a.clientY,ctrlKey:a.ctrlKey,metaKey:a.metaKey,altKey:a.altKey,shiftKey:a.shiftKey}},sigma.utils.getHeight=function(b){var c=b.target.ownerSVGElement?b.target.ownerSVGElement.height:b.target.height;return"number"==typeof c&&c||c!==a&&c.baseVal!==a&&c.baseVal.value},sigma.utils.getDelta=function(b){return b.wheelDelta!==a&&b.wheelDelta||b.detail!==a&&-b.detail},sigma.utils.getOffset=function(a){for(var b=0,c=0;a;)c+=parseInt(a.offsetTop),b+=parseInt(a.offsetLeft),a=a.offsetParent;return{top:c,left:b}},sigma.utils.doubleClick=function(a,b,c){var d,e=0;a._doubleClickHandler=a._doubleClickHandler||{},a._doubleClickHandler[b]=a._doubleClickHandler[b]||[],d=a._doubleClickHandler[b],d.push(function(a){return e++,2===e?(e=0,c(a)):void(1===e&&setTimeout(function(){e=0},sigma.settings.doubleClickTimeout))}),a.addEventListener(b,d[d.length-1],!1)},sigma.utils.unbindDoubleClick=function(a,b){for(var c,d=(a._doubleClickHandler||{})[b]||[];c=d.pop();)a.removeEventListener(b,c);delete(a._doubleClickHandler||{})[b]},sigma.utils.easings=sigma.utils.easings||{},sigma.utils.easings.linearNone=function(a){return a},sigma.utils.easings.quadraticIn=function(a){return a*a},sigma.utils.easings.quadraticOut=function(a){return a*(2-a)},sigma.utils.easings.quadraticInOut=function(a){return(a*=2)<1?.5*a*a:-.5*(--a*(a-2)-1)},sigma.utils.easings.cubicIn=function(a){return a*a*a},sigma.utils.easings.cubicOut=function(a){return--a*a*a+1},sigma.utils.easings.cubicInOut=function(a){return(a*=2)<1?.5*a*a*a:.5*((a-=2)*a*a+2)},sigma.utils.loadShader=function(a,b,c,d){var e,f=a.createShader(c);return a.shaderSource(f,b),a.compileShader(f),e=a.getShaderParameter(f,a.COMPILE_STATUS),e?f:(d&&d('Error compiling shader "'+f+'":'+a.getShaderInfoLog(f)),a.deleteShader(f),null)},sigma.utils.loadProgram=function(a,b,c,d,e){var f,g,h=a.createProgram();for(f=0;fg;g++)if(void 0!==e[g][a])return e[g][a];return void 0}if("object"==typeof a&&"string"==typeof b)return void 0!==(a||{})[b]?a[b]:f(b);for(c="object"==typeof a&&void 0===b?a:{},"string"==typeof a&&(c[a]=b),g=0,i=Object.keys(c),h=i.length;h>g;g++)d[i[g]]=c[i[g]];return this};for(f.embedObjects=function(){var b=e.concat(d).concat(Array.prototype.splice.call(arguments,0));return a.apply({},b)},b=0,c=arguments.length;c>b;b++)f(arguments[b]);return f};"undefined"!=typeof this.sigma?(this.sigma.classes=this.sigma.classes||{},this.sigma.classes.configurable=a):"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=a),exports.configurable=a):this.configurable=a}.call(this),function(){"use strict";function a(a,b,c){var d=function(){var d,e;for(d in g[a])g[a][d].apply(b,arguments);e=c.apply(b,arguments);for(d in f[a])f[a][d].apply(b,arguments);return e};return d}function b(a){var b;for(b in a)"hasOwnProperty"in a&&!a.hasOwnProperty(b)||delete a[b];return a}var c=Object.create(null),d=Object.create(null),e=Object.create(null),f=Object.create(null),g=Object.create(null),h={immutable:!0,clone:!0},i=function(a){return h[a]},j=function(b){var d,f,g;g={settings:b||i,nodesArray:[],edgesArray:[],nodesIndex:Object.create(null),edgesIndex:Object.create(null),inNeighborsIndex:Object.create(null),outNeighborsIndex:Object.create(null),allNeighborsIndex:Object.create(null),inNeighborsCount:Object.create(null),outNeighborsCount:Object.create(null),allNeighborsCount:Object.create(null)};for(d in e)e[d].call(g);for(d in c)f=a(d,g,c[d]),this[d]=f,g[d]=f};j.addMethod=function(a,b){if("string"!=typeof a||"function"!=typeof b||2!==arguments.length)throw"addMethod: Wrong arguments.";if(c[a]||j[a])throw'The method "'+a+'" already exists.';return c[a]=b,f[a]=Object.create(null),g[a]=Object.create(null),this},j.hasMethod=function(a){return!(!c[a]&&!j[a])},j.attach=function(a,b,c,d){if("string"!=typeof a||"string"!=typeof b||"function"!=typeof c||arguments.length<3||arguments.length>4)throw"attach: Wrong arguments.";var h;if("constructor"===a)h=e;else if(d){if(!g[a])throw'The method "'+a+'" does not exist.';h=g[a]}else{if(!f[a])throw'The method "'+a+'" does not exist.';h=f[a]}if(h[b])throw'A function "'+b+'" is already attached to the method "'+a+'".';return h[b]=c,this},j.attachBefore=function(a,b,c){return this.attach(a,b,c,!0)},j.addIndex=function(a,b){if("string"!=typeof a||Object(b)!==b||2!==arguments.length)throw"addIndex: Wrong arguments.";if(d[a])throw'The index "'+a+'" already exists.';var c;d[a]=b;for(c in b){if("function"!=typeof b[c])throw"The bindings must be functions.";j.attach(c,a,b[c])}return this},j.addMethod("addNode",function(a){if(Object(a)!==a||1!==arguments.length)throw"addNode: Wrong arguments.";if("string"!=typeof a.id&&"number"!=typeof a.id)throw"The node must have a string or number id.";if(this.nodesIndex[a.id])throw'The node "'+a.id+'" already exists.';var b,c=a.id,d=Object.create(null);if(this.settings("clone"))for(b in a)"id"!==b&&(d[b]=a[b]);else d=a;return this.settings("immutable")?Object.defineProperty(d,"id",{value:c,enumerable:!0}):d.id=c,this.inNeighborsIndex[c]=Object.create(null),this.outNeighborsIndex[c]=Object.create(null),this.allNeighborsIndex[c]=Object.create(null),this.inNeighborsCount[c]=0,this.outNeighborsCount[c]=0,this.allNeighborsCount[c]=0,this.nodesArray.push(d),this.nodesIndex[d.id]=d,this}),j.addMethod("addEdge",function(a){if(Object(a)!==a||1!==arguments.length)throw"addEdge: Wrong arguments.";if("string"!=typeof a.id&&"number"!=typeof a.id)throw"The edge must have a string or number id.";if("string"!=typeof a.source&&"number"!=typeof a.source||!this.nodesIndex[a.source])throw"The edge source must have an existing node id.";if("string"!=typeof a.target&&"number"!=typeof a.target||!this.nodesIndex[a.target])throw"The edge target must have an existing node id.";if(this.edgesIndex[a.id])throw'The edge "'+a.id+'" already exists.';var b,c=Object.create(null);if(this.settings("clone"))for(b in a)"id"!==b&&"source"!==b&&"target"!==b&&(c[b]=a[b]);else c=a;return this.settings("immutable")?(Object.defineProperty(c,"id",{value:a.id,enumerable:!0}),Object.defineProperty(c,"source",{value:a.source,enumerable:!0}),Object.defineProperty(c,"target",{value:a.target,enumerable:!0})):(c.id=a.id,c.source=a.source,c.target=a.target),this.edgesArray.push(c),this.edgesIndex[c.id]=c,this.inNeighborsIndex[c.target][c.source]||(this.inNeighborsIndex[c.target][c.source]=Object.create(null)),this.inNeighborsIndex[c.target][c.source][c.id]=c,this.outNeighborsIndex[c.source][c.target]||(this.outNeighborsIndex[c.source][c.target]=Object.create(null)),this.outNeighborsIndex[c.source][c.target][c.id]=c,this.allNeighborsIndex[c.source][c.target]||(this.allNeighborsIndex[c.source][c.target]=Object.create(null)),this.allNeighborsIndex[c.source][c.target][c.id]=c,c.target!==c.source&&(this.allNeighborsIndex[c.target][c.source]||(this.allNeighborsIndex[c.target][c.source]=Object.create(null)),this.allNeighborsIndex[c.target][c.source][c.id]=c),this.inNeighborsCount[c.target]++,this.outNeighborsCount[c.source]++,this.allNeighborsCount[c.target]++,this.allNeighborsCount[c.source]++,this}),j.addMethod("dropNode",function(a){if("string"!=typeof a&&"number"!=typeof a||1!==arguments.length)throw"dropNode: Wrong arguments.";if(!this.nodesIndex[a])throw'The node "'+a+'" does not exist.';var b,c,d;for(delete this.nodesIndex[a],b=0,d=this.nodesArray.length;d>b;b++)if(this.nodesArray[b].id===a){this.nodesArray.splice(b,1);break}for(b=this.edgesArray.length-1;b>=0;b--)(this.edgesArray[b].source===a||this.edgesArray[b].target===a)&&this.dropEdge(this.edgesArray[b].id);delete this.inNeighborsIndex[a],delete this.outNeighborsIndex[a],delete this.allNeighborsIndex[a],delete this.inNeighborsCount[a],delete this.outNeighborsCount[a],delete this.allNeighborsCount[a];for(c in this.nodesIndex)delete this.inNeighborsIndex[c][a],delete this.outNeighborsIndex[c][a],delete this.allNeighborsIndex[c][a];return this}),j.addMethod("dropEdge",function(a){if("string"!=typeof a&&"number"!=typeof a||1!==arguments.length)throw"dropEdge: Wrong arguments.";if(!this.edgesIndex[a])throw'The edge "'+a+'" does not exist.';var b,c,d;for(d=this.edgesIndex[a],delete this.edgesIndex[a],b=0,c=this.edgesArray.length;c>b;b++)if(this.edgesArray[b].id===a){this.edgesArray.splice(b,1);break}return delete this.inNeighborsIndex[d.target][d.source][d.id],Object.keys(this.inNeighborsIndex[d.target][d.source]).length||delete this.inNeighborsIndex[d.target][d.source],delete this.outNeighborsIndex[d.source][d.target][d.id],Object.keys(this.outNeighborsIndex[d.source][d.target]).length||delete this.outNeighborsIndex[d.source][d.target],delete this.allNeighborsIndex[d.source][d.target][d.id],Object.keys(this.allNeighborsIndex[d.source][d.target]).length||delete this.allNeighborsIndex[d.source][d.target],d.target!==d.source&&(delete this.allNeighborsIndex[d.target][d.source][d.id],Object.keys(this.allNeighborsIndex[d.target][d.source]).length||delete this.allNeighborsIndex[d.target][d.source]),this.inNeighborsCount[d.target]--,this.outNeighborsCount[d.source]--,this.allNeighborsCount[d.source]--,this.allNeighborsCount[d.target]--,this}),j.addMethod("kill",function(){this.nodesArray.length=0,this.edgesArray.length=0,delete this.nodesArray,delete this.edgesArray,delete this.nodesIndex,delete this.edgesIndex,delete this.inNeighborsIndex,delete this.outNeighborsIndex,delete this.allNeighborsIndex,delete this.inNeighborsCount,delete this.outNeighborsCount,delete this.allNeighborsCount
3 | }),j.addMethod("clear",function(){return this.nodesArray.length=0,this.edgesArray.length=0,b(this.nodesIndex),b(this.edgesIndex),b(this.nodesIndex),b(this.inNeighborsIndex),b(this.outNeighborsIndex),b(this.allNeighborsIndex),b(this.inNeighborsCount),b(this.outNeighborsCount),b(this.allNeighborsCount),this}),j.addMethod("read",function(a){var b,c,d;for(c=a.nodes||[],b=0,d=c.length;d>b;b++)this.addNode(c[b]);for(c=a.edges||[],b=0,d=c.length;d>b;b++)this.addEdge(c[b]);return this}),j.addMethod("nodes",function(a){if(!arguments.length)return this.nodesArray.slice(0);if(1===arguments.length&&("string"==typeof a||"number"==typeof a))return this.nodesIndex[a];if(1===arguments.length&&"[object Array]"===Object.prototype.toString.call(a)){var b,c,d=[];for(b=0,c=a.length;c>b;b++){if("string"!=typeof a[b]&&"number"!=typeof a[b])throw"nodes: Wrong arguments.";d.push(this.nodesIndex[a[b]])}return d}throw"nodes: Wrong arguments."}),j.addMethod("degree",function(a,b){if(b={"in":this.inNeighborsCount,out:this.outNeighborsCount}[b||""]||this.allNeighborsCount,"string"==typeof a||"number"==typeof a)return b[a];if("[object Array]"===Object.prototype.toString.call(a)){var c,d,e=[];for(c=0,d=a.length;d>c;c++){if("string"!=typeof a[c]&&"number"!=typeof a[c])throw"degree: Wrong arguments.";e.push(b[a[c]])}return e}throw"degree: Wrong arguments."}),j.addMethod("edges",function(a){if(!arguments.length)return this.edgesArray.slice(0);if(1===arguments.length&&("string"==typeof a||"number"==typeof a))return this.edgesIndex[a];if(1===arguments.length&&"[object Array]"===Object.prototype.toString.call(a)){var b,c,d=[];for(b=0,c=a.length;c>b;b++){if("string"!=typeof a[b]&&"number"!=typeof a[b])throw"edges: Wrong arguments.";d.push(this.edgesIndex[a[b]])}return d}throw"edges: Wrong arguments."}),"undefined"!=typeof sigma?(sigma.classes=sigma.classes||Object.create(null),sigma.classes.graph=j):"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=j),exports.graph=j):this.graph=j}.call(this),function(a){"use strict";if("undefined"==typeof sigma)throw"sigma is not declared";sigma.utils.pkg("sigma.classes"),sigma.classes.camera=function(a,b,c,d){sigma.classes.dispatcher.extend(this),Object.defineProperty(this,"graph",{value:b}),Object.defineProperty(this,"id",{value:a}),Object.defineProperty(this,"readPrefix",{value:"read_cam"+a+":"}),Object.defineProperty(this,"prefix",{value:"cam"+a+":"}),this.x=0,this.y=0,this.ratio=1,this.angle=0,this.isAnimated=!1,this.settings="object"==typeof d&&d?c.embedObject(d):c},sigma.classes.camera.prototype.goTo=function(b){if(!this.settings("enableCamera"))return this;var c,d,e=b||{},f=["x","y","ratio","angle"];for(c=0,d=f.length;d>c;c++)if(e[f[c]]!==a){if("number"!=typeof e[f[c]]||isNaN(e[f[c]]))throw'Value for "'+f[c]+'" is not a number.';this[f[c]]=e[f[c]]}return this.dispatchEvent("coordinatesUpdated"),this},sigma.classes.camera.prototype.applyView=function(b,c,d){d=d||{},c=c!==a?c:this.prefix,b=b!==a?b:this.readPrefix;var e,f,g,h=d.nodes||this.graph.nodes(),i=d.edges||this.graph.edges(),j=Math.cos(this.angle),k=Math.sin(this.angle),l=Math.pow(this.ratio,this.settings("nodesPowRatio")),m=Math.pow(this.ratio,this.settings("edgesPowRatio")),n=(d.width||0)/2,o=(d.height||0)/2;for(e=0,f=h.length;f>e;e++)g=h[e],g[c+"x"]=(((g[b+"x"]||0)-this.x)*j+((g[b+"y"]||0)-this.y)*k)/this.ratio+n,g[c+"y"]=(((g[b+"y"]||0)-this.y)*j-((g[b+"x"]||0)-this.x)*k)/this.ratio+o,g[c+"size"]=(g[b+"size"]||0)/l;for(e=0,f=i.length;f>e;e++)i[e][c+"size"]=(i[e][b+"size"]||0)/m;return this},sigma.classes.camera.prototype.graphPosition=function(a,b,c){var d=0,e=0,f=Math.cos(this.angle),g=Math.sin(this.angle);return c||(d=-(this.x*f+this.y*g)/this.ratio,e=-(this.y*f-this.x*g)/this.ratio),{x:(a*f+b*g)/this.ratio+d,y:(b*f-a*g)/this.ratio+e}},sigma.classes.camera.prototype.cameraPosition=function(a,b,c){var d=0,e=0,f=Math.cos(this.angle),g=Math.sin(this.angle);return c||(d=-(this.x*f+this.y*g)/this.ratio,e=-(this.y*f-this.x*g)/this.ratio),{x:((a-d)*f-(b-e)*g)*this.ratio,y:((b-e)*f+(a-d)*g)*this.ratio}},sigma.classes.camera.prototype.getMatrix=function(){var a=sigma.utils.matrices.scale(1/this.ratio),b=sigma.utils.matrices.rotation(this.angle),c=sigma.utils.matrices.translation(-this.x,-this.y),d=sigma.utils.matrices.multiply(c,sigma.utils.matrices.multiply(b,a));return d},sigma.classes.camera.prototype.getRectangle=function(a,b){var c=this.cameraPosition(a,0,!0),d=this.cameraPosition(0,b,!0),e=this.cameraPosition(a/2,b/2,!0),f=this.cameraPosition(a/4,0,!0).x,g=this.cameraPosition(0,b/4,!0).y;return{x1:this.x-e.x-f,y1:this.y-e.y-g,x2:this.x-e.x+f+c.x,y2:this.y-e.y-g+c.y,height:Math.sqrt(Math.pow(d.x,2)+Math.pow(d.y+2*g,2))}}}.call(this),function(a){"use strict";function b(a,b){var c=b.x+b.width/2,d=b.y+b.height/2,e=a.yd;d++)a.x2>=b[d][0].x&&a.x1<=b[d][1].x&&a.y1+a.height>=b[d][0].y&&a.y1<=b[d][2].y&&c.push(d);return c}function d(a,b){for(var c=[],d=0;4>d;d++)j.collision(a,b[d])&&c.push(d);return c}function e(a,b){var c,d,e=b.level+1,f=Math.round(b.bounds.width/2),g=Math.round(b.bounds.height/2),h=Math.round(b.bounds.x),j=Math.round(b.bounds.y);switch(a){case 0:c=h,d=j;break;case 1:c=h+f,d=j;break;case 2:c=h,d=j+g;break;case 3:c=h+f,d=j+g}return i({x:c,y:d,width:f,height:g},e,b.maxElements,b.maxLevel)}function f(b,d,g){if(g.leveli;i++)g.nodes[h[i]]===a&&(g.nodes[h[i]]=e(h[i],g)),f(b,d,g.nodes[h[i]]);else g.elements.push(b)}function g(c,d){if(d.levelg;g++)c.nodes[f[g]]!==a&&h(b,c.nodes[f[g]],d,e);else for(var j=0,k=c.elements.length;k>j;j++)e[c.elements[j].id]===a&&(e[c.elements[j].id]=c.elements[j]);return e}function i(a,b,c,d){return{level:b||0,bounds:a,corners:j.splitSquare(a),maxElements:c||20,maxLevel:d||4,elements:[],nodes:[]}}var j={pointToSquare:function(a){return{x1:a.x-a.size,y1:a.y-a.size,x2:a.x+a.size,y2:a.y-a.size,height:2*a.size}},isAxisAligned:function(a){return a.x1===a.x2||a.y1===a.y2},axisAlignedTopPoints:function(a){return a.y1===a.y2&&a.x1a.y1?{x1:a.x1-a.height,y1:a.y1,x2:a.x1,y2:a.y1,height:a.height}:a.x1===a.x2&&a.y2f;f++){var g=this.projection(b[f],a),h=this.projection(c[f],a);d.push(g.x*a.x+g.y*a.y),e.push(h.x*a.x+h.y*a.y)}var i=Math.max.apply(Math,d),j=Math.max.apply(Math,e),k=Math.min.apply(Math,d),l=Math.min.apply(Math,e);return i>=l&&j>=k},collision:function(a,b){for(var c=this.axis(a,b),d=!0,e=0;4>e;e++)d=d&&this.axisCollision(c[e],a,b);return d}},k=function(){this._geom=j,this._tree=null,this._cache={query:!1,result:!1}};k.prototype.index=function(a,b){if(!b.bounds)throw"sigma.classes.quad.index: bounds information not given.";var c=b.prefix||"";this._tree=i(b.bounds,0,b.maxElements,b.maxLevel);for(var d=0,e=a.length;e>d;d++)f(a[d],j.pointToSquare({x:a[d][c+"x"],y:a[d][c+"y"],size:a[d][c+"size"]}),this._tree);return this._cache={query:!1,result:!1},this._tree},k.prototype.point=function(a,b){return this._tree?g({x:a,y:b},this._tree)||[]:[]},k.prototype.area=function(a){var b,e,f=JSON.stringify(a);if(this._cache.query===f)return this._cache.result;j.isAxisAligned(a)?(b=c,e=j.axisAlignedTopPoints(a)):(b=d,e=j.rectangleCorners(a));var g=this._tree?h(e,this._tree,b):[],i=[];for(var k in g)i.push(g[k]);return this._cache.query=f,this._cache.result=i,i},"undefined"!=typeof this.sigma?(this.sigma.classes=this.sigma.classes||{},this.sigma.classes.quad=k):"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=k),exports.quad=k):this.quad=k}.call(this),function(a){"use strict";function b(a,b){var c=b.x+b.width/2,d=b.y+b.height/2,e=a.yd;d++)a.x2>=b[d][0].x&&a.x1<=b[d][1].x&&a.y1+a.height>=b[d][0].y&&a.y1<=b[d][2].y&&c.push(d);return c}function d(a,b){for(var c=[],d=0;4>d;d++)j.collision(a,b[d])&&c.push(d);return c}function e(a,b){var c,d,e=b.level+1,f=Math.round(b.bounds.width/2),g=Math.round(b.bounds.height/2),h=Math.round(b.bounds.x),j=Math.round(b.bounds.y);switch(a){case 0:c=h,d=j;break;case 1:c=h+f,d=j;break;case 2:c=h,d=j+g;break;case 3:c=h+f,d=j+g}return i({x:c,y:d,width:f,height:g},e,b.maxElements,b.maxLevel)}function f(b,d,g){if(g.leveli;i++)g.nodes[h[i]]===a&&(g.nodes[h[i]]=e(h[i],g)),f(b,d,g.nodes[h[i]]);else g.elements.push(b)}function g(c,d){if(d.levelg;g++)c.nodes[f[g]]!==a&&h(b,c.nodes[f[g]],d,e);else for(var j=0,k=c.elements.length;k>j;j++)e[c.elements[j].id]===a&&(e[c.elements[j].id]=c.elements[j]);return e}function i(a,b,c,d){return{level:b||0,bounds:a,corners:j.splitSquare(a),maxElements:c||40,maxLevel:d||8,elements:[],nodes:[]}}var j={pointToSquare:function(a){return{x1:a.x-a.size,y1:a.y-a.size,x2:a.x+a.size,y2:a.y-a.size,height:2*a.size}},lineToSquare:function(a){return a.y1a.y1?{x1:a.x1-a.height,y1:a.y1,x2:a.x1,y2:a.y1,height:a.height}:a.x1===a.x2&&a.y2f;f++){var g=this.projection(b[f],a),h=this.projection(c[f],a);d.push(g.x*a.x+g.y*a.y),e.push(h.x*a.x+h.y*a.y)}var i=Math.max.apply(Math,d),j=Math.max.apply(Math,e),k=Math.min.apply(Math,d),l=Math.min.apply(Math,e);return i>=l&&j>=k},collision:function(a,b){for(var c=this.axis(a,b),d=!0,e=0;4>e;e++)d=d&&this.axisCollision(c[e],a,b);return d}},k=function(){this._geom=j,this._tree=null,this._cache={query:!1,result:!1},this._enabled=!0};k.prototype.index=function(a,b){if(!this._enabled)return this._tree;if(!b.bounds)throw"sigma.classes.edgequad.index: bounds information not given.";var c,d,e,g,h,k=b.prefix||"";this._tree=i(b.bounds,0,b.maxElements,b.maxLevel);for(var l=a.edges(),m=0,n=l.length;n>m;m++)d=a.nodes(l[m].source),e=a.nodes(l[m].target),h={x1:d[k+"x"],y1:d[k+"y"],x2:e[k+"x"],y2:e[k+"y"],size:l[m][k+"size"]||0},"curve"===l[m].type||"curvedArrow"===l[m].type?d.id===e.id?(g={x:d[k+"x"],y:d[k+"y"],size:d[k+"size"]||0},f(l[m],j.selfLoopToSquare(g),this._tree)):(c=sigma.utils.getQuadraticControlPoint(h.x1,h.y1,h.x2,h.y2),f(l[m],j.quadraticCurveToSquare(h,c),this._tree)):f(l[m],j.lineToSquare(h),this._tree);return this._cache={query:!1,result:!1},this._tree},k.prototype.point=function(a,b){return this._enabled&&this._tree?g({x:a,y:b},this._tree)||[]:[]},k.prototype.area=function(a){if(!this._enabled)return[];var b,e,f=JSON.stringify(a);if(this._cache.query===f)return this._cache.result;j.isAxisAligned(a)?(b=c,e=j.axisAlignedTopPoints(a)):(b=d,e=j.rectangleCorners(a));var g=this._tree?h(e,this._tree,b):[],i=[];for(var k in g)i.push(g[k]);return this._cache.query=f,this._cache.result=i,i},"undefined"!=typeof this.sigma?(this.sigma.classes=this.sigma.classes||{},this.sigma.classes.edgequad=k):"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=k),exports.edgequad=k):this.edgequad=k}.call(this),function(){"use strict";if("undefined"==typeof sigma)throw"sigma is not declared";sigma.utils.pkg("sigma.captors"),sigma.captors.mouse=function(a,b,c){function d(a){var b,c,d;return y("mouseEnabled")&&(v.dispatchEvent("mousemove",sigma.utils.mouseCoords(a)),q)?(r=!0,s=!0,u&&clearTimeout(u),u=setTimeout(function(){r=!1},y("dragTimeout")),sigma.misc.animation.killAll(x),x.isMoving=!0,d=x.cameraPosition(sigma.utils.getX(a)-o,sigma.utils.getY(a)-p,!0),b=k-d.x,c=l-d.y,(b!==x.x||c!==x.y)&&(m=x.x,n=x.y,x.goTo({x:b,y:c})),a.preventDefault?a.preventDefault():a.returnValue=!1,a.stopPropagation(),!1):void 0}function e(a){if(y("mouseEnabled")&&q){q=!1,u&&clearTimeout(u),x.isMoving=!1;var b=sigma.utils.getX(a),c=sigma.utils.getY(a);r?(sigma.misc.animation.killAll(x),sigma.misc.animation.camera(x,{x:x.x+y("mouseInertiaRatio")*(x.x-m),y:x.y+y("mouseInertiaRatio")*(x.y-n)},{easing:"quadraticOut",duration:y("mouseInertiaDuration")})):(o!==b||p!==c)&&x.goTo({x:x.x,y:x.y}),v.dispatchEvent("mouseup",sigma.utils.mouseCoords(a)),r=!1}}function f(a){if(y("mouseEnabled"))switch(k=x.x,l=x.y,m=x.x,n=x.y,o=sigma.utils.getX(a),p=sigma.utils.getY(a),s=!1,t=(new Date).getTime(),a.which){case 2:break;case 3:v.dispatchEvent("rightclick",sigma.utils.mouseCoords(a,o,p));break;default:q=!0,v.dispatchEvent("mousedown",sigma.utils.mouseCoords(a,o,p))}}function g(){y("mouseEnabled")&&v.dispatchEvent("mouseout")}function h(a){if(y("mouseEnabled")){var b=sigma.utils.mouseCoords(a);b.isDragging=(new Date).getTime()-t>100&&s,v.dispatchEvent("click",b)}return a.preventDefault?a.preventDefault():a.returnValue=!1,a.stopPropagation(),!1}function i(a){var b,c,d;return y("mouseEnabled")?(c=1/y("doubleClickZoomingRatio"),v.dispatchEvent("doubleclick",sigma.utils.mouseCoords(a,o,p)),y("doubleClickEnabled")&&(b=x.cameraPosition(sigma.utils.getX(a)-sigma.utils.getCenter(a).x,sigma.utils.getY(a)-sigma.utils.getCenter(a).y,!0),d={duration:y("doubleClickZoomDuration")},sigma.utils.zoomTo(x,b.x,b.y,c,d)),a.preventDefault?a.preventDefault():a.returnValue=!1,a.stopPropagation(),!1):void 0}function j(a){var b,c,d;return y("mouseEnabled")&&y("mouseWheelEnabled")?(c=sigma.utils.getDelta(a)>0?1/y("zoomingRatio"):y("zoomingRatio"),b=x.cameraPosition(sigma.utils.getX(a)-sigma.utils.getCenter(a).x,sigma.utils.getY(a)-sigma.utils.getCenter(a).y,!0),d={duration:y("mouseZoomDuration")},sigma.utils.zoomTo(x,b.x,b.y,c,d),a.preventDefault?a.preventDefault():a.returnValue=!1,a.stopPropagation(),!1):void 0}var k,l,m,n,o,p,q,r,s,t,u,v=this,w=a,x=b,y=c;sigma.classes.dispatcher.extend(this),sigma.utils.doubleClick(w,"click",i),w.addEventListener("DOMMouseScroll",j,!1),w.addEventListener("mousewheel",j,!1),w.addEventListener("mousemove",d,!1),w.addEventListener("mousedown",f,!1),w.addEventListener("click",h,!1),w.addEventListener("mouseout",g,!1),document.addEventListener("mouseup",e,!1),this.kill=function(){sigma.utils.unbindDoubleClick(w,"click"),w.removeEventListener("DOMMouseScroll",j),w.removeEventListener("mousewheel",j),w.removeEventListener("mousemove",d),w.removeEventListener("mousedown",f),w.removeEventListener("click",h),w.removeEventListener("mouseout",g),document.removeEventListener("mouseup",e)}}}.call(this),function(){"use strict";if("undefined"==typeof sigma)throw"sigma is not declared";sigma.utils.pkg("sigma.captors"),sigma.captors.touch=function(a,b,c){function d(a){var b=sigma.utils.getOffset(B);return{x:a.pageX-b.left,y:a.pageY-b.top}}function e(a){if(D("touchEnabled")){var b,c,e,f,g,h;switch(E=a.touches,E.length){case 1:C.isMoving=!0,w=1,i=C.x,j=C.y,m=C.x,n=C.y,g=d(E[0]),q=g.x,r=g.y;break;case 2:return C.isMoving=!0,w=2,g=d(E[0]),h=d(E[1]),b=g.x,e=g.y,c=h.x,f=h.y,m=C.x,n=C.y,k=C.angle,l=C.ratio,i=C.x,j=C.y,q=b,r=e,s=c,t=f,u=Math.atan2(t-r,s-q),v=Math.sqrt((t-r)*(t-r)+(s-q)*(s-q)),a.preventDefault(),!1}}}function f(a){if(D("touchEnabled")){E=a.touches;var b=D("touchInertiaRatio");switch(z&&(x=!1,clearTimeout(z)),w){case 2:if(1===a.touches.length){e(a),a.preventDefault();break}case 1:C.isMoving=!1,A.dispatchEvent("stopDrag"),x&&(y=!1,sigma.misc.animation.camera(C,{x:C.x+b*(C.x-m),y:C.y+b*(C.y-n)},{easing:"quadraticOut",duration:D("touchInertiaDuration")})),x=!1,w=0}}}function g(a){if(!y&&D("touchEnabled")){var b,c,e,f,g,h,B,F,G,H,I,J,K,L,M,N,O;switch(E=a.touches,x=!0,z&&clearTimeout(z),z=setTimeout(function(){x=!1},D("dragTimeout")),w){case 1:F=d(E[0]),b=F.x,e=F.y,H=C.cameraPosition(b-q,e-r,!0),L=i-H.x,M=j-H.y,(L!==C.x||M!==C.y)&&(m=C.x,n=C.y,C.goTo({x:L,y:M}),A.dispatchEvent("mousemove",sigma.utils.mouseCoords(a,F.x,F.y)),A.dispatchEvent("drag"));break;case 2:F=d(E[0]),G=d(E[1]),b=F.x,e=F.y,c=G.x,f=G.y,I=C.cameraPosition((q+s)/2-sigma.utils.getCenter(a).x,(r+t)/2-sigma.utils.getCenter(a).y,!0),B=C.cameraPosition((b+c)/2-sigma.utils.getCenter(a).x,(e+f)/2-sigma.utils.getCenter(a).y,!0),J=Math.atan2(f-e,c-b)-u,K=Math.sqrt((f-e)*(f-e)+(c-b)*(c-b))/v,b=I.x,e=I.y,N=l/K,b*=K,e*=K,O=k-J,g=Math.cos(-J),h=Math.sin(-J),c=b*g+e*h,f=e*g-b*h,b=c,e=f,L=b-B.x+i,M=e-B.y+j,(N!==C.ratio||O!==C.angle||L!==C.x||M!==C.y)&&(m=C.x,n=C.y,o=C.angle,p=C.ratio,C.goTo({x:L,y:M,angle:O,ratio:N}),A.dispatchEvent("drag"))}return a.preventDefault(),!1}}function h(a){var b,c,e;return a.touches&&1===a.touches.length&&D("touchEnabled")?(y=!0,c=1/D("doubleClickZoomingRatio"),b=d(a.touches[0]),A.dispatchEvent("doubleclick",sigma.utils.mouseCoords(a,b.x,b.y)),D("doubleClickEnabled")&&(b=C.cameraPosition(b.x-sigma.utils.getCenter(a).x,b.y-sigma.utils.getCenter(a).y,!0),e={duration:D("doubleClickZoomDuration"),onComplete:function(){y=!1}},sigma.utils.zoomTo(C,b.x,b.y,c,e)),a.preventDefault?a.preventDefault():a.returnValue=!1,a.stopPropagation(),!1):void 0}var i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A=this,B=a,C=b,D=c,E=[];sigma.classes.dispatcher.extend(this),sigma.utils.doubleClick(B,"touchstart",h),B.addEventListener("touchstart",e,!1),B.addEventListener("touchend",f,!1),B.addEventListener("touchcancel",f,!1),B.addEventListener("touchleave",f,!1),B.addEventListener("touchmove",g,!1),this.kill=function(){sigma.utils.unbindDoubleClick(B,"touchstart"),B.addEventListener("touchstart",e),B.addEventListener("touchend",f),B.addEventListener("touchcancel",f),B.addEventListener("touchleave",f),B.addEventListener("touchmove",g)}}}.call(this),function(a){"use strict";if("undefined"==typeof sigma)throw"sigma is not declared";if("undefined"==typeof conrad)throw"conrad is not declared";sigma.utils.pkg("sigma.renderers"),sigma.renderers.canvas=function(a,b,c,d){if("object"!=typeof d)throw"sigma.renderers.canvas: Wrong arguments.";if(!(d.container instanceof HTMLElement))throw"Container not found.";var e,f,g,h;for(sigma.classes.dispatcher.extend(this),Object.defineProperty(this,"conradId",{value:sigma.utils.id()}),this.graph=a,this.camera=b,this.contexts={},this.domElements={},this.options=d,this.container=this.options.container,this.settings="object"==typeof d.settings&&d.settings?c.embedObjects(d.settings):c,this.nodesOnScreen=[],this.edgesOnScreen=[],this.jobs={},this.options.prefix="renderer"+this.conradId+":",this.settings("batchEdgesDrawing")?(this.initDOM("canvas","edges"),this.initDOM("canvas","scene"),this.contexts.nodes=this.contexts.scene,this.contexts.labels=this.contexts.scene):(this.initDOM("canvas","scene"),this.contexts.edges=this.contexts.scene,this.contexts.nodes=this.contexts.scene,this.contexts.labels=this.contexts.scene),this.initDOM("canvas","mouse"),this.contexts.hover=this.contexts.mouse,this.captors=[],g=this.options.captors||[sigma.captors.mouse,sigma.captors.touch],e=0,f=g.length;f>e;e++)h="function"==typeof g[e]?g[e]:sigma.captors[g[e]],this.captors.push(new h(this.domElements.mouse,this.camera,this.settings));sigma.misc.bindEvents.call(this,this.options.prefix),sigma.misc.drawHovers.call(this,this.options.prefix),this.resize(!1)},sigma.renderers.canvas.prototype.render=function(b){b=b||{};var c,d,e,f,g,h,i,j,k,l,m,n,o,p={},q=this.graph,r=this.graph.nodes,s=(this.options.prefix||"",this.settings(b,"drawEdges")),t=this.settings(b,"drawNodes"),u=this.settings(b,"drawLabels"),v=this.settings(b,"drawEdgeLabels"),w=this.settings.embedObjects(b,{prefix:this.options.prefix});this.resize(!1),this.settings(b,"hideEdgesOnMove")&&(this.camera.isAnimated||this.camera.isMoving)&&(s=!1),this.camera.applyView(a,this.options.prefix,{width:this.width,height:this.height}),this.clear();for(e in this.jobs)conrad.hasJob(e)&&conrad.killJob(e);for(this.edgesOnScreen=[],this.nodesOnScreen=this.camera.quadtree.area(this.camera.getRectangle(this.width,this.height)),c=this.nodesOnScreen,d=0,f=c.length;f>d;d++)p[c[d].id]=c[d];if(s){for(c=q.edges(),d=0,f=c.length;f>d;d++)g=c[d],!p[g.source]&&!p[g.target]||g.hidden||r(g.source).hidden||r(g.target).hidden||this.edgesOnScreen.push(g);if(this.settings(b,"batchEdgesDrawing"))h="edges_"+this.conradId,n=w("canvasEdgesBatchSize"),l=this.edgesOnScreen,f=l.length,k=0,i=Math.min(l.length,k+n),j=function(){for(o=this.contexts.edges.globalCompositeOperation,this.contexts.edges.globalCompositeOperation="destination-over",m=sigma.canvas.edges,d=k;i>d;d++)g=l[d],(m[g.type||this.settings(b,"defaultEdgeType")]||m.def)(g,q.nodes(g.source),q.nodes(g.target),this.contexts.edges,w);if(v)for(m=sigma.canvas.edges.labels,d=k;i>d;d++)g=l[d],g.hidden||(m[g.type||this.settings(b,"defaultEdgeType")]||m.def)(g,q.nodes(g.source),q.nodes(g.target),this.contexts.labels,w);return this.contexts.edges.globalCompositeOperation=o,i===l.length?(delete this.jobs[h],!1):(k=i+1,i=Math.min(l.length,k+n),!0)},this.jobs[h]=j,conrad.addJob(h,j.bind(this));else{for(m=sigma.canvas.edges,c=this.edgesOnScreen,d=0,f=c.length;f>d;d++)g=c[d],(m[g.type||this.settings(b,"defaultEdgeType")]||m.def)(g,q.nodes(g.source),q.nodes(g.target),this.contexts.edges,w);if(v)for(m=sigma.canvas.edges.labels,c=this.edgesOnScreen,d=0,f=c.length;f>d;d++)c[d].hidden||(m[c[d].type||this.settings(b,"defaultEdgeType")]||m.def)(c[d],q.nodes(c[d].source),q.nodes(c[d].target),this.contexts.labels,w)}}if(t)for(m=sigma.canvas.nodes,c=this.nodesOnScreen,d=0,f=c.length;f>d;d++)c[d].hidden||(m[c[d].type||this.settings(b,"defaultNodeType")]||m.def)(c[d],this.contexts.nodes,w);if(u)for(m=sigma.canvas.labels,c=this.nodesOnScreen,d=0,f=c.length;f>d;d++)c[d].hidden||(m[c[d].type||this.settings(b,"defaultNodeType")]||m.def)(c[d],this.contexts.labels,w);return this.dispatchEvent("render"),this},sigma.renderers.canvas.prototype.initDOM=function(a,b){var c=document.createElement(a);c.style.position="absolute",c.setAttribute("class","sigma-"+b),this.domElements[b]=c,this.container.appendChild(c),"canvas"===a.toLowerCase()&&(this.contexts[b]=c.getContext("2d"))},sigma.renderers.canvas.prototype.resize=function(b,c){var d,e=this.width,f=this.height,g=sigma.utils.getPixelRatio();if(b!==a&&c!==a?(this.width=b,this.height=c):(this.width=this.container.offsetWidth,this.height=this.container.offsetHeight,b=this.width,c=this.height),e!==this.width||f!==this.height)for(d in this.domElements)this.domElements[d].style.width=b+"px",this.domElements[d].style.height=c+"px","canvas"===this.domElements[d].tagName.toLowerCase()&&(this.domElements[d].setAttribute("width",b*g+"px"),this.domElements[d].setAttribute("height",c*g+"px"),1!==g&&this.contexts[d].scale(g,g));return this},sigma.renderers.canvas.prototype.clear=function(){for(var a in this.contexts)this.contexts[a].clearRect(0,0,this.width,this.height);return this},sigma.renderers.canvas.prototype.kill=function(){for(var a,b;b=this.captors.pop();)b.kill();delete this.captors;for(a in this.domElements)this.domElements[a].parentNode.removeChild(this.domElements[a]),delete this.domElements[a],delete this.contexts[a];delete this.domElements,delete this.contexts},sigma.utils.pkg("sigma.canvas.nodes"),sigma.utils.pkg("sigma.canvas.edges"),sigma.utils.pkg("sigma.canvas.labels")}.call(this),function(a){"use strict";if("undefined"==typeof sigma)throw"sigma is not declared";sigma.utils.pkg("sigma.renderers"),sigma.renderers.webgl=function(a,b,c,d){if("object"!=typeof d)throw"sigma.renderers.webgl: Wrong arguments.";if(!(d.container instanceof HTMLElement))throw"Container not found.";var e,f,g,h;for(sigma.classes.dispatcher.extend(this),this.jobs={},Object.defineProperty(this,"conradId",{value:sigma.utils.id()}),this.graph=a,this.camera=b,this.contexts={},this.domElements={},this.options=d,this.container=this.options.container,this.settings="object"==typeof d.settings&&d.settings?c.embedObjects(d.settings):c,this.options.prefix=this.camera.readPrefix,Object.defineProperty(this,"nodePrograms",{value:{}}),Object.defineProperty(this,"edgePrograms",{value:{}}),Object.defineProperty(this,"nodeFloatArrays",{value:{}}),Object.defineProperty(this,"edgeFloatArrays",{value:{}}),Object.defineProperty(this,"edgeIndicesArrays",{value:{}}),this.settings(d,"batchEdgesDrawing")?(this.initDOM("canvas","edges",!0),this.initDOM("canvas","nodes",!0)):(this.initDOM("canvas","scene",!0),this.contexts.nodes=this.contexts.scene,this.contexts.edges=this.contexts.scene),this.initDOM("canvas","labels"),this.initDOM("canvas","mouse"),this.contexts.hover=this.contexts.mouse,this.captors=[],g=this.options.captors||[sigma.captors.mouse,sigma.captors.touch],e=0,f=g.length;f>e;e++)h="function"==typeof g[e]?g[e]:sigma.captors[g[e]],this.captors.push(new h(this.domElements.mouse,this.camera,this.settings));sigma.misc.bindEvents.call(this,this.camera.prefix),sigma.misc.drawHovers.call(this,this.camera.prefix),this.resize()},sigma.renderers.webgl.prototype.process=function(){var a,b,c,d,e,f,g=this.graph,h=sigma.utils.extend(h,this.options),i=this.settings(h,"defaultEdgeType"),j=this.settings(h,"defaultNodeType");for(d in this.nodeFloatArrays)delete this.nodeFloatArrays[d];for(d in this.edgeFloatArrays)delete this.edgeFloatArrays[d];for(d in this.edgeIndicesArrays)delete this.edgeIndicesArrays[d];for(a=g.edges(),b=0,c=a.length;c>b;b++)e=a[b].type||i,d=e&&sigma.webgl.edges[e]?e:"def",this.edgeFloatArrays[d]||(this.edgeFloatArrays[d]={edges:[]}),this.edgeFloatArrays[d].edges.push(a[b]);for(a=g.nodes(),b=0,c=a.length;c>b;b++)e=a[b].type||j,d=e&&sigma.webgl.nodes[e]?e:"def",this.nodeFloatArrays[d]||(this.nodeFloatArrays[d]={nodes:[]}),this.nodeFloatArrays[d].nodes.push(a[b]);for(d in this.edgeFloatArrays){for(f=sigma.webgl.edges[d],a=this.edgeFloatArrays[d].edges,this.edgeFloatArrays[d].array=new Float32Array(a.length*f.POINTS*f.ATTRIBUTES),b=0,c=a.length;c>b;b++)a[b].hidden||g.nodes(a[b].source).hidden||g.nodes(a[b].target).hidden||f.addEdge(a[b],g.nodes(a[b].source),g.nodes(a[b].target),this.edgeFloatArrays[d].array,b*f.POINTS*f.ATTRIBUTES,h.prefix,this.settings);"function"==typeof f.computeIndices&&(this.edgeIndicesArrays[d]=f.computeIndices(this.edgeFloatArrays[d].array))}for(d in this.nodeFloatArrays)for(f=sigma.webgl.nodes[d],a=this.nodeFloatArrays[d].nodes,this.nodeFloatArrays[d].array=new Float32Array(a.length*f.POINTS*f.ATTRIBUTES),b=0,c=a.length;c>b;b++)this.nodeFloatArrays[d].array||(this.nodeFloatArrays[d].array=new Float32Array(a.length*f.POINTS*f.ATTRIBUTES)),a[b].hidden||f.addNode(a[b],this.nodeFloatArrays[d].array,b*f.POINTS*f.ATTRIBUTES,h.prefix,this.settings);return this},sigma.renderers.webgl.prototype.render=function(b){var c,d,e,f,g,h,i=this,j=(this.graph,this.contexts.nodes),k=this.contexts.edges,l=this.camera.getMatrix(),m=sigma.utils.extend(b,this.options),n=this.settings(m,"drawLabels"),o=this.settings(m,"drawEdges"),p=this.settings(m,"drawNodes");this.resize(!1),this.settings(m,"hideEdgesOnMove")&&(this.camera.isAnimated||this.camera.isMoving)&&(o=!1),this.clear(),l=sigma.utils.matrices.multiply(l,sigma.utils.matrices.translation(this.width/2,this.height/2));for(f in this.jobs)conrad.hasJob(f)&&conrad.killJob(f);if(o)if(this.settings(m,"batchEdgesDrawing"))(function(){var a,b,c,d,e,f,g,h,i,j;c="edges_"+this.conradId,j=this.settings(m,"webglEdgesBatchSize"),a=Object.keys(this.edgeFloatArrays),a.length&&(b=0,i=sigma.webgl.edges[a[b]],e=this.edgeFloatArrays[a[b]].array,h=this.edgeIndicesArrays[a[b]],g=0,f=Math.min(g+j*i.POINTS,e.length/i.ATTRIBUTES),d=function(){return this.edgePrograms[a[b]]||(this.edgePrograms[a[b]]=i.initProgram(k)),f>g&&(k.useProgram(this.edgePrograms[a[b]]),i.render(k,this.edgePrograms[a[b]],e,{settings:this.settings,matrix:l,width:this.width,height:this.height,ratio:this.camera.ratio,scalingRatio:this.settings(m,"webglOversamplingRatio"),start:g,count:f-g,indicesData:h})),f>=e.length/i.ATTRIBUTES&&b===a.length-1?(delete this.jobs[c],!1):(f>=e.length/i.ATTRIBUTES?(b++,e=this.edgeFloatArrays[a[b]].array,i=sigma.webgl.edges[a[b]],g=0,f=Math.min(g+j*i.POINTS,e.length/i.ATTRIBUTES)):(g=f,f=Math.min(g+j*i.POINTS,e.length/i.ATTRIBUTES)),!0)},this.jobs[c]=d,conrad.addJob(c,d.bind(this)))}).call(this);else for(f in this.edgeFloatArrays)h=sigma.webgl.edges[f],this.edgePrograms[f]||(this.edgePrograms[f]=h.initProgram(k)),this.edgeFloatArrays[f]&&(k.useProgram(this.edgePrograms[f]),h.render(k,this.edgePrograms[f],this.edgeFloatArrays[f].array,{settings:this.settings,matrix:l,width:this.width,height:this.height,ratio:this.camera.ratio,scalingRatio:this.settings(m,"webglOversamplingRatio"),indicesData:this.edgeIndicesArrays[f]}));
4 | if(p){j.blendFunc(j.SRC_ALPHA,j.ONE_MINUS_SRC_ALPHA),j.enable(j.BLEND);for(f in this.nodeFloatArrays)h=sigma.webgl.nodes[f],this.nodePrograms[f]||(this.nodePrograms[f]=h.initProgram(j)),this.nodeFloatArrays[f]&&(j.useProgram(this.nodePrograms[f]),h.render(j,this.nodePrograms[f],this.nodeFloatArrays[f].array,{settings:this.settings,matrix:l,width:this.width,height:this.height,ratio:this.camera.ratio,scalingRatio:this.settings(m,"webglOversamplingRatio")}))}if(n)for(c=this.camera.quadtree.area(this.camera.getRectangle(this.width,this.height)),this.camera.applyView(a,a,{nodes:c,edges:[],width:this.width,height:this.height}),g=function(a){return i.settings({prefix:i.camera.prefix},a)},d=0,e=c.length;e>d;d++)c[d].hidden||(sigma.canvas.labels[c[d].type||this.settings(m,"defaultNodeType")]||sigma.canvas.labels.def)(c[d],this.contexts.labels,g);return this.dispatchEvent("render"),this},sigma.renderers.webgl.prototype.initDOM=function(a,b,c){var d=document.createElement(a),e=this;d.style.position="absolute",d.setAttribute("class","sigma-"+b),this.domElements[b]=d,this.container.appendChild(d),"canvas"===a.toLowerCase()&&(this.contexts[b]=d.getContext(c?"experimental-webgl":"2d",{preserveDrawingBuffer:!0}),c&&(d.addEventListener("webglcontextlost",function(a){a.preventDefault()},!1),d.addEventListener("webglcontextrestored",function(){e.render()},!1)))},sigma.renderers.webgl.prototype.resize=function(b,c){var d,e=this.width,f=this.height,g=sigma.utils.getPixelRatio();if(b!==a&&c!==a?(this.width=b,this.height=c):(this.width=this.container.offsetWidth,this.height=this.container.offsetHeight,b=this.width,c=this.height),e!==this.width||f!==this.height)for(d in this.domElements)this.domElements[d].style.width=b+"px",this.domElements[d].style.height=c+"px","canvas"===this.domElements[d].tagName.toLowerCase()&&(this.contexts[d]&&this.contexts[d].scale?(this.domElements[d].setAttribute("width",b*g+"px"),this.domElements[d].setAttribute("height",c*g+"px"),1!==g&&this.contexts[d].scale(g,g)):(this.domElements[d].setAttribute("width",b*this.settings("webglOversamplingRatio")+"px"),this.domElements[d].setAttribute("height",c*this.settings("webglOversamplingRatio")+"px")));for(d in this.contexts)this.contexts[d]&&this.contexts[d].viewport&&this.contexts[d].viewport(0,0,this.width*this.settings("webglOversamplingRatio"),this.height*this.settings("webglOversamplingRatio"));return this},sigma.renderers.webgl.prototype.clear=function(){return this.contexts.labels.clearRect(0,0,this.width,this.height),this.contexts.nodes.clear(this.contexts.nodes.COLOR_BUFFER_BIT),this.contexts.edges.clear(this.contexts.edges.COLOR_BUFFER_BIT),this},sigma.renderers.webgl.prototype.kill=function(){for(var a,b;b=this.captors.pop();)b.kill();delete this.captors;for(a in this.domElements)this.domElements[a].parentNode.removeChild(this.domElements[a]),delete this.domElements[a],delete this.contexts[a];delete this.domElements,delete this.contexts},sigma.utils.pkg("sigma.webgl.nodes"),sigma.utils.pkg("sigma.webgl.edges"),sigma.utils.pkg("sigma.canvas.labels")}.call(this),function(a){"use strict";if("undefined"==typeof sigma)throw"sigma is not declared";if("undefined"==typeof conrad)throw"conrad is not declared";sigma.utils.pkg("sigma.renderers"),sigma.renderers.svg=function(a,b,c,d){if("object"!=typeof d)throw"sigma.renderers.svg: Wrong arguments.";if(!(d.container instanceof HTMLElement))throw"Container not found.";var e,f,g,h,i=this;for(sigma.classes.dispatcher.extend(this),this.graph=a,this.camera=b,this.domElements={graph:null,groups:{},nodes:{},edges:{},labels:{},hovers:{}},this.measurementCanvas=null,this.options=d,this.container=this.options.container,this.settings="object"==typeof d.settings&&d.settings?c.embedObjects(d.settings):c,this.settings("freeStyle",!!this.options.freeStyle),this.settings("xmlns","http://www.w3.org/2000/svg"),this.nodesOnScreen=[],this.edgesOnScreen=[],this.options.prefix="renderer"+sigma.utils.id()+":",this.initDOM("svg"),this.captors=[],g=this.options.captors||[sigma.captors.mouse,sigma.captors.touch],e=0,f=g.length;f>e;e++)h="function"==typeof g[e]?g[e]:sigma.captors[g[e]],this.captors.push(new h(this.domElements.graph,this.camera,this.settings));window.addEventListener("resize",function(){i.resize()}),sigma.misc.bindDOMEvents.call(this,this.domElements.graph),this.bindHovers(this.options.prefix),this.resize(!1)},sigma.renderers.svg.prototype.render=function(b){b=b||{};var c,d,e,f,g,h,i,j,k,l={},m=this.graph,n=this.graph.nodes,o=(this.options.prefix||"",this.settings(b,"drawEdges")),p=this.settings(b,"drawNodes"),q=(this.settings(b,"drawLabels"),this.settings.embedObjects(b,{prefix:this.options.prefix,forceLabels:this.options.forceLabels}));for(this.settings(b,"hideEdgesOnMove")&&(this.camera.isAnimated||this.camera.isMoving)&&(o=!1),this.camera.applyView(a,this.options.prefix,{width:this.width,height:this.height}),this.hideDOMElements(this.domElements.nodes),this.hideDOMElements(this.domElements.edges),this.hideDOMElements(this.domElements.labels),this.edgesOnScreen=[],this.nodesOnScreen=this.camera.quadtree.area(this.camera.getRectangle(this.width,this.height)),c=this.nodesOnScreen,d=0,f=c.length;f>d;d++)l[c[d].id]=c[d];for(c=m.edges(),d=0,f=c.length;f>d;d++)g=c[d],!l[g.source]&&!l[g.target]||g.hidden||n(g.source).hidden||n(g.target).hidden||this.edgesOnScreen.push(g);if(j=sigma.svg.nodes,k=sigma.svg.labels,p)for(c=this.nodesOnScreen,d=0,f=c.length;f>d;d++)c[d].hidden||this.domElements.nodes[c[d].id]||(e=(j[c[d].type]||j.def).create(c[d],q),this.domElements.nodes[c[d].id]=e,this.domElements.groups.nodes.appendChild(e),e=(k[c[d].type]||k.def).create(c[d],q),this.domElements.labels[c[d].id]=e,this.domElements.groups.labels.appendChild(e));if(p)for(c=this.nodesOnScreen,d=0,f=c.length;f>d;d++)c[d].hidden||((j[c[d].type]||j.def).update(c[d],this.domElements.nodes[c[d].id],q),(k[c[d].type]||k.def).update(c[d],this.domElements.labels[c[d].id],q));if(j=sigma.svg.edges,o)for(c=this.edgesOnScreen,d=0,f=c.length;f>d;d++)this.domElements.edges[c[d].id]||(h=n(c[d].source),i=n(c[d].target),e=(j[c[d].type]||j.def).create(c[d],h,i,q),this.domElements.edges[c[d].id]=e,this.domElements.groups.edges.appendChild(e));if(o)for(c=this.edgesOnScreen,d=0,f=c.length;f>d;d++)h=n(c[d].source),i=n(c[d].target),(j[c[d].type]||j.def).update(c[d],this.domElements.edges[c[d].id],h,i,q);return this.dispatchEvent("render"),this},sigma.renderers.svg.prototype.initDOM=function(a){var b,c,d,e=document.createElementNS(this.settings("xmlns"),a),f=this.settings("classPrefix");e.style.position="absolute",e.setAttribute("class",f+"-svg"),e.setAttribute("xmlns",this.settings("xmlns")),e.setAttribute("xmlns:xlink","http://www.w3.org/1999/xlink"),e.setAttribute("version","1.1");var g=document.createElement("canvas");g.setAttribute("class",f+"-measurement-canvas"),this.domElements.graph=this.container.appendChild(e);var h=["edges","nodes","labels","hovers"];for(d=0,c=h.length;c>d;d++)b=document.createElementNS(this.settings("xmlns"),"g"),b.setAttributeNS(null,"id",f+"-group-"+h[d]),b.setAttributeNS(null,"class",f+"-group"),this.domElements.groups[h[d]]=this.domElements.graph.appendChild(b);this.container.appendChild(g),this.measurementCanvas=g.getContext("2d")},sigma.renderers.svg.prototype.hideDOMElements=function(a){var b,c;for(c in a)b=a[c],sigma.svg.utils.hide(b);return this},sigma.renderers.svg.prototype.bindHovers=function(a){function b(b){var c=b.data.node,d=g.settings.embedObjects({prefix:a});if(d("enableHovering")){var h=(f[c.type]||f.def).create(c,g.domElements.nodes[c.id],g.measurementCanvas,d);g.domElements.hovers[c.id]=h,g.domElements.groups.hovers.appendChild(h),e=c}}function c(b){var c=b.data.node,d=g.settings.embedObjects({prefix:a});d("enableHovering")&&(g.domElements.groups.hovers.removeChild(g.domElements.hovers[c.id]),e=null,delete g.domElements.hovers[c.id],g.domElements.groups.nodes.appendChild(g.domElements.nodes[c.id]))}function d(){if(e){var b=g.settings.embedObjects({prefix:a});g.domElements.groups.hovers.removeChild(g.domElements.hovers[e.id]),delete g.domElements.hovers[e.id];var c=(f[e.type]||f.def).create(e,g.domElements.nodes[e.id],g.measurementCanvas,b);g.domElements.hovers[e.id]=c,g.domElements.groups.hovers.appendChild(c)}}var e,f=sigma.svg.hovers,g=this;this.bind("overNode",b),this.bind("outNode",c),this.bind("render",d)},sigma.renderers.svg.prototype.resize=function(b,c){var d=this.width,e=this.height,f=1;return b!==a&&c!==a?(this.width=b,this.height=c):(this.width=this.container.offsetWidth,this.height=this.container.offsetHeight,b=this.width,c=this.height),(d!==this.width||e!==this.height)&&(this.domElements.graph.style.width=b+"px",this.domElements.graph.style.height=c+"px","svg"===this.domElements.graph.tagName.toLowerCase()&&(this.domElements.graph.setAttribute("width",b*f),this.domElements.graph.setAttribute("height",c*f))),this},sigma.utils.pkg("sigma.svg.nodes"),sigma.utils.pkg("sigma.svg.edges"),sigma.utils.pkg("sigma.svg.labels")}.call(this),function(a){"use strict";if("undefined"==typeof sigma)throw"sigma is not declared";sigma.utils.pkg("sigma.renderers");var b,c=!!a.WebGLRenderingContext;if(c){b=document.createElement("canvas");try{c=!(!b.getContext("webgl")&&!b.getContext("experimental-webgl"))}catch(d){c=!1}}sigma.renderers.def=c?sigma.renderers.webgl:sigma.renderers.canvas}(this),function(){"use strict";sigma.utils.pkg("sigma.webgl.nodes"),sigma.webgl.nodes.def={POINTS:3,ATTRIBUTES:5,addNode:function(a,b,c,d,e){var f=sigma.utils.floatColor(a.color||e("defaultNodeColor"));b[c++]=a[d+"x"],b[c++]=a[d+"y"],b[c++]=a[d+"size"],b[c++]=f,b[c++]=0,b[c++]=a[d+"x"],b[c++]=a[d+"y"],b[c++]=a[d+"size"],b[c++]=f,b[c++]=2*Math.PI/3,b[c++]=a[d+"x"],b[c++]=a[d+"y"],b[c++]=a[d+"size"],b[c++]=f,b[c++]=4*Math.PI/3},render:function(a,b,c,d){var e,f=a.getAttribLocation(b,"a_position"),g=a.getAttribLocation(b,"a_size"),h=a.getAttribLocation(b,"a_color"),i=a.getAttribLocation(b,"a_angle"),j=a.getUniformLocation(b,"u_resolution"),k=a.getUniformLocation(b,"u_matrix"),l=a.getUniformLocation(b,"u_ratio"),m=a.getUniformLocation(b,"u_scale");e=a.createBuffer(),a.bindBuffer(a.ARRAY_BUFFER,e),a.bufferData(a.ARRAY_BUFFER,c,a.DYNAMIC_DRAW),a.uniform2f(j,d.width,d.height),a.uniform1f(l,1/Math.pow(d.ratio,d.settings("nodesPowRatio"))),a.uniform1f(m,d.scalingRatio),a.uniformMatrix3fv(k,!1,d.matrix),a.enableVertexAttribArray(f),a.enableVertexAttribArray(g),a.enableVertexAttribArray(h),a.enableVertexAttribArray(i),a.vertexAttribPointer(f,2,a.FLOAT,!1,this.ATTRIBUTES*Float32Array.BYTES_PER_ELEMENT,0),a.vertexAttribPointer(g,1,a.FLOAT,!1,this.ATTRIBUTES*Float32Array.BYTES_PER_ELEMENT,8),a.vertexAttribPointer(h,1,a.FLOAT,!1,this.ATTRIBUTES*Float32Array.BYTES_PER_ELEMENT,12),a.vertexAttribPointer(i,1,a.FLOAT,!1,this.ATTRIBUTES*Float32Array.BYTES_PER_ELEMENT,16),a.drawArrays(a.TRIANGLES,d.start||0,d.count||c.length/this.ATTRIBUTES)},initProgram:function(a){var b,c,d;return b=sigma.utils.loadShader(a,["attribute vec2 a_position;","attribute float a_size;","attribute float a_color;","attribute float a_angle;","uniform vec2 u_resolution;","uniform float u_ratio;","uniform float u_scale;","uniform mat3 u_matrix;","varying vec4 color;","varying vec2 center;","varying float radius;","void main() {","radius = a_size * u_ratio;","vec2 position = (u_matrix * vec3(a_position, 1)).xy;","center = position * u_scale;","center = vec2(center.x, u_scale * u_resolution.y - center.y);","position = position +","2.0 * radius * vec2(cos(a_angle), sin(a_angle));","position = (position / u_resolution * 2.0 - 1.0) * vec2(1, -1);","radius = radius * u_scale;","gl_Position = vec4(position, 0, 1);","float c = a_color;","color.b = mod(c, 256.0); c = floor(c / 256.0);","color.g = mod(c, 256.0); c = floor(c / 256.0);","color.r = mod(c, 256.0); c = floor(c / 256.0); color /= 255.0;","color.a = 1.0;","}"].join("\n"),a.VERTEX_SHADER),c=sigma.utils.loadShader(a,["precision mediump float;","varying vec4 color;","varying vec2 center;","varying float radius;","void main(void) {","vec4 color0 = vec4(0.0, 0.0, 0.0, 0.0);","vec2 m = gl_FragCoord.xy - center;","float diff = radius - sqrt(m.x * m.x + m.y * m.y);","if (diff > 0.0)","gl_FragColor = color;","else","gl_FragColor = color0;","}"].join("\n"),a.FRAGMENT_SHADER),d=sigma.utils.loadProgram(a,[b,c])}}}(),function(){"use strict";sigma.utils.pkg("sigma.webgl.nodes"),sigma.webgl.nodes.fast={POINTS:1,ATTRIBUTES:4,addNode:function(a,b,c,d,e){b[c++]=a[d+"x"],b[c++]=a[d+"y"],b[c++]=a[d+"size"],b[c++]=sigma.utils.floatColor(a.color||e("defaultNodeColor"))},render:function(a,b,c,d){var e,f=a.getAttribLocation(b,"a_position"),g=a.getAttribLocation(b,"a_size"),h=a.getAttribLocation(b,"a_color"),i=a.getUniformLocation(b,"u_resolution"),j=a.getUniformLocation(b,"u_matrix"),k=a.getUniformLocation(b,"u_ratio"),l=a.getUniformLocation(b,"u_scale");e=a.createBuffer(),a.bindBuffer(a.ARRAY_BUFFER,e),a.bufferData(a.ARRAY_BUFFER,c,a.DYNAMIC_DRAW),a.uniform2f(i,d.width,d.height),a.uniform1f(k,1/Math.pow(d.ratio,d.settings("nodesPowRatio"))),a.uniform1f(l,d.scalingRatio),a.uniformMatrix3fv(j,!1,d.matrix),a.enableVertexAttribArray(f),a.enableVertexAttribArray(g),a.enableVertexAttribArray(h),a.vertexAttribPointer(f,2,a.FLOAT,!1,this.ATTRIBUTES*Float32Array.BYTES_PER_ELEMENT,0),a.vertexAttribPointer(g,1,a.FLOAT,!1,this.ATTRIBUTES*Float32Array.BYTES_PER_ELEMENT,8),a.vertexAttribPointer(h,1,a.FLOAT,!1,this.ATTRIBUTES*Float32Array.BYTES_PER_ELEMENT,12),a.drawArrays(a.POINTS,d.start||0,d.count||c.length/this.ATTRIBUTES)},initProgram:function(a){var b,c,d;return b=sigma.utils.loadShader(a,["attribute vec2 a_position;","attribute float a_size;","attribute float a_color;","uniform vec2 u_resolution;","uniform float u_ratio;","uniform float u_scale;","uniform mat3 u_matrix;","varying vec4 color;","void main() {","gl_Position = vec4(","((u_matrix * vec3(a_position, 1)).xy /","u_resolution * 2.0 - 1.0) * vec2(1, -1),","0,","1",");","gl_PointSize = a_size * u_ratio * u_scale * 2.0;","float c = a_color;","color.b = mod(c, 256.0); c = floor(c / 256.0);","color.g = mod(c, 256.0); c = floor(c / 256.0);","color.r = mod(c, 256.0); c = floor(c / 256.0); color /= 255.0;","color.a = 1.0;","}"].join("\n"),a.VERTEX_SHADER),c=sigma.utils.loadShader(a,["precision mediump float;","varying vec4 color;","void main(void) {","float border = 0.01;","float radius = 0.5;","vec4 color0 = vec4(0.0, 0.0, 0.0, 0.0);","vec2 m = gl_PointCoord - vec2(0.5, 0.5);","float dist = radius - sqrt(m.x * m.x + m.y * m.y);","float t = 0.0;","if (dist > border)","t = 1.0;","else if (dist > 0.0)","t = dist / border;","gl_FragColor = mix(color0, color, t);","}"].join("\n"),a.FRAGMENT_SHADER),d=sigma.utils.loadProgram(a,[b,c])}}}(),function(){"use strict";sigma.utils.pkg("sigma.webgl.edges"),sigma.webgl.edges.def={POINTS:6,ATTRIBUTES:7,addEdge:function(a,b,c,d,e,f,g){var h=(a[f+"size"]||1)/2,i=b[f+"x"],j=b[f+"y"],k=c[f+"x"],l=c[f+"y"],m=a.color;if(!m)switch(g("edgeColor")){case"source":m=b.color||g("defaultNodeColor");break;case"target":m=c.color||g("defaultNodeColor");break;default:m=g("defaultEdgeColor")}m=sigma.utils.floatColor(m),d[e++]=i,d[e++]=j,d[e++]=k,d[e++]=l,d[e++]=h,d[e++]=0,d[e++]=m,d[e++]=k,d[e++]=l,d[e++]=i,d[e++]=j,d[e++]=h,d[e++]=1,d[e++]=m,d[e++]=k,d[e++]=l,d[e++]=i,d[e++]=j,d[e++]=h,d[e++]=0,d[e++]=m,d[e++]=k,d[e++]=l,d[e++]=i,d[e++]=j,d[e++]=h,d[e++]=0,d[e++]=m,d[e++]=i,d[e++]=j,d[e++]=k,d[e++]=l,d[e++]=h,d[e++]=1,d[e++]=m,d[e++]=i,d[e++]=j,d[e++]=k,d[e++]=l,d[e++]=h,d[e++]=0,d[e++]=m},render:function(a,b,c,d){var e,f=a.getAttribLocation(b,"a_color"),g=a.getAttribLocation(b,"a_position1"),h=a.getAttribLocation(b,"a_position2"),i=a.getAttribLocation(b,"a_thickness"),j=a.getAttribLocation(b,"a_minus"),k=a.getUniformLocation(b,"u_resolution"),l=a.getUniformLocation(b,"u_matrix"),m=a.getUniformLocation(b,"u_matrixHalfPi"),n=a.getUniformLocation(b,"u_matrixHalfPiMinus"),o=a.getUniformLocation(b,"u_ratio"),p=a.getUniformLocation(b,"u_scale");e=a.createBuffer(),a.bindBuffer(a.ARRAY_BUFFER,e),a.bufferData(a.ARRAY_BUFFER,c,a.STATIC_DRAW),a.uniform2f(k,d.width,d.height),a.uniform1f(o,d.ratio/Math.pow(d.ratio,d.settings("edgesPowRatio"))),a.uniform1f(p,d.scalingRatio),a.uniformMatrix3fv(l,!1,d.matrix),a.uniformMatrix2fv(m,!1,sigma.utils.matrices.rotation(Math.PI/2,!0)),a.uniformMatrix2fv(n,!1,sigma.utils.matrices.rotation(-Math.PI/2,!0)),a.enableVertexAttribArray(f),a.enableVertexAttribArray(g),a.enableVertexAttribArray(h),a.enableVertexAttribArray(i),a.enableVertexAttribArray(j),a.vertexAttribPointer(g,2,a.FLOAT,!1,this.ATTRIBUTES*Float32Array.BYTES_PER_ELEMENT,0),a.vertexAttribPointer(h,2,a.FLOAT,!1,this.ATTRIBUTES*Float32Array.BYTES_PER_ELEMENT,8),a.vertexAttribPointer(i,1,a.FLOAT,!1,this.ATTRIBUTES*Float32Array.BYTES_PER_ELEMENT,16),a.vertexAttribPointer(j,1,a.FLOAT,!1,this.ATTRIBUTES*Float32Array.BYTES_PER_ELEMENT,20),a.vertexAttribPointer(f,1,a.FLOAT,!1,this.ATTRIBUTES*Float32Array.BYTES_PER_ELEMENT,24),a.drawArrays(a.TRIANGLES,d.start||0,d.count||c.length/this.ATTRIBUTES)},initProgram:function(a){var b,c,d;return b=sigma.utils.loadShader(a,["attribute vec2 a_position1;","attribute vec2 a_position2;","attribute float a_thickness;","attribute float a_minus;","attribute float a_color;","uniform vec2 u_resolution;","uniform float u_ratio;","uniform float u_scale;","uniform mat3 u_matrix;","uniform mat2 u_matrixHalfPi;","uniform mat2 u_matrixHalfPiMinus;","varying vec4 color;","void main() {","vec2 position = a_thickness * u_ratio *","normalize(a_position2 - a_position1);","mat2 matrix = a_minus * u_matrixHalfPiMinus +","(1.0 - a_minus) * u_matrixHalfPi;","position = matrix * position + a_position1;","gl_Position = vec4(","((u_matrix * vec3(position, 1)).xy /","u_resolution * 2.0 - 1.0) * vec2(1, -1),","0,","1",");","float c = a_color;","color.b = mod(c, 256.0); c = floor(c / 256.0);","color.g = mod(c, 256.0); c = floor(c / 256.0);","color.r = mod(c, 256.0); c = floor(c / 256.0); color /= 255.0;","color.a = 1.0;","}"].join("\n"),a.VERTEX_SHADER),c=sigma.utils.loadShader(a,["precision mediump float;","varying vec4 color;","void main(void) {","gl_FragColor = color;","}"].join("\n"),a.FRAGMENT_SHADER),d=sigma.utils.loadProgram(a,[b,c])}}}(),function(){"use strict";sigma.utils.pkg("sigma.webgl.edges"),sigma.webgl.edges.fast={POINTS:2,ATTRIBUTES:3,addEdge:function(a,b,c,d,e,f,g){var h=((a[f+"size"]||1)/2,b[f+"x"]),i=b[f+"y"],j=c[f+"x"],k=c[f+"y"],l=a.color;if(!l)switch(g("edgeColor")){case"source":l=b.color||g("defaultNodeColor");break;case"target":l=c.color||g("defaultNodeColor");break;default:l=g("defaultEdgeColor")}l=sigma.utils.floatColor(l),d[e++]=h,d[e++]=i,d[e++]=l,d[e++]=j,d[e++]=k,d[e++]=l},render:function(a,b,c,d){var e,f=a.getAttribLocation(b,"a_color"),g=a.getAttribLocation(b,"a_position"),h=a.getUniformLocation(b,"u_resolution"),i=a.getUniformLocation(b,"u_matrix");e=a.createBuffer(),a.bindBuffer(a.ARRAY_BUFFER,e),a.bufferData(a.ARRAY_BUFFER,c,a.DYNAMIC_DRAW),a.uniform2f(h,d.width,d.height),a.uniformMatrix3fv(i,!1,d.matrix),a.enableVertexAttribArray(g),a.enableVertexAttribArray(f),a.vertexAttribPointer(g,2,a.FLOAT,!1,this.ATTRIBUTES*Float32Array.BYTES_PER_ELEMENT,0),a.vertexAttribPointer(f,1,a.FLOAT,!1,this.ATTRIBUTES*Float32Array.BYTES_PER_ELEMENT,8),a.lineWidth(3),a.drawArrays(a.LINES,d.start||0,d.count||c.length/this.ATTRIBUTES)},initProgram:function(a){var b,c,d;return b=sigma.utils.loadShader(a,["attribute vec2 a_position;","attribute float a_color;","uniform vec2 u_resolution;","uniform mat3 u_matrix;","varying vec4 color;","void main() {","gl_Position = vec4(","((u_matrix * vec3(a_position, 1)).xy /","u_resolution * 2.0 - 1.0) * vec2(1, -1),","0,","1",");","float c = a_color;","color.b = mod(c, 256.0); c = floor(c / 256.0);","color.g = mod(c, 256.0); c = floor(c / 256.0);","color.r = mod(c, 256.0); c = floor(c / 256.0); color /= 255.0;","color.a = 1.0;","}"].join("\n"),a.VERTEX_SHADER),c=sigma.utils.loadShader(a,["precision mediump float;","varying vec4 color;","void main(void) {","gl_FragColor = color;","}"].join("\n"),a.FRAGMENT_SHADER),d=sigma.utils.loadProgram(a,[b,c])}}}(),function(){"use strict";sigma.utils.pkg("sigma.webgl.edges"),sigma.webgl.edges.arrow={POINTS:9,ATTRIBUTES:11,addEdge:function(a,b,c,d,e,f,g){var h=(a[f+"size"]||1)/2,i=b[f+"x"],j=b[f+"y"],k=c[f+"x"],l=c[f+"y"],m=c[f+"size"],n=a.color;if(!n)switch(g("edgeColor")){case"source":n=b.color||g("defaultNodeColor");break;case"target":n=c.color||g("defaultNodeColor");break;default:n=g("defaultEdgeColor")}n=sigma.utils.floatColor(n),d[e++]=i,d[e++]=j,d[e++]=k,d[e++]=l,d[e++]=h,d[e++]=m,d[e++]=0,d[e++]=0,d[e++]=0,d[e++]=0,d[e++]=n,d[e++]=k,d[e++]=l,d[e++]=i,d[e++]=j,d[e++]=h,d[e++]=m,d[e++]=1,d[e++]=1,d[e++]=0,d[e++]=0,d[e++]=n,d[e++]=k,d[e++]=l,d[e++]=i,d[e++]=j,d[e++]=h,d[e++]=m,d[e++]=1,d[e++]=0,d[e++]=0,d[e++]=0,d[e++]=n,d[e++]=k,d[e++]=l,d[e++]=i,d[e++]=j,d[e++]=h,d[e++]=m,d[e++]=1,d[e++]=0,d[e++]=0,d[e++]=0,d[e++]=n,d[e++]=i,d[e++]=j,d[e++]=k,d[e++]=l,d[e++]=h,d[e++]=m,d[e++]=0,d[e++]=1,d[e++]=0,d[e++]=0,d[e++]=n,d[e++]=i,d[e++]=j,d[e++]=k,d[e++]=l,d[e++]=h,d[e++]=m,d[e++]=0,d[e++]=0,d[e++]=0,d[e++]=0,d[e++]=n,d[e++]=k,d[e++]=l,d[e++]=i,d[e++]=j,d[e++]=h,d[e++]=m,d[e++]=1,d[e++]=0,d[e++]=1,d[e++]=-1,d[e++]=n,d[e++]=k,d[e++]=l,d[e++]=i,d[e++]=j,d[e++]=h,d[e++]=m,d[e++]=1,d[e++]=0,d[e++]=1,d[e++]=0,d[e++]=n,d[e++]=k,d[e++]=l,d[e++]=i,d[e++]=j,d[e++]=h,d[e++]=m,d[e++]=1,d[e++]=0,d[e++]=1,d[e++]=1,d[e++]=n},render:function(a,b,c,d){var e,f=a.getAttribLocation(b,"a_pos1"),g=a.getAttribLocation(b,"a_pos2"),h=a.getAttribLocation(b,"a_thickness"),i=a.getAttribLocation(b,"a_tSize"),j=a.getAttribLocation(b,"a_delay"),k=a.getAttribLocation(b,"a_minus"),l=a.getAttribLocation(b,"a_head"),m=a.getAttribLocation(b,"a_headPosition"),n=a.getAttribLocation(b,"a_color"),o=a.getUniformLocation(b,"u_resolution"),p=a.getUniformLocation(b,"u_matrix"),q=a.getUniformLocation(b,"u_matrixHalfPi"),r=a.getUniformLocation(b,"u_matrixHalfPiMinus"),s=a.getUniformLocation(b,"u_ratio"),t=a.getUniformLocation(b,"u_nodeRatio"),u=a.getUniformLocation(b,"u_arrowHead"),v=a.getUniformLocation(b,"u_scale");e=a.createBuffer(),a.bindBuffer(a.ARRAY_BUFFER,e),a.bufferData(a.ARRAY_BUFFER,c,a.STATIC_DRAW),a.uniform2f(o,d.width,d.height),a.uniform1f(s,d.ratio/Math.pow(d.ratio,d.settings("edgesPowRatio"))),a.uniform1f(t,Math.pow(d.ratio,d.settings("nodesPowRatio"))/d.ratio),a.uniform1f(u,5),a.uniform1f(v,d.scalingRatio),a.uniformMatrix3fv(p,!1,d.matrix),a.uniformMatrix2fv(q,!1,sigma.utils.matrices.rotation(Math.PI/2,!0)),a.uniformMatrix2fv(r,!1,sigma.utils.matrices.rotation(-Math.PI/2,!0)),a.enableVertexAttribArray(f),a.enableVertexAttribArray(g),a.enableVertexAttribArray(h),a.enableVertexAttribArray(i),a.enableVertexAttribArray(j),a.enableVertexAttribArray(k),a.enableVertexAttribArray(l),a.enableVertexAttribArray(m),a.enableVertexAttribArray(n),a.vertexAttribPointer(f,2,a.FLOAT,!1,this.ATTRIBUTES*Float32Array.BYTES_PER_ELEMENT,0),a.vertexAttribPointer(g,2,a.FLOAT,!1,this.ATTRIBUTES*Float32Array.BYTES_PER_ELEMENT,8),a.vertexAttribPointer(h,1,a.FLOAT,!1,this.ATTRIBUTES*Float32Array.BYTES_PER_ELEMENT,16),a.vertexAttribPointer(i,1,a.FLOAT,!1,this.ATTRIBUTES*Float32Array.BYTES_PER_ELEMENT,20),a.vertexAttribPointer(j,1,a.FLOAT,!1,this.ATTRIBUTES*Float32Array.BYTES_PER_ELEMENT,24),a.vertexAttribPointer(k,1,a.FLOAT,!1,this.ATTRIBUTES*Float32Array.BYTES_PER_ELEMENT,28),a.vertexAttribPointer(l,1,a.FLOAT,!1,this.ATTRIBUTES*Float32Array.BYTES_PER_ELEMENT,32),a.vertexAttribPointer(m,1,a.FLOAT,!1,this.ATTRIBUTES*Float32Array.BYTES_PER_ELEMENT,36),a.vertexAttribPointer(n,1,a.FLOAT,!1,this.ATTRIBUTES*Float32Array.BYTES_PER_ELEMENT,40),a.drawArrays(a.TRIANGLES,d.start||0,d.count||c.length/this.ATTRIBUTES)},initProgram:function(a){var b,c,d;return b=sigma.utils.loadShader(a,["attribute vec2 a_pos1;","attribute vec2 a_pos2;","attribute float a_thickness;","attribute float a_tSize;","attribute float a_delay;","attribute float a_minus;","attribute float a_head;","attribute float a_headPosition;","attribute float a_color;","uniform vec2 u_resolution;","uniform float u_ratio;","uniform float u_nodeRatio;","uniform float u_arrowHead;","uniform float u_scale;","uniform mat3 u_matrix;","uniform mat2 u_matrixHalfPi;","uniform mat2 u_matrixHalfPiMinus;","varying vec4 color;","void main() {","vec2 pos = normalize(a_pos2 - a_pos1);","mat2 matrix = (1.0 - a_head) *","(","a_minus * u_matrixHalfPiMinus +","(1.0 - a_minus) * u_matrixHalfPi",") + a_head * (","a_headPosition * u_matrixHalfPiMinus * 0.6 +","(a_headPosition * a_headPosition - 1.0) * mat2(1.0)",");","pos = a_pos1 + (","(1.0 - a_head) * a_thickness * u_ratio * matrix * pos +","a_head * u_arrowHead * a_thickness * u_ratio * matrix * pos +","a_delay * pos * (","a_tSize / u_nodeRatio +","u_arrowHead * a_thickness * u_ratio",")",");","gl_Position = vec4(","((u_matrix * vec3(pos, 1)).xy /","u_resolution * 2.0 - 1.0) * vec2(1, -1),","0,","1",");","float c = a_color;","color.b = mod(c, 256.0); c = floor(c / 256.0);","color.g = mod(c, 256.0); c = floor(c / 256.0);","color.r = mod(c, 256.0); c = floor(c / 256.0); color /= 255.0;","color.a = 1.0;","}"].join("\n"),a.VERTEX_SHADER),c=sigma.utils.loadShader(a,["precision mediump float;","varying vec4 color;","void main(void) {","gl_FragColor = color;","}"].join("\n"),a.FRAGMENT_SHADER),d=sigma.utils.loadProgram(a,[b,c])}}}(),function(){"use strict";if("undefined"==typeof sigma)throw"sigma is not declared";sigma.utils.pkg("sigma.canvas.labels"),sigma.canvas.labels.def=function(a,b,c){var d,e=c("prefix")||"",f=a[e+"size"];f0&&(b.beginPath(),b.fillStyle="node"===c("nodeBorderColor")?a.color||c("defaultNodeColor"):c("defaultNodeBorderColor"),b.arc(a[j+"x"],a[j+"y"],k+c("borderSize"),0,2*Math.PI,!0),b.closePath(),b.fill());var m=sigma.canvas.nodes[a.type]||sigma.canvas.nodes.def;m(a,b,c),a.label&&"string"==typeof a.label&&(b.fillStyle="node"===c("labelHoverColor")?a.color||c("defaultNodeColor"):c("defaultLabelHoverColor"),b.fillText(a.label,Math.round(a[j+"x"]+k+3),Math.round(a[j+"y"]+l/3)))}}.call(this),function(){"use strict";sigma.utils.pkg("sigma.canvas.nodes"),sigma.canvas.nodes.def=function(a,b,c){var d=c("prefix")||"";b.fillStyle=a.color||c("defaultNodeColor"),b.beginPath(),b.arc(a[d+"x"],a[d+"y"],a[d+"size"],0,2*Math.PI,!0),b.closePath(),b.fill()}}(),function(){"use strict";sigma.utils.pkg("sigma.canvas.edges"),sigma.canvas.edges.def=function(a,b,c,d,e){var f=a.color,g=e("prefix")||"",h=a[g+"size"]||1,i=e("edgeColor"),j=e("defaultNodeColor"),k=e("defaultEdgeColor");if(!f)switch(i){case"source":f=b.color||j;break;case"target":f=c.color||j;break;default:f=k}d.strokeStyle=f,d.lineWidth=h,d.beginPath(),d.moveTo(b[g+"x"],b[g+"y"]),d.lineTo(c[g+"x"],c[g+"y"]),d.stroke()}}(),function(){"use strict";sigma.utils.pkg("sigma.canvas.edges"),sigma.canvas.edges.curve=function(a,b,c,d,e){var f=a.color,g=e("prefix")||"",h=a[g+"size"]||1,i=e("edgeColor"),j=e("defaultNodeColor"),k=e("defaultEdgeColor"),l={},m=b[g+"size"],n=b[g+"x"],o=b[g+"y"],p=c[g+"x"],q=c[g+"y"];if(l=b.id===c.id?sigma.utils.getSelfLoopControlPoints(n,o,m):sigma.utils.getQuadraticControlPoint(n,o,p,q),!f)switch(i){case"source":f=b.color||j;break;case"target":f=c.color||j;break;default:f=k}d.strokeStyle=f,d.lineWidth=h,d.beginPath(),d.moveTo(n,o),b.id===c.id?d.bezierCurveTo(l.x1,l.y1,l.x2,l.y2,p,q):d.quadraticCurveTo(l.x,l.y,p,q),d.stroke()}}(),function(){"use strict";sigma.utils.pkg("sigma.canvas.edges"),sigma.canvas.edges.arrow=function(a,b,c,d,e){var f=a.color,g=e("prefix")||"",h=e("edgeColor"),i=e("defaultNodeColor"),j=e("defaultEdgeColor"),k=a[g+"size"]||1,l=c[g+"size"],m=b[g+"x"],n=b[g+"y"],o=c[g+"x"],p=c[g+"y"],q=Math.max(2.5*k,e("minArrowSize")),r=Math.sqrt(Math.pow(o-m,2)+Math.pow(p-n,2)),s=m+(o-m)*(r-q-l)/r,t=n+(p-n)*(r-q-l)/r,u=(o-m)*q/r,v=(p-n)*q/r;if(!f)switch(h){case"source":f=b.color||i;break;case"target":f=c.color||i;break;default:f=j}d.strokeStyle=f,d.lineWidth=k,d.beginPath(),d.moveTo(m,n),d.lineTo(s,t),d.stroke(),d.fillStyle=f,d.beginPath(),d.moveTo(s+u,t+v),d.lineTo(s+.6*v,t-.6*u),d.lineTo(s-.6*v,t+.6*u),d.lineTo(s+u,t+v),d.closePath(),d.fill()}}(),function(){"use strict";sigma.utils.pkg("sigma.canvas.edges"),sigma.canvas.edges.curvedArrow=function(a,b,c,d,e){var f,g,h,i,j,k=a.color,l=e("prefix")||"",m=e("edgeColor"),n=e("defaultNodeColor"),o=e("defaultEdgeColor"),p={},q=a[l+"size"]||1,r=c[l+"size"],s=b[l+"x"],t=b[l+"y"],u=c[l+"x"],v=c[l+"y"],w=Math.max(2.5*q,e("minArrowSize"));if(p=b.id===c.id?sigma.utils.getSelfLoopControlPoints(s,t,r):sigma.utils.getQuadraticControlPoint(s,t,u,v),b.id===c.id?(f=Math.sqrt(Math.pow(u-p.x1,2)+Math.pow(v-p.y1,2)),g=p.x1+(u-p.x1)*(f-w-r)/f,h=p.y1+(v-p.y1)*(f-w-r)/f,i=(u-p.x1)*w/f,j=(v-p.y1)*w/f):(f=Math.sqrt(Math.pow(u-p.x,2)+Math.pow(v-p.y,2)),g=p.x+(u-p.x)*(f-w-r)/f,h=p.y+(v-p.y)*(f-w-r)/f,i=(u-p.x)*w/f,j=(v-p.y)*w/f),!k)switch(m){case"source":k=b.color||n;break;case"target":k=c.color||n;break;default:k=o}d.strokeStyle=k,d.lineWidth=q,d.beginPath(),d.moveTo(s,t),b.id===c.id?d.bezierCurveTo(p.x2,p.y2,p.x1,p.y1,g,h):d.quadraticCurveTo(p.x,p.y,g,h),d.stroke(),d.fillStyle=k,d.beginPath(),d.moveTo(g+i,h+j),d.lineTo(g+.6*j,h-.6*i),d.lineTo(g-.6*j,h+.6*i),d.lineTo(g+i,h+j),d.closePath(),d.fill()}}(),function(){"use strict";sigma.utils.pkg("sigma.canvas.edgehovers"),sigma.canvas.edgehovers.def=function(a,b,c,d,e){var f=a.color,g=e("prefix")||"",h=a[g+"size"]||1,i=e("edgeColor"),j=e("defaultNodeColor"),k=e("defaultEdgeColor");if(!f)switch(i){case"source":f=b.color||j;break;case"target":f=c.color||j;break;default:f=k}f="edge"===e("edgeHoverColor")?a.hover_color||f:a.hover_color||e("defaultEdgeHoverColor")||f,h*=e("edgeHoverSizeRatio"),d.strokeStyle=f,d.lineWidth=h,d.beginPath(),d.moveTo(b[g+"x"],b[g+"y"]),d.lineTo(c[g+"x"],c[g+"y"]),d.stroke()}}(),function(){"use strict";sigma.utils.pkg("sigma.canvas.edgehovers"),sigma.canvas.edgehovers.curve=function(a,b,c,d,e){var f=a.color,g=e("prefix")||"",h=e("edgeHoverSizeRatio")*(a[g+"size"]||1),i=e("edgeColor"),j=e("defaultNodeColor"),k=e("defaultEdgeColor"),l={},m=b[g+"size"],n=b[g+"x"],o=b[g+"y"],p=c[g+"x"],q=c[g+"y"];if(l=b.id===c.id?sigma.utils.getSelfLoopControlPoints(n,o,m):sigma.utils.getQuadraticControlPoint(n,o,p,q),!f)switch(i){case"source":f=b.color||j;break;case"target":f=c.color||j;break;default:f=k}f="edge"===e("edgeHoverColor")?a.hover_color||f:a.hover_color||e("defaultEdgeHoverColor")||f,d.strokeStyle=f,d.lineWidth=h,d.beginPath(),d.moveTo(n,o),b.id===c.id?d.bezierCurveTo(l.x1,l.y1,l.x2,l.y2,p,q):d.quadraticCurveTo(l.x,l.y,p,q),d.stroke()}}(),function(){"use strict";sigma.utils.pkg("sigma.canvas.edgehovers"),sigma.canvas.edgehovers.arrow=function(a,b,c,d,e){var f=a.color,g=e("prefix")||"",h=e("edgeColor"),i=e("defaultNodeColor"),j=e("defaultEdgeColor"),k=a[g+"size"]||1,l=c[g+"size"],m=b[g+"x"],n=b[g+"y"],o=c[g+"x"],p=c[g+"y"];
5 | k=a.hover?e("edgeHoverSizeRatio")*k:k;var q=2.5*k,r=Math.sqrt(Math.pow(o-m,2)+Math.pow(p-n,2)),s=m+(o-m)*(r-q-l)/r,t=n+(p-n)*(r-q-l)/r,u=(o-m)*q/r,v=(p-n)*q/r;if(!f)switch(h){case"source":f=b.color||i;break;case"target":f=c.color||i;break;default:f=j}f="edge"===e("edgeHoverColor")?a.hover_color||f:a.hover_color||e("defaultEdgeHoverColor")||f,d.strokeStyle=f,d.lineWidth=k,d.beginPath(),d.moveTo(m,n),d.lineTo(s,t),d.stroke(),d.fillStyle=f,d.beginPath(),d.moveTo(s+u,t+v),d.lineTo(s+.6*v,t-.6*u),d.lineTo(s-.6*v,t+.6*u),d.lineTo(s+u,t+v),d.closePath(),d.fill()}}(),function(){"use strict";sigma.utils.pkg("sigma.canvas.edgehovers"),sigma.canvas.edgehovers.curvedArrow=function(a,b,c,d,e){var f,g,h,i,j,k,l=a.color,m=e("prefix")||"",n=e("edgeColor"),o=e("defaultNodeColor"),p=e("defaultEdgeColor"),q={},r=e("edgeHoverSizeRatio")*(a[m+"size"]||1),s=c[m+"size"],t=b[m+"x"],u=b[m+"y"],v=c[m+"x"],w=c[m+"y"];if(q=b.id===c.id?sigma.utils.getSelfLoopControlPoints(t,u,s):sigma.utils.getQuadraticControlPoint(t,u,v,w),b.id===c.id?(f=Math.sqrt(Math.pow(v-q.x1,2)+Math.pow(w-q.y1,2)),g=2.5*r,h=q.x1+(v-q.x1)*(f-g-s)/f,i=q.y1+(w-q.y1)*(f-g-s)/f,j=(v-q.x1)*g/f,k=(w-q.y1)*g/f):(f=Math.sqrt(Math.pow(v-q.x,2)+Math.pow(w-q.y,2)),g=2.5*r,h=q.x+(v-q.x)*(f-g-s)/f,i=q.y+(w-q.y)*(f-g-s)/f,j=(v-q.x)*g/f,k=(w-q.y)*g/f),!l)switch(n){case"source":l=b.color||o;break;case"target":l=c.color||o;break;default:l=p}l="edge"===e("edgeHoverColor")?a.hover_color||l:a.hover_color||e("defaultEdgeHoverColor")||l,d.strokeStyle=l,d.lineWidth=r,d.beginPath(),d.moveTo(t,u),b.id===c.id?d.bezierCurveTo(q.x2,q.y2,q.x1,q.y1,h,i):d.quadraticCurveTo(q.x,q.y,h,i),d.stroke(),d.fillStyle=l,d.beginPath(),d.moveTo(h+j,i+k),d.lineTo(h+.6*k,i-.6*j),d.lineTo(h-.6*k,i+.6*j),d.lineTo(h+j,i+k),d.closePath(),d.fill()}}(),function(){"use strict";if("undefined"==typeof sigma)throw"sigma is not declared";sigma.utils.pkg("sigma.canvas.extremities"),sigma.canvas.extremities.def=function(a,b,c,d,e){(sigma.canvas.hovers[b.type]||sigma.canvas.hovers.def)(b,d,e),(sigma.canvas.hovers[c.type]||sigma.canvas.hovers.def)(c,d,e)}}.call(this),function(){"use strict";sigma.utils.pkg("sigma.svg.utils"),sigma.svg.utils={show:function(a){return a.style.display="",this},hide:function(a){return a.style.display="none",this}}}(),function(){"use strict";sigma.utils.pkg("sigma.svg.nodes"),sigma.svg.nodes.def={create:function(a,b){var c=(b("prefix")||"",document.createElementNS(b("xmlns"),"circle"));return c.setAttributeNS(null,"data-node-id",a.id),c.setAttributeNS(null,"class",b("classPrefix")+"-node"),c.setAttributeNS(null,"fill",a.color||b("defaultNodeColor")),c},update:function(a,b,c){var d=c("prefix")||"";return b.setAttributeNS(null,"cx",a[d+"x"]),b.setAttributeNS(null,"cy",a[d+"y"]),b.setAttributeNS(null,"r",a[d+"size"]),c("freeStyle")||b.setAttributeNS(null,"fill",a.color||c("defaultNodeColor")),b.style.display="",this}}}(),function(){"use strict";sigma.utils.pkg("sigma.svg.edges"),sigma.svg.edges.def={create:function(a,b,c,d){var e=a.color,f=(d("prefix")||"",d("edgeColor")),g=d("defaultNodeColor"),h=d("defaultEdgeColor");if(!e)switch(f){case"source":e=b.color||g;break;case"target":e=c.color||g;break;default:e=h}var i=document.createElementNS(d("xmlns"),"line");return i.setAttributeNS(null,"data-edge-id",a.id),i.setAttributeNS(null,"class",d("classPrefix")+"-edge"),i.setAttributeNS(null,"stroke",e),i},update:function(a,b,c,d,e){var f=e("prefix")||"";return b.setAttributeNS(null,"stroke-width",a[f+"size"]||1),b.setAttributeNS(null,"x1",c[f+"x"]),b.setAttributeNS(null,"y1",c[f+"y"]),b.setAttributeNS(null,"x2",d[f+"x"]),b.setAttributeNS(null,"y2",d[f+"y"]),b.style.display="",this}}}(),function(){"use strict";sigma.utils.pkg("sigma.svg.edges"),sigma.svg.edges.curve={create:function(a,b,c,d){var e=a.color,f=(d("prefix")||"",d("edgeColor")),g=d("defaultNodeColor"),h=d("defaultEdgeColor");if(!e)switch(f){case"source":e=b.color||g;break;case"target":e=c.color||g;break;default:e=h}var i=document.createElementNS(d("xmlns"),"path");return i.setAttributeNS(null,"data-edge-id",a.id),i.setAttributeNS(null,"class",d("classPrefix")+"-edge"),i.setAttributeNS(null,"stroke",e),i},update:function(a,b,c,d,e){var f=e("prefix")||"";b.setAttributeNS(null,"stroke-width",a[f+"size"]||1);var g=(c[f+"x"]+d[f+"x"])/2+(d[f+"y"]-c[f+"y"])/4,h=(c[f+"y"]+d[f+"y"])/2+(c[f+"x"]-d[f+"x"])/4,i="M"+c[f+"x"]+","+c[f+"y"]+" Q"+g+","+h+" "+d[f+"x"]+","+d[f+"y"];return b.setAttributeNS(null,"d",i),b.setAttributeNS(null,"fill","none"),b.style.display="",this}}}(),function(){"use strict";if("undefined"==typeof sigma)throw"sigma is not declared";sigma.utils.pkg("sigma.svg.labels"),sigma.svg.labels.def={create:function(a,b){var c=b("prefix")||"",d=a[c+"size"],e=document.createElementNS(b("xmlns"),"text"),f="fixed"===b("labelSize")?b("defaultLabelSize"):b("labelSizeRatio")*d,g="node"===b("labelColor")?a.color||b("defaultNodeColor"):b("defaultLabelColor");return e.setAttributeNS(null,"data-label-target",a.id),e.setAttributeNS(null,"class",b("classPrefix")+"-label"),e.setAttributeNS(null,"font-size",f),e.setAttributeNS(null,"font-family",b("font")),e.setAttributeNS(null,"fill",g),e.innerHTML=a.label,e.textContent=a.label,e},update:function(a,b,c){var d=c("prefix")||"",e=a[d+"size"],f="fixed"===c("labelSize")?c("defaultLabelSize"):c("labelSizeRatio")*e;return!c("forceLabels")&&ed;d++)if(!y[x[d]])throw new Error('The rescale setting "'+x[d]+'" is not recognized.');var z=~x.indexOf("nodePosition"),A=~x.indexOf("nodeSize"),B=~x.indexOf("edgeSize");for(j="outside"===n("scalingMode")?Math.max(v/Math.max(r-p,1),w/Math.max(s-q,1)):Math.min(v/Math.max(r-p,1),w/Math.max(s-q,1)),k=(n("rescaleIgnoreSize")?0:(n("maxNodeSize")||t)/j)+(n("sideMargin")||0),r+=k,p-=k,s+=k,q-=k,j="outside"===n("scalingMode")?Math.max(v/Math.max(r-p,1),w/Math.max(s-q,1)):Math.min(v/Math.max(r-p,1),w/Math.max(s-q,1)),n("maxNodeSize")||n("minNodeSize")?n("maxNodeSize")===n("minNodeSize")?(f=0,g=+n("maxNodeSize")):(f=(n("maxNodeSize")-n("minNodeSize"))/t,g=+n("minNodeSize")):(f=1,g=0),n("maxEdgeSize")||n("minEdgeSize")?n("maxEdgeSize")===n("minEdgeSize")?(h=0,i=+n("minEdgeSize")):(h=(n("maxEdgeSize")-n("minEdgeSize"))/u,i=+n("minEdgeSize")):(h=1,i=0),d=0,e=m.length;e>d;d++)m[d][b+"size"]=m[d][a+"size"]*(B?h:1)+(B?i:0);for(d=0,e=l.length;e>d;d++)l[d][b+"size"]=l[d][a+"size"]*(A?f:1)+(A?g:0),l[d][b+"x"]=(l[d][a+"x"]-(r+p)/2)*(z?j:1),l[d][b+"y"]=(l[d][a+"y"]-(s+q)/2)*(z?j:1)},sigma.utils.getBoundaries=function(a,b,c){var d,e,f=a.edges(),g=a.nodes(),h=-1/0,i=-1/0,j=1/0,k=1/0,l=-1/0,m=-1/0;if(c)for(d=0,e=f.length;e>d;d++)h=Math.max(f[d][b+"size"],h);for(d=0,e=g.length;e>d;d++)i=Math.max(g[d][b+"size"],i),l=Math.max(g[d][b+"x"],l),j=Math.min(g[d][b+"x"],j),m=Math.max(g[d][b+"y"],m),k=Math.min(g[d][b+"y"],k);return h=h||1,i=i||1,{weightMax:h,sizeMax:i,minX:j,minY:k,maxX:l,maxY:m}}}.call(this),function(){"use strict";if("undefined"==typeof sigma)throw"sigma is not declared";sigma.utils.pkg("sigma.middlewares"),sigma.middlewares.copy=function(a,b){var c,d,e;if(b+""!=a+""){for(e=this.graph.nodes(),c=0,d=e.length;d>c;c++)e[c][b+"x"]=e[c][a+"x"],e[c][b+"y"]=e[c][a+"y"],e[c][b+"size"]=e[c][a+"size"];for(e=this.graph.edges(),c=0,d=e.length;d>c;c++)e[c][b+"size"]=e[c][a+"size"]}}}.call(this),function(a){"use strict";if("undefined"==typeof sigma)throw"sigma is not declared";sigma.utils.pkg("sigma.misc.animation.running");var b=function(){var a=0;return function(){return""+ ++a}}();sigma.misc.animation.camera=function(c,d,e){if(!(c instanceof sigma.classes.camera&&"object"==typeof d&&d))throw"animation.camera: Wrong arguments.";if("number"!=typeof d.x&&"number"!=typeof d.y&&"number"!=typeof d.ratio&&"number"!=typeof d.angle)throw"There must be at least one valid coordinate in the given val.";var f,g,h,i,j,k,l=e||{},m=sigma.utils.dateNow();return k={x:c.x,y:c.y,ratio:c.ratio,angle:c.angle},j=l.duration,i="function"!=typeof l.easing?sigma.utils.easings[l.easing||"quadraticInOut"]:l.easing,f=function(){var b,e=l.duration?(sigma.utils.dateNow()-m)/l.duration:1;e>=1?(c.isAnimated=!1,c.goTo({x:d.x!==a?d.x:k.x,y:d.y!==a?d.y:k.y,ratio:d.ratio!==a?d.ratio:k.ratio,angle:d.angle!==a?d.angle:k.angle}),cancelAnimationFrame(g),delete sigma.misc.animation.running[g],"function"==typeof l.onComplete&&l.onComplete()):(b=i(e),c.isAnimated=!0,c.goTo({x:d.x!==a?k.x+(d.x-k.x)*b:k.x,y:d.y!==a?k.y+(d.y-k.y)*b:k.y,ratio:d.ratio!==a?k.ratio+(d.ratio-k.ratio)*b:k.ratio,angle:d.angle!==a?k.angle+(d.angle-k.angle)*b:k.angle}),"function"==typeof l.onNewFrame&&l.onNewFrame(),h.frameId=requestAnimationFrame(f))},g=b(),h={frameId:requestAnimationFrame(f),target:c,type:"camera",options:l,fn:f},sigma.misc.animation.running[g]=h,g},sigma.misc.animation.kill=function(a){if(1!==arguments.length||"number"!=typeof a)throw"animation.kill: Wrong arguments.";var b=sigma.misc.animation.running[a];return b&&(cancelAnimationFrame(a),delete sigma.misc.animation.running[b.frameId],"camera"===b.type&&(b.target.isAnimated=!1),"function"==typeof(b.options||{}).onComplete&&b.options.onComplete()),this},sigma.misc.animation.killAll=function(a){var b,c,d=0,e="string"==typeof a?a:null,f="object"==typeof a?a:null,g=sigma.misc.animation.running;for(c in g)e&&g[c].type!==e||f&&g[c].target!==f||(b=sigma.misc.animation.running[c],cancelAnimationFrame(b.frameId),delete sigma.misc.animation.running[c],"camera"===b.type&&(b.target.isAnimated=!1),d++,"function"==typeof(b.options||{}).onComplete&&b.options.onComplete());return d},sigma.misc.animation.has=function(a){var b,c="string"==typeof a?a:null,d="object"==typeof a?a:null,e=sigma.misc.animation.running;for(b in e)if(!(c&&e[b].type!==c||d&&e[b].target!==d))return!0;return!1}}.call(this),function(a){"use strict";if("undefined"==typeof sigma)throw"sigma is not declared";sigma.utils.pkg("sigma.misc"),sigma.misc.bindEvents=function(b){function c(a){a&&(h="x"in a.data?a.data.x:h,i="y"in a.data?a.data.y:i);var c,d,e,f,g,k,l,m,n=[],o=h+j.width/2,p=i+j.height/2,q=j.camera.cameraPosition(h,i),r=j.camera.quadtree.point(q.x,q.y);if(r.length)for(c=0,e=r.length;e>c;c++)if(f=r[c],g=f[b+"x"],k=f[b+"y"],l=f[b+"size"],!f.hidden&&o>g-l&&g+l>o&&p>k-l&&k+l>p&&Math.sqrt(Math.pow(o-g,2)+Math.pow(p-k,2))n[d].size){n.splice(d,0,f),m=!0;break}m||n.push(f)}return n}function d(c){function d(a,b){for(r=!1,g=0;ga[g].size){a.splice(g,0,b),r=!0;break}r||a.push(b)}if(!j.settings("enableEdgeHovering"))return[];var e=sigma.renderers.canvas&&j instanceof sigma.renderers.canvas;if(!e)throw new Error("The edge events feature is not compatible with the WebGL renderer");c&&(h="x"in c.data?c.data.x:h,i="y"in c.data?c.data.y:i);var f,g,k,l,m,n,o,p,q,r,s=j.settings("edgeHoverPrecision"),t={},u=[],v=h+j.width/2,w=i+j.height/2,x=j.camera.cameraPosition(h,i),y=[];if(e){var z=j.camera.quadtree.area(j.camera.getRectangle(j.width,j.height));for(l=z,f=0,k=l.length;k>f;f++)t[l[f].id]=l[f]}if(j.camera.edgequadtree!==a&&(y=j.camera.edgequadtree.point(x.x,x.y)),y.length)for(f=0,k=y.length;k>f;f++)m=y[f],o=j.graph.nodes(m.source),p=j.graph.nodes(m.target),n=m[b+"size"]||m["read_"+b+"size"],!m.hidden&&!o.hidden&&!p.hidden&&(!e||t[m.source]||t[m.target])&&sigma.utils.getDistance(o[b+"x"],o[b+"y"],v,w)>o[b+"size"]&&sigma.utils.getDistance(p[b+"x"],p[b+"y"],v,w)>p[b+"size"]&&("curve"==m.type||"curvedArrow"==m.type?o.id===p.id?(q=sigma.utils.getSelfLoopControlPoints(o[b+"x"],o[b+"y"],o[b+"size"]),sigma.utils.isPointOnBezierCurve(v,w,o[b+"x"],o[b+"y"],p[b+"x"],p[b+"y"],q.x1,q.y1,q.x2,q.y2,Math.max(n,s))&&d(u,m)):(q=sigma.utils.getQuadraticControlPoint(o[b+"x"],o[b+"y"],p[b+"x"],p[b+"y"]),sigma.utils.isPointOnQuadraticCurve(v,w,o[b+"x"],o[b+"y"],p[b+"x"],p[b+"y"],q.x,q.y,Math.max(n,s))&&d(u,m)):sigma.utils.isPointOnSegment(v,w,o[b+"x"],o[b+"y"],p[b+"x"],p[b+"y"],Math.max(n,s))&&d(u,m));return u}function e(a){function b(a){j.settings("eventsEnabled")&&(j.dispatchEvent("click",a.data),i=c(a),k=d(a),i.length?(j.dispatchEvent("clickNode",{node:i[0],captor:a.data}),j.dispatchEvent("clickNodes",{node:i,captor:a.data})):k.length?(j.dispatchEvent("clickEdge",{edge:k[0],captor:a.data}),j.dispatchEvent("clickEdges",{edge:k,captor:a.data})):j.dispatchEvent("clickStage",{captor:a.data}))}function e(a){j.settings("eventsEnabled")&&(j.dispatchEvent("doubleClick",a.data),i=c(a),k=d(a),i.length?(j.dispatchEvent("doubleClickNode",{node:i[0],captor:a.data}),j.dispatchEvent("doubleClickNodes",{node:i,captor:a.data})):k.length?(j.dispatchEvent("doubleClickEdge",{edge:k[0],captor:a.data}),j.dispatchEvent("doubleClickEdges",{edge:k,captor:a.data})):j.dispatchEvent("doubleClickStage",{captor:a.data}))}function f(a){j.settings("eventsEnabled")&&(j.dispatchEvent("rightClick",a.data),i=c(a),k=d(a),i.length?(j.dispatchEvent("rightClickNode",{node:i[0],captor:a.data}),j.dispatchEvent("rightClickNodes",{node:i,captor:a.data})):k.length?(j.dispatchEvent("rightClickEdge",{edge:k[0],captor:a.data}),j.dispatchEvent("rightClickEdges",{edge:k,captor:a.data})):j.dispatchEvent("rightClickStage",{captor:a.data}))}function g(a){if(j.settings("eventsEnabled")){var b,c,d,e,f=[],g=[];for(b in l)f.push(l[b]);for(l={},c=0,d=f.length;d>c;c++)j.dispatchEvent("outNode",{node:f[c],captor:a.data});for(f.length&&j.dispatchEvent("outNodes",{nodes:f,captor:a.data}),m={},c=0,e=g.length;e>c;c++)j.dispatchEvent("outEdge",{edge:g[c],captor:a.data});g.length&&j.dispatchEvent("outEdges",{edges:g,captor:a.data})}}function h(a){if(j.settings("eventsEnabled")){i=c(a),k=d(a);var b,e,f,g,h=[],n=[],o={},p=i.length,q=[],r=[],s={},t=k.length;for(b=0;p>b;b++)f=i[b],o[f.id]=f,l[f.id]||(n.push(f),l[f.id]=f);for(e in l)o[e]||(h.push(l[e]),delete l[e]);for(b=0,p=n.length;p>b;b++)j.dispatchEvent("overNode",{node:n[b],captor:a.data});for(b=0,p=h.length;p>b;b++)j.dispatchEvent("outNode",{node:h[b],captor:a.data});for(n.length&&j.dispatchEvent("overNodes",{nodes:n,captor:a.data}),h.length&&j.dispatchEvent("outNodes",{nodes:h,captor:a.data}),b=0;t>b;b++)g=k[b],s[g.id]=g,m[g.id]||(r.push(g),m[g.id]=g);for(e in m)s[e]||(q.push(m[e]),delete m[e]);for(b=0,t=r.length;t>b;b++)j.dispatchEvent("overEdge",{edge:r[b],captor:a.data});for(b=0,t=q.length;t>b;b++)j.dispatchEvent("outEdge",{edge:q[b],captor:a.data});r.length&&j.dispatchEvent("overEdges",{edges:r,captor:a.data}),q.length&&j.dispatchEvent("outEdges",{edges:q,captor:a.data})}}var i,k,l={},m={};a.bind("click",b),a.bind("mousedown",h),a.bind("mouseup",h),a.bind("mousemove",h),a.bind("mouseout",g),a.bind("doubleclick",e),a.bind("rightclick",f),j.bind("render",h)}var f,g,h,i,j=this;for(f=0,g=this.captors.length;g>f;f++)e(this.captors[f])}}.call(this),function(){"use strict";if("undefined"==typeof sigma)throw"sigma is not declared";sigma.utils.pkg("sigma.misc"),sigma.misc.bindDOMEvents=function(a){function b(a){this.attr=function(b){return a.getAttributeNS(null,b)},this.tag=a.tagName,this.class=this.attr("class"),this.id=this.attr("id"),this.isNode=function(){return!!~this.class.indexOf(g.settings("classPrefix")+"-node")},this.isEdge=function(){return!!~this.class.indexOf(g.settings("classPrefix")+"-edge")},this.isHover=function(){return!!~this.class.indexOf(g.settings("classPrefix")+"-hover")}}function c(a){if(g.settings("eventsEnabled")){g.dispatchEvent("click",a);var c=new b(a.target);c.isNode()?g.dispatchEvent("clickNode",{node:h.nodes(c.attr("data-node-id"))}):g.dispatchEvent("clickStage"),a.preventDefault(),a.stopPropagation()}}function d(a){if(g.settings("eventsEnabled")){g.dispatchEvent("doubleClick",a);var c=new b(a.target);c.isNode()?g.dispatchEvent("doubleClickNode",{node:h.nodes(c.attr("data-node-id"))}):g.dispatchEvent("doubleClickStage"),a.preventDefault(),a.stopPropagation()}}function e(a){var c=a.toElement||a.target;if(g.settings("eventsEnabled")&&c){var d=new b(c);if(d.isNode())g.dispatchEvent("overNode",{node:h.nodes(d.attr("data-node-id"))});else if(d.isEdge()){var e=h.edges(d.attr("data-edge-id"));g.dispatchEvent("overEdge",{edge:e,source:h.nodes(e.source),target:h.nodes(e.target)})}}}function f(a){var c=a.fromElement||a.originalTarget;if(g.settings("eventsEnabled")){var d=new b(c);if(d.isNode())g.dispatchEvent("outNode",{node:h.nodes(d.attr("data-node-id"))});else if(d.isEdge()){var e=h.edges(d.attr("data-edge-id"));g.dispatchEvent("outEdge",{edge:e,source:h.nodes(e.source),target:h.nodes(e.target)})}}}var g=this,h=this.graph;a.addEventListener("click",c,!1),sigma.utils.doubleClick(a,"click",d),a.addEventListener("touchstart",c,!1),sigma.utils.doubleClick(a,"touchstart",d),a.addEventListener("mouseover",e,!0),a.addEventListener("mouseout",f,!0)}}.call(this),function(){"use strict";if("undefined"==typeof sigma)throw"sigma is not declared";sigma.utils.pkg("sigma.misc"),sigma.misc.drawHovers=function(a){function b(){var b,f,g,h,i,j=c.contexts.hover.canvas,k=c.settings("defaultNodeType"),l=c.settings("defaultEdgeType"),m=sigma.canvas.hovers,n=sigma.canvas.edgehovers,o=sigma.canvas.extremities,p=c.settings.embedObjects({prefix:a});if(c.contexts.hover.clearRect(0,0,j.width,j.height),p("enableHovering")&&p("singleHover")&&Object.keys(d).length&&(h=d[Object.keys(d)[0]],(m[h.type]||m[k]||m.def)(h,c.contexts.hover,p)),p("enableHovering")&&!p("singleHover"))for(b in d)(m[d[b].type]||m[k]||m.def)(d[b],c.contexts.hover,p);if(p("enableEdgeHovering")&&p("singleHover")&&Object.keys(e).length&&(i=e[Object.keys(e)[0]],f=c.graph.nodes(i.source),g=c.graph.nodes(i.target),i.hidden||((n[i.type]||n[l]||n.def)(i,f,g,c.contexts.hover,p),p("edgeHoverExtremities")?(o[i.type]||o.def)(i,f,g,c.contexts.hover,p):((sigma.canvas.nodes[f.type]||sigma.canvas.nodes.def)(f,c.contexts.hover,p),(sigma.canvas.nodes[g.type]||sigma.canvas.nodes.def)(g,c.contexts.hover,p)))),p("enableEdgeHovering")&&!p("singleHover"))for(b in e)i=e[b],f=c.graph.nodes(i.source),g=c.graph.nodes(i.target),i.hidden||((n[i.type]||n[l]||n.def)(i,f,g,c.contexts.hover,p),p("edgeHoverExtremities")?(o[i.type]||o.def)(i,f,g,c.contexts.hover,p):((sigma.canvas.nodes[f.type]||sigma.canvas.nodes.def)(f,c.contexts.hover,p),(sigma.canvas.nodes[g.type]||sigma.canvas.nodes.def)(g,c.contexts.hover,p)))}var c=this,d={},e={};this.bind("overNode",function(a){var c=a.data.node;c.hidden||(d[c.id]=c,b())}),this.bind("outNode",function(a){delete d[a.data.node.id],b()}),this.bind("overEdge",function(a){var c=a.data.edge;c.hidden||(e[c.id]=c,b())}),this.bind("outEdge",function(a){delete e[a.data.edge.id],b()}),this.bind("render",function(){b()})}}.call(this);
--------------------------------------------------------------------------------
/jslib/knowledge.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Knowledge graph query
3 | * @author TaoPR (github.com/starcolon)
4 | */
5 |
6 | var Knw = {}
7 | var ODatabase = require('orientjs').ODatabase;
8 | var Promise = require('bluebird');
9 |
10 | /**
11 | * Make a connection to the OrientDB
12 | * @param {string} database name
13 | * @param {string} username
14 | * @param {string} password
15 | * @param {bool} force reconnect?
16 | * @return {Promise} which wraps a [db] variable
17 | */
18 | Knw.connect = function(db,usrname,psw,reconnect=false){
19 | // Do nothing if connected
20 | if (Knw.db && !reconnect)
21 | return Promise.resolve(Knw.db);
22 |
23 | Knw.db = new ODatabase({
24 | host: 'localhost',
25 | port: 2424,
26 | username: usrname,
27 | password: psw,
28 | name: db
29 | });
30 |
31 | return Knw.db.open();
32 | }
33 |
34 |
35 | /**
36 | * List all vertices where condition is met
37 | * @param {Object} must satisfy OrientDB query format
38 | */
39 | Knw.nodes = function(condition){
40 | if (condition)
41 | return Knw.db.select().from('V').where(condition).all();
42 | return Knw.db.select().from('V').all();
43 | }
44 |
45 | Knw.allTopics = () => Knw.nodes({'@class': 'TOPIC'})
46 | Knw.allKeywords = () => Knw.nodes({'@class': 'KEYWORD'})
47 |
48 | /**
49 | * List all edges where condition is met
50 | * @param {Object} must satisfy OrientDB query format
51 | */
52 | Knw.edges = function(condition){
53 | if (condition)
54 | return Knw.db.select().from('E').where(condition).all();
55 | return Knw.db.select().from('E').all();
56 | }
57 |
58 | /**
59 | * List all outbound edges from the specified node
60 | * @param {Object} node object
61 | * @return {Promise}
62 | */
63 | Knw.getOutE = function(limit){
64 | return function(node){
65 | var linked = {'out': node.id}
66 | var output = (node.type=='TOPIC') ?
67 | Knw.db.select().from('has').where(linked) :
68 | Knw.db.select().from('rel').where(linked);
69 |
70 | if (limit){
71 | return output.limit(limit).all();
72 | }
73 | else
74 | return output.all();
75 | }
76 | }
77 |
78 | /**
79 | * List all outbound index edges from the specified node
80 | * @param {Object} node object
81 | * @return {Promise}
82 | */
83 | Knw.getInboundIndex = function(limit){
84 | return function(node){
85 |
86 | var linked = {'in': node.id}
87 | var output = Knw.db
88 | .select().from('e')
89 | .where(linked)
90 | .order('weight desc')
91 |
92 | if (limit){
93 | return output.limit(limit).all();
94 | }
95 | else
96 | return output.all();
97 | }
98 | }
99 |
100 | module.exports = Knw;
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "vor-visualiser",
3 | "version": "0.0.1",
4 | "description": "A knowledge graph visualiser module",
5 | "main": "visualise.js",
6 | "directories": {
7 | "test": "test"
8 | },
9 | "scripts": {
10 | "test": "echo \"Error: no test specified\" && exit 1"
11 | },
12 | "repository": {
13 | "type": "git",
14 | "url": "git+https://github.com/starcolon/vor-knowledge-graph.git"
15 | },
16 | "keywords": [
17 | "vor",
18 | "graph",
19 | "knowledge",
20 | "visualise",
21 | "visualize",
22 | "visualizer",
23 | "visualiser",
24 | "render",
25 | "d3"
26 | ],
27 | "author": "TaoPR (StarColon)",
28 | "license": "ISC",
29 | "bugs": {
30 | "url": "https://github.com/starcolon/vor-knowledge-graph/issues"
31 | },
32 | "homepage": "https://github.com/starcolon/vor-knowledge-graph#readme",
33 | "dependencies": {
34 | "bluebird": "^3.4.1",
35 | "orientjs": "^2.2.2",
36 | "sigma": "^1.1.0"
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/pos-patterns:
--------------------------------------------------------------------------------
1 | NN-NN
2 | NN
3 | NNS
4 | NNP
5 | JJ
6 | CD
7 | JJ-JJ
8 | NN-JJ-JJ
9 | NNP-NN
10 | JJ-NN
11 | JJ-NNS
12 | JJ-NN-NN
13 |
--------------------------------------------------------------------------------
/pos-stopwords:
--------------------------------------------------------------------------------
1 | the
2 | i.e.
3 | e.g.
4 | etc.
5 | such
6 | other
7 | words
8 | way
9 | respectively
--------------------------------------------------------------------------------
/pylib/jobmq/rabbit.py:
--------------------------------------------------------------------------------
1 | """
2 | RabbitMQ job pipe
3 | @author TaoPR (github.com/starcolon)
4 | """
5 |
6 | import pika
7 | import signal
8 | import time
9 |
10 | class JobMQ:
11 | def __init__(server_addr,q):
12 | conn = pika.BlockingConnection(pika.ConnectionParameters(server_addr))
13 | channel = conn.channel()
14 | channel.queue_declare(queue=q)
15 |
16 | self.conn = conn
17 | self.channel = channel
18 | self.q_name = q
19 |
20 | def publish(job):
21 | self.channel.basic_publish(
22 | exchange='',
23 | routing_key=self.q_name,
24 | body=job)
25 |
26 | def next():
27 | pass
28 |
29 | # Message generator
30 | def iter(transformation=lambda x:x):
31 | TIMEOUT = 5 # seconds
32 | # Start the awaiting signal
33 | try:
34 | def __timeout(signum,frame):
35 | raise StopIteration
36 |
37 | signal.signal(signal.SIGALRM,__timeout)
38 | signal.alarm(TIMEOUT)
39 | for methodframe, prop, body in self.channel.consume(self.q_name):
40 | signal.alarm(0)
41 | job = transformation(body.decode('utf-8'))
42 |
43 | yield job
44 | self.channel.basic_ack(methodframe.delivery_tag)
45 |
46 | # Startover a new timer
47 | signal.alarm(TIMEOUT)
48 |
49 | except StopIteration as e:
50 | signal.alarm(0) # Cancel the timer
51 | print(colored('--Timeout, no further message--','yellow'))
52 | raise
53 |
54 | except Exception as e:
55 | signal.alarm(0)
56 | print(colored('--Exception broke the iteration--','yellow'))
57 | raise
58 |
59 | def end():
60 | print('Ending MQ # ',self.q_name)
61 | try:
62 | self.conn.close()
63 | except pika.exceptions.ConnectionClosed:
64 | print(colored('MQ appeared offline','red'))
65 |
66 |
--------------------------------------------------------------------------------
/pylib/knowledge/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tao-pr/vor-knowledge-graph/7c7140b682796b997871751090ac3a1b3ea36eee/pylib/knowledge/__init__.py
--------------------------------------------------------------------------------
/pylib/knowledge/datasource.py:
--------------------------------------------------------------------------------
1 | """
2 | Mining data source access
3 | @author TaoPR (github.com/starcolon)
4 | """
5 | from pymongo import MongoClient
6 | from pymongo import InsertOne
7 |
8 | class MineDB:
9 |
10 | def __init__(self,host,db,coll):
11 | addr = "mongodb://{0}:27017/".format(host)
12 | self.mongo = MongoClient(addr)
13 | self.db = self.mongo[db]
14 | self.src = self.db[coll]
15 |
16 | def count(self,conditions={}):
17 | return self.src.count(conditions)
18 |
19 | def query(self,conditions={},field=None,skip=0):
20 | query = self.src.find(conditions) if skip==0 else self.src.find(filter=conditions,skip=skip)
21 | for n in query:
22 | # No field name specified, generate the entire record
23 | if field is None:
24 | yield n
25 | # Generate the specified field
26 | else:
27 | yield n[field]
28 |
29 | def update(self,criteria,updater):
30 | self.src.update_one(criteria,updater)
31 |
32 | def insert(self,record):
33 | self.src.insert_one(record)
34 |
35 | def insert_many(self,records):
36 | new_records = [InsertOne(r) for r in records]
37 | self.src.bulk_write(new_records)
38 |
--------------------------------------------------------------------------------
/pylib/knowledge/graph.py:
--------------------------------------------------------------------------------
1 | """
2 | Knowledge graph
3 | @author TaoPR (github.com/starcolon)
4 | """
5 |
6 | import pyorient
7 | from pyorient.exceptions import PyOrientSchemaException
8 | import numpy as np
9 | import os.path
10 | import json
11 | from termcolor import colored
12 |
13 | class Knowledge:
14 |
15 | """
16 | Create a new knowledge graph connection.
17 | The constructor itself is idemponent which means it creates
18 | a new database and fill in the structure if it doesn't exist.
19 | Otherwise, it just open the existing connection.
20 | """
21 | def __init__(self,host,dbname,usrname,psw):
22 | self.orient = pyorient.OrientDB(host,2424)
23 | self.__session = self.orient.connect(usrname,psw)
24 | if self.orient.db_exists(dbname):
25 | print(colored('Connecting OrientDB: {0}'.format(dbname),'green'))
26 | self.orient.db_open(dbname,usrname,psw)
27 | else:
28 | print(colored('Creating OrientDB: {0}'.format(dbname),'magenta'))
29 | self.orient.db_create(
30 | dbname,
31 | pyorient.DB_TYPE_GRAPH
32 | )
33 | self.orient.db_open(dbname,usrname,psw)
34 |
35 | # Make sure {Edge} and {Vertex} classes are recognised by the DB
36 | self.__prepare_classes()
37 |
38 |
39 | """
40 | Create initial classes (Edge + Vertex)
41 | """
42 | def __prepare_classes(self):
43 | try:
44 | self.orient.command('create class TOPIC extends V')
45 | self.orient.command('create class KEYWORD extends V')
46 | self.orient.command('create class REL extends E')
47 | self.orient.command('create class HAS extends E')
48 | except PyOrientSchemaException as e:
49 | print(colored('[WARNING] Preparing graph schema','yellow'))
50 | print(colored(e,'yellow'))
51 |
52 |
53 | """
54 | Permanently remove all edges and vertices
55 | """
56 | def clear(self):
57 | self.orient.command('delete vertex TOPIC')
58 | self.orient.command('delete vertex KEYWORD')
59 | self.orient.command('delete edge') # May this be redundant?
60 | print(colored('[Graph clearance] done','yellow'))
61 |
62 | """
63 | Add a set of new knowledge links
64 | @param {str} topic
65 | @param {list} of {str} words
66 | @param {float} weight of the link
67 | @param {bool} verbose
68 | """
69 | def add(self,topic,words,weights,verbose):
70 |
71 | if verbose: print(colored('Adding : ','green'), topic, ' ===> ', words)
72 |
73 | # Escape some unwanted characters from topic and words
74 | unwanted = "'"
75 | topic = topic.replace(unwanted," ")
76 | words = map(lambda w: w.replace(unwanted, " "), words)
77 | weights = iter(weights) if weights is not None else None
78 |
79 | # Add a new topic if not exist
80 | queryTopic = "select from TOPIC where title='{0}'".format(topic)
81 | if len(self.orient.command(queryTopic))==0:
82 | if verbose: print(colored('New topic added: ','green'), topic)
83 | self.orient.command("create vertex TOPIC set title='{0}'"
84 | .format(topic))
85 |
86 | # Add new words which don't exist
87 | for w in words:
88 | queryWord = "select from KEYWORD where w='{0}'".format(w)
89 | if len(self.orient.command(queryWord))==0:
90 | if verbose: print(colored('New word added: ','green'), w)
91 | self.orient.command("create vertex KEYWORD set w='{0}'".format(w))
92 |
93 | # Add a link from {topic} => {word}
94 | if verbose: print(colored('New link [{0}] HAS => [{1}]'.format(topic,w),'green'))
95 |
96 | # If [weight] is specified,
97 | # Create an inverted-index edge from
98 | # [keyword] => [topic]
99 | if weights is None:
100 | # General relation
101 | self.orient.command("create edge HAS from ({0}) to ({1})"\
102 | .format(queryTopic, queryWord))
103 | else:
104 | # Invert-index
105 | weight = next(weights)
106 | self.orient.command("create edge INDEX from ({0}) to ({1}) SET weight={2}"\
107 | .format(queryWord, queryTopic, weight))
108 |
109 | # Add links between words
110 | for w in words:
111 | # Add a link to sibling words (words which co-exist in same sentence)
112 | siblings = (u for u in words if not w == u)
113 | for s in siblings:
114 | querySib = "select from KEYWORD where w='{0}'".format(s)
115 | self.orient.command("create edge REL from ({0}) to ({1})".
116 | format(queryWord,querySib))
117 |
118 | """
119 | {Generator} Enumurate keywords by strength of connections
120 | """
121 | def top_keywords(self):
122 | query = "select w, in().size() as cnt from keyword order by cnt desc"
123 | for k in self.orient.query(query):
124 | yield k
125 |
126 | """
127 | {Generator} Enumurate all topics
128 | """
129 | def __iter__(self):
130 | query = "select from topic"
131 | for k in self.orient.query(query):
132 | yield k
133 |
134 | """
135 | {Generator} Enumerate all keywords in a topic
136 | @param {str} topic title
137 | """
138 | def keywords_in_topic(self, topic, with_edge_count=False):
139 | subquery = "select expand(out()) from topic where title = '{0}'".format(topic)
140 | query = "select w from ({0})".format(subquery) \
141 | if not with_edge_count \
142 | else "select w, in().size() as freq from (select expand(out()) from topic where title = '{}')".format(topic)
143 | for k in self.orient.query(query):
144 | yield k
145 |
146 | """
147 | {Generator} Enumerate all topics which the given keyword
148 | belong to
149 | @param {str} keyword to query
150 | """
151 | def topics_which_have(self,w):
152 | query = "select in().title from KEYWORD where w='{}'".format(w)
153 | for k in self.orient.query(query):
154 | yield k
155 |
156 |
--------------------------------------------------------------------------------
/pylib/spider/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tao-pr/vor-knowledge-graph/7c7140b682796b997871751090ac3a1b3ea36eee/pylib/spider/__init__.py
--------------------------------------------------------------------------------
/pylib/spider/crawler.py:
--------------------------------------------------------------------------------
1 | """
2 | Website crawler with specific reader
3 | @author TaoPR (github.com/starcolon)
4 | """
5 |
6 | import urllib.request
7 | from htmldom import htmldom
8 | from termcolor import colored
9 |
10 | """
11 | Download and scrape the content from the given URL
12 | @param {string} URL locator of the website to scrape
13 | @param {list} of tuples: (field name, selector function)
14 | @param {bool} turn on/off verbose output
15 | @return {object} scraped content
16 | """
17 | def download_page(url,selectors,verbose=False):
18 | # Download the entire page HTML, processed as a DOM tree
19 | print(colored('Fetching: ','green') + colored(url,'cyan'))
20 | page = htmldom.HtmlDom(url).createDom()
21 |
22 | # Apply selector functions in order to create
23 | # a content package
24 | content = {}
25 | for tup in selectors:
26 | field, selector = tup
27 | if verbose:
28 | print(colored(' Mapping : ','green'), field)
29 | content[field] = selector(page)
30 |
31 | return content
--------------------------------------------------------------------------------
/pylib/spider/wiki.py:
--------------------------------------------------------------------------------
1 | """
2 | Wikipedia single page crawler and scraper
3 | @author TaoPR (github.com/starcolon)
4 | """
5 |
6 | import re
7 | from termcolor import colored
8 | from . import crawler
9 |
10 | def download_wiki(url,verbose=False):
11 | selector = [
12 | ('title', wiki_title),
13 | ('contents', wiki_contents),
14 | ('rels', wiki_rels)
15 | ]
16 | content = crawler.download_page(url,selector,verbose)
17 |
18 | if verbose:
19 | print(colored('[Downloaded : {0}]'.format(url),'cyan'))
20 | print(colored(' title : ','cyan'), content['title'])
21 | print(colored(' rels : ','cyan'), len(content['rels']))
22 |
23 | return content
24 |
25 | def wiki_title(page):
26 | return page.find('h1#firstHeading').text()
27 |
28 | """
29 | Scrape the main article content from the specified
30 | wikipedia page
31 | """
32 | def wiki_contents(page):
33 | contents = []
34 | paragraphs = page.find('#bodyContent p')
35 | for p in paragraphs:
36 | contents.append(p.text())
37 |
38 | return contents
39 |
40 | """
41 | Scrape all related links (to other wiki pages)
42 | from the current wikipedia page
43 | """
44 | def wiki_rels(page):
45 | links = []
46 | for li in page.find('ul li a'):
47 |
48 | href = li.attr('href')
49 |
50 | # Skip unuseable or unwanted links
51 | if href[0]=='#' or \
52 | ':' in href or \
53 | '//' in href or \
54 | 'index.php' in href or \
55 | 'Main_Page' in href or \
56 | re.search('\/+\w{2,5}.wiki[m|p]edia.org\/*.*', href):
57 | continue
58 |
59 | links.append(href)
60 |
61 | return links
--------------------------------------------------------------------------------
/pylib/text/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tao-pr/vor-knowledge-graph/7c7140b682796b997871751090ac3a1b3ea36eee/pylib/text/__init__.py
--------------------------------------------------------------------------------
/pylib/text/cleanser.py:
--------------------------------------------------------------------------------
1 | """
2 | Text cleanser
3 | @author TaoPR (github.com/starcolon)
4 | """
5 |
6 | import re
7 |
8 | """
9 | Remove unwanted tokens from the given text
10 | """
11 | def cleanse(txt):
12 | txt_ = txt
13 | for p in patterns():
14 | txt_ = re.sub(p, ' ', txt_)
15 | return txt_
16 |
17 | def patterns():
18 | return [ \
19 | r'(',\
20 | r')',\
21 | r',',\
22 | r'{.+}',\
23 | r'<.*>',\
24 | r'.+;',\
25 | r'\.($| )',\
26 | r'\n',\
27 | r'[\d+]',
28 | r' \w{1,2}\.',
29 | r'\[.*\]',
30 | r'title=\"\w*\"',
31 | r'\(\)']
32 |
33 |
--------------------------------------------------------------------------------
/pylib/text/intent.py:
--------------------------------------------------------------------------------
1 | """
2 | Sentence intent classifier
3 | @author TaoPR (github.com/starcolon)
4 | """
5 | import numpy as np
6 | import os.path
7 | import pickle
8 | import json
9 | from termcolor import colored
10 | from sklearn.cluster import KMeans
11 | from sklearn.preprocessing import LabelEncoder
12 |
13 | """
14 | Create a new instance of intent classifier, label encoder included.
15 | @param {list} distinct list of intent labels (string)
16 | @param {string} classification method
17 | @return {object} classifier object along with the label encoder
18 | """
19 | def new(intent_labels=[],method='kmeans'):
20 |
21 | # Classification methods
22 | methods = {
23 | 'kmeans': KMeans(n_clusters = len(intent_labels))
24 | }
25 |
26 | # Label encoders
27 | encoder = LabelEncoder()
28 | encoder.fit(intent_labels)
29 |
30 | return {
31 | 'clf': methods[method],
32 | 'encoder': encoder
33 | }
34 |
35 |
36 | def save(operations,path):
37 | with open(path,'wb+') as f:
38 | pickle.dump(operations,f)
39 |
40 | def load(path):
41 | with open(path,'rb') as f:
42 | return pickle.load(f)
43 |
44 | """
45 | Load the existing operations or create new if not exist
46 | """
47 | def safe_load(path):
48 | if os.path.isfile(path):
49 | print(colored('Text intent classifier loaded.','cyan'))
50 | return load(path)
51 | else:
52 | print(colored('Text intent classifier created...','yellow'))
53 | return new()
54 |
55 | # Classify multiple vectors at a time using
56 | # the specified trained operations
57 | def classify(opr):
58 | def classify_us(vectors):
59 | vs = opr['clf'].predict(vectors)
60 | return opr['encoder'].inverse_transform(vs)
61 | return classify_us
62 |
63 | def train(opr):
64 | def fit(vectors,labels):
65 | # Make labels numeric
66 | numeric_labels = opr['encoder'].transform(labels)
67 | opr['clf'].fit(vectors,numeric_labels)
68 | return fit
69 |
70 |
--------------------------------------------------------------------------------
/pylib/text/pos_tree.py:
--------------------------------------------------------------------------------
1 | """
2 | POS pattern tree
3 | @author TaoPR (github.com/starcolon)
4 | ---
5 | Pattern tree is built from a list of POS patterns.
6 | It is employed as a sentence parser which captures
7 | and build a mini knowledge graph.
8 | """
9 |
10 | from collections import deque
11 |
12 | class PatternCapture:
13 | def __init__(self):
14 | self.__patterns = []
15 |
16 | """
17 | Read in the list of POS structure from a text file
18 | """
19 | def load(self,path):
20 | with open(path,'r') as f:
21 | self.__patterns = set([p.replace('\n','') for p in f.readlines()])
22 |
23 | """
24 | Save a list of POS structure to a text file
25 | """
26 | def save(self,path):
27 | with open(path,'w') as f:
28 | for p in self.__patterns:
29 | f.write(p+"\n")
30 |
31 | def append(self,p):
32 | self.__patterns.append(p)
33 |
34 | def join(self,delim):
35 | return delim.join(self.__patterns)
36 |
37 | """
38 | Capture the keyword tree of the given sentence
39 | Sample output:
40 | [
41 | ['Ammonia','Phosphate'],
42 | ['Heated',['Potassiam','Nitrate'],'Solution']
43 | ]
44 | @param {list} of tuples of (word,tag)
45 | @return {list} represents a captured tree
46 | """
47 | def capture(self,pos_sentence):
48 | pos_deq = deque(pos_sentence)
49 | tree = []
50 |
51 | # Iterate through the sentence POS tags (greedy capture)
52 | bichain, bichain_tag = deque(),deque()
53 | trichain, trichain_tag = deque(),deque()
54 |
55 | while len(pos_deq)>0:
56 | t,tag = pos_deq.popleft()
57 |
58 | # Accumulate the current chain
59 | bichain.append(t)
60 | bichain_tag.append(tag)
61 | trichain.append(t)
62 | trichain_tag.append(tag)
63 |
64 | # Check if the individual tag matches any of the patterns?
65 | if tag in self.__patterns:
66 | tree.append(t)
67 |
68 | # Check if current bichain matches any patterns?
69 | if len(bichain)==2:
70 | if '-'.join(bichain_tag) in self.__patterns:
71 | tree.append(' '.join(bichain))
72 | # Clean up for the next iteration
73 | bichain.popleft()
74 | bichain_tag.popleft()
75 |
76 | # Check if current trichain matches any patterns?
77 | if len(trichain)==3:
78 | if '-'.join(trichain_tag) in self.__patterns:
79 | tree.append(' '.join(trichain))
80 | # Clean up for the next iteration
81 | trichain.popleft()
82 | trichain_tag.popleft()
83 |
84 | return tree
85 |
86 |
87 |
--------------------------------------------------------------------------------
/pylib/text/structure.py:
--------------------------------------------------------------------------------
1 | """
2 | Sentence pattern semantic extraction module
3 | @author TaoPR (github.com/starcolon)
4 | """
5 |
6 | import numpy as np
7 | import os.path
8 | import pickle
9 | import json
10 | from collections import deque
11 | from termcolor import colored
12 | from textblob import TextBlob
13 |
14 |
15 | """
16 | Extract part of speech structure of the given text
17 | @param {list} of tokenised words
18 | """
19 | def pos_tag(words):
20 | def generate(t):
21 | tags = TextBlob(t).tags
22 | if len(tags)==0:
23 | return None
24 | return tags[0]
25 |
26 | blobs = [generate(t) for t in words]
27 | return [b for b in blobs if b is not None]
28 |
29 | def tag_with_color(words):
30 | pos = pos_tag(words)
31 | tokens = ' | '.join([colored(tag,'yellow') + ':' + t for t,tag in pos])
32 | print(tokens)
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/pylib/text/texthasher.py:
--------------------------------------------------------------------------------
1 | """
2 | Text hashing (vectoriser)
3 | @author TaoPR (github.com/starcolon)
4 | """
5 |
6 | import numpy as np
7 | import os.path
8 | import pickle
9 | import json
10 | from termcolor import colored
11 | from sklearn.feature_extraction.text import TfidfVectorizer
12 | from sklearn.feature_extraction.text import HashingVectorizer
13 | from sklearn.feature_selection import SelectKBest, chi2
14 | from sklearn.linear_model import RidgeClassifier
15 | from sklearn.neighbors import NearestCentroid
16 | from sklearn.preprocessing import Normalizer
17 | from sklearn.pipeline import make_pipeline
18 | from sklearn.decomposition import SparsePCA
19 | from sklearn.decomposition import TruncatedSVD
20 | from sklearn.decomposition import LatentDirichletAllocation
21 |
22 | decomposers = {
23 | 'LDA': lambda n:
24 | LatentDirichletAllocation( # TFIDF --> Topic term
25 | n_topics=n,
26 | max_iter=15
27 | ),
28 | 'PCA': lambda n:
29 | SparsePCA( # NOTE: PCA is supposed to perform slow, so we limit num of iterations
30 | n_components=n,
31 | alpha=1,
32 | max_iter=10
33 | ),
34 | 'SVD': lambda n:
35 | TruncatedSVD(n) # SVD may introduce some estimation losses
36 | }
37 |
38 | # Create a text process pipeline (vectorizer)
39 | def new(n_components=None,stop_words=[],decomposition='SVD'):
40 |
41 | # Word stemming (English)
42 | # TAOTODO:
43 |
44 | # Prepare vectoriser engines
45 | idf = TfidfVectorizer(
46 | ngram_range=(1,3), #Unigram,bigram,& trigram
47 | stop_words=stop_words
48 | )
49 |
50 | # Prepare normaliser
51 | norm = Normalizer(norm='l2') # Cosine similarity
52 |
53 | # Prepare dimentionality reducer
54 | if n_components:
55 | reducer = decomposers[decomposition](n_components)
56 | return [idf,reducer,norm]
57 | else:
58 | return [idf,norm]
59 |
60 |
61 | def save(operations,path):
62 | with open(path,'wb+') as f:
63 | pickle.dump(operations,f)
64 |
65 | def load(path):
66 | with open(path,'rb') as f:
67 | return pickle.load(f)
68 |
69 | """
70 | Load the transformer pipeline object
71 | from the physical file,
72 | or initialise a new object if the file doesn't exist
73 | """
74 | def safe_load(path,n_components,stop_words,decomposition):
75 | if os.path.isfile(path):
76 | print(colored('Text hasher loaded.','cyan'))
77 | return load(path)
78 | else:
79 | print(colored('Text hasher created...','yellow'))
80 | return new(n_components,stop_words,decomposition)
81 |
82 |
83 | def hash(operations,learn=False,verbose=True):
84 | # @param {iterable} of string
85 | def hash_me(dataset):
86 | x = dataset
87 |
88 | if learn:
89 | for i in range(len(operations)):
90 | verbose and print(colored('Processing ... #{0} : {1}'.format(i,type(operations[i])),'grey'))
91 | x = operations[i].fit_transform(x)
92 | else:
93 | for i in range(len(operations)):
94 | x = operations[i].transform(x)
95 |
96 | return x
97 | return hash_me
98 |
99 |
100 |
101 |
--------------------------------------------------------------------------------
/repl_word2vec.py:
--------------------------------------------------------------------------------
1 | """
2 | Play with word2vec model
3 | @author TaoPR (github.com/starcolon)
4 | """
5 |
6 | import os
7 | import sys
8 | import argparse
9 | import word2vec
10 | from termcolor import colored
11 |
12 | arguments = argparse.ArgumentParser()
13 | arguments.add_argument('--modelpath', type=str, default='./models/word2vec.bin', help='Path of the word2vec binary model.')
14 | args = vars(arguments.parse_args(sys.argv[1:]))
15 |
16 | def repl(model):
17 | print(colored('[Model] Loaded:','cyan'))
18 | print('... Model shape : {}'.format(model.vectors.shape))
19 | print('... Clusters : {}'.format(model.clusters))
20 | while True:
21 | w = input('Enter word to test : ')
22 | try:
23 | indexes, metrics = model.cosine(w)
24 | print('... indexes : {}'.format(indexes))
25 | print('... metrics : {}'.format(metrics))
26 | print('... similar : {}'.format(model.vocab[indexes]))
27 | print('... response : ')
28 | print(model.generate_response(indexes, metrics).tolist())
29 | except Exception:
30 | print(colored('... Vocab not recognised by the model.','red'))
31 |
32 |
33 | if __name__ == '__main__':
34 | # Load the word2vec model
35 | model_path = os.path.realpath(args['modelpath'])
36 | if not os.path.isfile(model_path):
37 | print(colored('[ERROR] word2vec model does not exist.','red'))
38 | raise RuntimeError('Model does not exist')
39 | print(colored('[Model] loading binary model.','cyan'))
40 | model = word2vec.WordVectors.from_binary(model_path, encoding='ISO-8859-1')
41 | repl(model)
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | htmldom == 2.0
2 | nltk == 3.6.6
3 | numpy == 1.22.0
4 | pika == 0.10.0
5 | pybloom_live == 2.1.0
6 | pymongo == 3.2.2
7 | pyorient == 1.5.4
8 | termcolor == 1.1.0
9 | textblob == 0.11.0
10 | word2vec == 0.9.1
11 | scikit_learn == 0.18.1
12 |
--------------------------------------------------------------------------------
/test/__pycache__/crawler.cpython-34.pyc:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tao-pr/vor-knowledge-graph/7c7140b682796b997871751090ac3a1b3ea36eee/test/__pycache__/crawler.cpython-34.pyc
--------------------------------------------------------------------------------
/test/crawler.py:
--------------------------------------------------------------------------------
1 | """
2 | Spider / crawler test suite
3 | """
4 |
5 | import re
6 | from pprint import pprint
7 | from pylib.spider import wiki
8 |
9 | def test_wiki_crawl():
10 | wiki_url = 'https://en.wikipedia.org/wiki/Celery'
11 | content = wiki.download_wiki(wiki_url,verbose=True)
12 |
13 | celery_first_content = ["Celery",
14 | "(Apium graveolens",
15 | "), a marshland plant variety",
16 | "in the family Apiaceae",
17 | ", has been cultivated as a vegetable",
18 | "since antiquity. Depending on location and cultivar, either its stalks, leaves, or hypocotyl",
19 | "are eaten and used in cooking."
20 | ]
21 |
22 | assert content['title'] == 'Celery'
23 | assert len(content['contents']) > 0
24 | assert content['contents'][0] == "\n".join(celery_first_content)
25 | assert content['rels'][:3] == [
26 | '/wiki/Microgram',
27 | '/wiki/Milligram',
28 | '/wiki/International_unit'
29 | ]
--------------------------------------------------------------------------------
/visualise.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Knowledge graph visualiser module
3 | * @author TaoPR (github.com/starcolon)
4 | */
5 |
6 | var args = process.argv.slice(2);
7 | var colors = require('colors');
8 | var fs = require('fs');
9 | var Promise = require('bluebird');
10 |
11 |
12 | // Validate the arguments initially
13 | (function validateArgs(args){
14 | if (args.length==0){
15 | console.error('[ERROR] You need to supply OrientDB root database like:'.red);
16 | console.error('$ node visualise MYPASSWORD'.red);
17 | process.exit(1);
18 | }
19 | })(args);
20 |
21 | const usrname = 'root';
22 | var password = args[0];
23 |
24 | var indexGraphMapper = function(KB){
25 | return function(nodes){
26 | var nodeHash = new Set();
27 | var nodes = nodes.map(n => {
28 | nodeHash.add(n['@rid'].toString());
29 | return {
30 | id: n['@rid'],
31 | type: n['@class'],
32 | label: n['@class']=='TOPIC' ? n.title : n.w,
33 | color: n['@class']=='TOPIC' ? '#F00000' : '#FFAAAA',
34 | value: n['@class']=='TOPIC' ? 5 : 3,
35 | borderWidth: n['@class']=='TOPIC' ? 3 : 1
36 | }
37 | })
38 |
39 | nodeHash = Array.from(nodeHash)
40 |
41 | var topicOnly = (n) => n.type == 'TOPIC';
42 | var keywordOnly = (n) => n.type == 'KEYWORD';
43 | var allKeywords = nodes.filter(keywordOnly);
44 | var allTopics = nodes.filter(topicOnly);
45 |
46 | // Enumurate index between [nodes] <==> [keywords]
47 | console.log('Enurating edges...');
48 | var edges = [];
49 | var collectEdges = (iter) => iter.then((es) => {
50 | es.forEach(e => {
51 |
52 | var _in = e['in'].toString();
53 | var _out = e['out'].toString();
54 |
55 | edges.push({
56 | from: nodeHash.indexOf(_in),
57 | to: nodeHash.indexOf(_out),
58 | value: e['weight']
59 | });
60 | })
61 | });
62 |
63 | // Prepare edge enumuration jobs
64 | var jobs = allTopics
65 | .map(KB.getInboundIndex(42))
66 | .map(collectEdges);
67 |
68 | return Promise
69 | .all(jobs).then(() => [nodes,edges])
70 | .then((p) => {
71 |
72 | var [nodes, edges] = p;
73 |
74 | console.log('Remapping nodes...')
75 | nodes = nodes.map( n => {
76 | n.id = nodeHash.indexOf(n.id.toString()); // @rid => integer
77 | return n
78 | })
79 |
80 | // Remove unlinked nodes from the graph
81 | var allInboundNodes = new Set(edges.map(e => e.to));
82 | var allOutboundNodes = new Set(edges.map(e => e.from));
83 | nodes = nodes.filter(n =>
84 | allOutboundNodes.has(n.id) ||
85 | allInboundNodes.has(n.id));
86 |
87 | var graph = { nodes: nodes, edges: edges };
88 | var sgraph = JSON.stringify(graph);
89 |
90 | return [graph,sgraph]
91 | });
92 | }
93 | }
94 |
95 | var circularGraphMapper = function(KB){
96 | return function(nodes){
97 | // Reduce the form of nodes so they become renderable.
98 | var L = nodes.filter((n) => n['@class']=='TOPIC').length;
99 | var degreeStep = 2*Math.PI/L;
100 | var i = -1;
101 |
102 | nodes = nodes.map((n) => {
103 | i++;
104 | var randomDegree = Math.random()*2*Math.PI;
105 | var randomR = Math.random()*0.8;
106 | var x = randomR * Math.cos(randomDegree);
107 | var y = randomR * Math.sin(randomDegree);
108 |
109 | return {
110 | id: n['@rid'],
111 | type: n['@class'],
112 | label: n['@class']=='TOPIC' ? n.title : n.w,
113 | x: n['@class']=='TOPIC' ? Math.cos(degreeStep*i) : x,
114 | y: n['@class']=='TOPIC' ? Math.sin(degreeStep*i) : y,
115 | size: n['@class']=='TOPIC' ? 10 : 1,
116 | color: n['@class']=='TOPIC' ? '#F00000' : '#FFAAAA'
117 | }
118 | })
119 |
120 | // Take outbound edges of those underlying nodes
121 | console.log('Enumerating edges...');
122 | var edges = [];
123 | var collectEdges = (node) => node.then((e) => {
124 | edges.push(e);
125 | });
126 | var topic = (n) => n.type=='TOPIC';
127 |
128 | // Prepare edge enumuration jobs
129 | var jobs = nodes
130 | .filter(topic)
131 | .map(KB.getOutE(100))
132 | .map(collectEdges);
133 |
134 | // Make sure edges of all underlying nodes are processed.
135 | return Promise
136 | .all(jobs).then(() => [nodes,edges])
137 | .then((p) => {
138 |
139 | console.log('Transforming nodes & edges ...')
140 | var [nodes, edges] = p;
141 |
142 | // Flatten edges
143 | edges = edges.reduce((a,b) => a.concat(b), []);
144 |
145 | // Make all edges renderable
146 | edges = edges.map((e) => {
147 | return {
148 | id: Math.random()*10000,
149 | source: e.out,
150 | target: e.in,
151 | type: 'curve'
152 | }
153 | })
154 |
155 | var graph = { nodes: nodes, edges: edges };
156 | var sgraph = JSON.stringify(graph);
157 |
158 | return [graph,sgraph]
159 | })
160 | }
161 | }
162 |
163 | function saveToJSON(outputPath){
164 | return function (graphArray){
165 |
166 | var graph = graphArray[0];
167 | var sgraph = graphArray[1];
168 |
169 | return new Promise((done,reject) => {
170 | console.log('Initialising I/O ...');
171 | var content = `function getGraph(){ return ${sgraph} }`;
172 | fs.writeFile(`./HTML/${outputPath}`,content,(err) => {
173 | console.log('Serialising graph to JS ...'.green);
174 | console.log(` ${graph.nodes.length} nodes`);
175 | console.log(` ${graph.edges.length} links`);
176 | if (err){
177 | console.error('Serialisation failed...'.red);
178 | console.error(err);
179 | return reject();
180 | }
181 | else{
182 | console.log('Graph HTML is ready in ./HTML/'.green);
183 | return done();
184 | }
185 | })
186 | })
187 | }
188 | }
189 |
190 | //-------------------------------
191 | // [OrientDB data] => [JSON data]
192 | // mapping strategies
193 | //-------------------------------
194 | const dataMapping = [
195 | {
196 | 'name': 'vor',
197 | 'mapper': circularGraphMapper,
198 | 'output': 'graph-data.js'
199 | },
200 | {
201 | 'name': 'vorindex',
202 | 'mapper': indexGraphMapper,
203 | 'output': 'graph-index.js'
204 | }
205 | ]
206 |
207 | /**
208 | * Map [OrientDB data] => [renderable JSON]
209 | * by the predefined mapping strategies
210 | */
211 |
212 | Promise.mapSeries(dataMapping, (db) => {
213 |
214 | console.log('================================'.cyan)
215 | console.log('[Datasource] Processing : '.cyan, db)
216 | console.log('================================'.cyan)
217 |
218 | var KB = require('./jslib/knowledge.js');
219 | var reconnect = true
220 | var mapToJSON = db.mapper;
221 | var outputPath = db.output;
222 |
223 | return KB
224 | .connect(db.name,usrname,password,reconnect)
225 | .catch((e) => {
226 | console.error(`[ERROR] connection to OrientDB [${db.name}] failed.`.red);
227 | console.error(e);
228 | process.exit(1);
229 | })
230 | .then((g) => {
231 | console.log(`[Connected] to OrientDB [${db.name}].`.green);
232 |
233 | // Read all nodes in
234 | var nodes = KB.nodes();
235 | console.log('All nodes retrieved...')
236 |
237 | // Fulfill the values and go on
238 | return Promise.resolve(nodes)
239 | })
240 | .then(mapToJSON(KB))
241 | .then(saveToJSON(outputPath))
242 | })
243 | .then((_) => process.exit())
--------------------------------------------------------------------------------