|
a |
|
b/example/ctakes/ctakes.py |
|
|
1 |
#!/usr/bin/env python |
|
|
2 |
|
|
|
3 |
"""Demonstrates access to UMLS via CTAKES. |
|
|
4 |
|
|
|
5 |
""" |
|
|
6 |
__author__ = 'Paul Landes' |
|
|
7 |
|
|
|
8 |
from dataclasses import dataclass, field |
|
|
9 |
from pathlib import Path |
|
|
10 |
import pandas as pd |
|
|
11 |
from zensols.cli import CliHarness, ProgramNameConfigurator |
|
|
12 |
from zensols.mednlp.ctakes import CTakesParserStash |
|
|
13 |
|
|
|
14 |
|
|
|
15 |
# the definition of the application class executed from the CLI glue code |
|
|
16 |
@dataclass |
|
|
17 |
class Application(object): |
|
|
18 |
"""Demonstrates how to use the cTAKES application wrapper. |
|
|
19 |
|
|
|
20 |
""" |
|
|
21 |
# tell the application not mistake the `uts_client` as an option when |
|
|
22 |
# generating the online help with the -h option |
|
|
23 |
CLI_META = {'option_excludes': {'ctakes_stash'}} |
|
|
24 |
|
|
|
25 |
ctakes_stash: CTakesParserStash = field() |
|
|
26 |
"""Runs the cTAKES CUI entity linker on a directory of medical notes.""" |
|
|
27 |
|
|
|
28 |
def entities(self, sent: str = None, output: Path = None): |
|
|
29 |
"""Parse a sentence and print cTAKES entities. |
|
|
30 |
|
|
|
31 |
:param sent: the sentence to parse and generate features |
|
|
32 |
|
|
|
33 |
""" |
|
|
34 |
if sent is None: |
|
|
35 |
sent = 'He was diagnosed with kidney failure in the United States.' |
|
|
36 |
self.ctakes_stash.set_documents([sent]) |
|
|
37 |
df: pd.DataFrame = self.ctakes_stash['0'] |
|
|
38 |
print(df) |
|
|
39 |
if output is not None: |
|
|
40 |
df.to_csv(output) |
|
|
41 |
print(f'wrote: {output}') |
|
|
42 |
|
|
|
43 |
|
|
|
44 |
if (__name__ == '__main__'): |
|
|
45 |
CliHarness( |
|
|
46 |
app_config_resource='ctakes.conf', |
|
|
47 |
app_config_context=ProgramNameConfigurator( |
|
|
48 |
None, default='ctakes').create_section(), |
|
|
49 |
proto_args='', |
|
|
50 |
proto_factory_kwargs={'reload_pattern': '^ctakes'}, |
|
|
51 |
).run() |