-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathner_extract.py
40 lines (34 loc) · 1.06 KB
/
ner_extract.py
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
'''
Take NER output from GATE
Extract entities
Usage: ner_extract.py (-i FILE ...) (-e ENTITY ...) [-o DIR]
-h --help show this
-o --output DIR specify output directory [default: ./results]
-i --input FILE specify input files
-e --entities entities to extract
Mike Widner <[email protected]>
'''
import os
import sys
from collections import defaultdict
from bs4 import BeautifulSoup
from docopt import docopt
options = docopt(__doc__)
entities = [e.lower() for e in options['--entities']]
entity_table = defaultdict(list)
OUTPUT_DIR = options['--output']
for filename in options['--input']:
fh = open(filename, 'r')
text = fh.read()
fh.close()
soup = BeautifulSoup(text)
for item in soup.find_all(entities):
entity_table[item.name].append(item.string)
for entity_type in entity_table.keys():
if not os.path.isdir(OUTPUT_DIR):
os.makedirs(OUTPUT_DIR)
key = os.path.basename(filename)
key = os.path.splitext(key)[0]
fh = open(OUTPUT_DIR + '/' + key + "_" + entity_type + ".txt", 'w')
fh.write('\n'.join(entity_table[entity_type]))
fh.close()