Repository navigation
Expand file tree
/
Copy pathwikipedia-miner.py
More file actions
executable file
·70 lines (52 loc) · 1.95 KB
/
Copy pathwikipedia-miner.py
File metadata and controls
executable file
·70 lines (52 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
__author__ = 'nlw'
import argparse
import logging
import unittest
import re
from scigraph.api.SciGraph import SciGraph
from scigraph.renderers.TabRenderer import *
from scigraph.renderers.RawRenderer import *
from scigraph.renderers.EntityAnnotationTabRenderer import *
import wikipedia
renderer = EntityAnnotationTabRenderer()
def main():
parser = argparse.ArgumentParser(description='SciGraph-wikiparser'
'Wikipedia section parser',
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-u', '--url', type=str, default="http://scigraph-ontology-dev.monarchinitiative.org/scigraph/",
help='A base URL for SciGraph')
parser.add_argument('-t', '--to', type=str, required=False,
help='Renderer')
parser.add_argument('-p', '--page', type=str, required=True,
help='Page')
args = parser.parse_args()
sg = SciGraph(args.url)
parse_wikipage(sg, args.page)
def parse_wikipage(sg, n):
page = wikipedia.page(n)
print("#TITLE: " + page.title)
sentences = page.content.split("\n")
h2 = 'Main'
h3 = ''
for sentence in sentences:
if (sentence == ""):
continue
m = re.match("=== (.*) ===", sentence)
if m:
h3 = m.group(1)
sentence = h3
else:
m = re.match("== (.*) ==", sentence)
if m:
h2 = m.group(1)
sentence = h2
print("#HEADING: " + h2 + " . " +h3)
rs = sg.annotate(sentence)
print("#TEXT: \"" + sentence + "\"")
print("#SPANS:")
for span in rs.spans:
vals = [str(x) for x in [page.title,h2,h3,span.start, span.end, span.token.id, ",".join(span.token.terms), span.text]]
print("\t".join(vals))
if __name__ == "__main__":
main()