├── amr_verbnet_semantics ├── __init__.py ├── core │ ├── __init__.py │ ├── models.py │ ├── extract_timeline.py │ └── spacy_nlp_parse.py ├── grpc_defs │ ├── __init__.py │ ├── ACEDoc_pb2_grpc.py │ ├── enhanced_amr_pb2_grpc.py │ ├── ace_pb2_grpc.py │ ├── enhanced_amr_pb2.py │ └── ace_pb2.py ├── service │ ├── __init__.py │ ├── abstract_kb.py │ ├── ulkb.py │ ├── corpus.py │ ├── verbnet.py │ ├── propbank.py │ ├── amr.py │ ├── sparql.py │ └── semlink.py ├── corpus_readers │ ├── __init__.py │ ├── verbnet_reader.py │ └── propbank_reader.py ├── grpc_clients │ ├── __init__.py │ └── clients.py ├── test │ ├── test_issue_13.py │ ├── test_local_amr_client.py │ ├── test_verbnet.py │ ├── test_ground_text_to_verbnet.py │ ├── test_query_semantics.py │ ├── test_reification.py │ ├── test_amr_parsing.py │ ├── test_visualize_dep_parse.py │ ├── test_visualize_sem_graph.py │ ├── test_propbank.py │ ├── test_issue_12.py │ ├── test_cache_amr_client.py │ ├── test_service.py │ ├── test_issue_8.py │ ├── test_issue_10.py │ ├── test_issue_11.py │ ├── test_issue_7.py │ ├── test_issue_9.py │ ├── test_issue_6.py │ ├── test_rdf_mappings.py │ ├── test_ground_amr.py │ └── test_ground_text_to_verbnet.ipynb ├── utils │ ├── format_util.py │ ├── eval_util.py │ ├── text_util.py │ ├── reification_util.py │ └── amr_util.py └── web_app │ ├── __init__.py │ ├── status.py │ ├── routes.py │ └── error_handlers.py ├── KG ├── images │ ├── README │ ├── ULKB_SUMMARY2.png │ ├── ulkb_access_img_pb.jpg │ ├── ulkb_access_img_vn.jpg │ └── ulkb_access_img_LClass.jpg ├── UL_KB_V3_PUB.ttl.zip ├── UL_KB_V4_PUB.ttl.zip ├── UL_KB_V5_PUB.ttl.zip ├── ingestion │ ├── ULKB_UnifiedMapping_V1.xlsx │ └── README.md ├── service │ ├── README.md │ ├── config_sparql.yaml │ └── config.py ├── config_sparql.yaml ├── README ├── config.py └── ulkb_access.py ├── setup.cfg ├── scripts ├── remove_snapshot.sh ├── create_conda_env.sh ├── remove_conda_env.sh ├── refactor_code.sh ├── download_semlink.sh ├── download_third_party.sh ├── download_propbank.sh ├── download_verbnet.sh └── install.sh ├── assets ├── blazegraph_install_1.jpg ├── blazegraph_install_2.jpg └── blazegraph_install_3.jpg ├── set_environment.sh ├── config.py ├── requirements.txt ├── README.md ├── app_config.py ├── config_template.yaml ├── setup.py ├── DESIGN.md ├── .gitignore ├── INSTALLATION.md └── LICENSE /amr_verbnet_semantics/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /amr_verbnet_semantics/core/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /amr_verbnet_semantics/grpc_defs/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /amr_verbnet_semantics/service/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /amr_verbnet_semantics/corpus_readers/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /amr_verbnet_semantics/grpc_clients/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /KG/images/README: -------------------------------------------------------------------------------- 1 | Directory for images in the Jupyter notebook 2 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [aliases] 2 | test=pytest 3 | 4 | [flake8] 5 | max-line-length=120 -------------------------------------------------------------------------------- /scripts/remove_snapshot.sh: -------------------------------------------------------------------------------- 1 | find . -type f -name "*snapshot.pickle*" | xargs rm 2 | -------------------------------------------------------------------------------- /KG/UL_KB_V3_PUB.ttl.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/AMR-CSLogic/HEAD/KG/UL_KB_V3_PUB.ttl.zip -------------------------------------------------------------------------------- /KG/UL_KB_V4_PUB.ttl.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/AMR-CSLogic/HEAD/KG/UL_KB_V4_PUB.ttl.zip -------------------------------------------------------------------------------- /KG/UL_KB_V5_PUB.ttl.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/AMR-CSLogic/HEAD/KG/UL_KB_V5_PUB.ttl.zip -------------------------------------------------------------------------------- /scripts/create_conda_env.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | conda create -n amr-verbnet -y python=3.7 -------------------------------------------------------------------------------- /KG/images/ULKB_SUMMARY2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/AMR-CSLogic/HEAD/KG/images/ULKB_SUMMARY2.png -------------------------------------------------------------------------------- /scripts/remove_conda_env.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | conda remove --name amr-verbnet --all -y 5 | -------------------------------------------------------------------------------- /assets/blazegraph_install_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/AMR-CSLogic/HEAD/assets/blazegraph_install_1.jpg -------------------------------------------------------------------------------- /assets/blazegraph_install_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/AMR-CSLogic/HEAD/assets/blazegraph_install_2.jpg -------------------------------------------------------------------------------- /assets/blazegraph_install_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/AMR-CSLogic/HEAD/assets/blazegraph_install_3.jpg -------------------------------------------------------------------------------- /KG/images/ulkb_access_img_pb.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/AMR-CSLogic/HEAD/KG/images/ulkb_access_img_pb.jpg -------------------------------------------------------------------------------- /KG/images/ulkb_access_img_vn.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/AMR-CSLogic/HEAD/KG/images/ulkb_access_img_vn.jpg -------------------------------------------------------------------------------- /KG/images/ulkb_access_img_LClass.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/AMR-CSLogic/HEAD/KG/images/ulkb_access_img_LClass.jpg -------------------------------------------------------------------------------- /set_environment.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | [ ! -d venv ] && virtualenv venv --python=python3.6 4 | . venv/bin/activate 5 | -------------------------------------------------------------------------------- /amr_verbnet_semantics/grpc_defs/ACEDoc_pb2_grpc.py: -------------------------------------------------------------------------------- 1 | # Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT! 2 | -------------------------------------------------------------------------------- /KG/ingestion/ULKB_UnifiedMapping_V1.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/AMR-CSLogic/HEAD/KG/ingestion/ULKB_UnifiedMapping_V1.xlsx -------------------------------------------------------------------------------- /amr_verbnet_semantics/test/test_issue_13.py: -------------------------------------------------------------------------------- 1 | 2 | from amr_verbnet_semantics.core.amr_verbnet_enhance import \ 3 | ground_text_to_verbnet 4 | -------------------------------------------------------------------------------- /KG/service/README.md: -------------------------------------------------------------------------------- 1 | The code here is designed to access the ULKB version. Make sure you match the ULKB_access_VX with the right version of the KG. 2 | -------------------------------------------------------------------------------- /config.py: -------------------------------------------------------------------------------- 1 | """ 2 | Config file for FLASK web service. 3 | """ 4 | import logging 5 | 6 | # Secret for session management 7 | LOGGING_LEVEL = logging.INFO 8 | -------------------------------------------------------------------------------- /scripts/refactor_code.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | find . -type f -name "*.py" | xargs autoflake --in-place --remove-all-unused-imports 3 | find . -type f -name "*.py" | xargs isort 4 | -------------------------------------------------------------------------------- /amr_verbnet_semantics/test/test_local_amr_client.py: -------------------------------------------------------------------------------- 1 | from amr_verbnet_semantics.service.amr import LocalAMRClient 2 | 3 | if __name__ == "__main__": 4 | 5 | amr_client = LocalAMRClient() 6 | 7 | list_text = [ 8 | "I loved him writing novels.", 9 | "I admired him for his honesty." 10 | ] 11 | for text in list_text: 12 | amr = amr_client.get_amr(text) 13 | print(amr) 14 | 15 | -------------------------------------------------------------------------------- /scripts/download_semlink.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Download Semlink from its GitHub repository 4 | # Example: sh scripts/download_semlink.sh [save_dir] 5 | # Usage: sh scripts/download_semlink.sh ./data 6 | 7 | set -e 8 | save_dir=$1 9 | 10 | mkdir -p ${save_dir} 11 | pushd . 12 | cd ${save_dir} 13 | git clone https://github.com/cu-clear/semlink.git 14 | popd 15 | 16 | echo "Saved to ${save_dir}/semlink" 17 | echo "DONE." -------------------------------------------------------------------------------- /amr_verbnet_semantics/test/test_verbnet.py: -------------------------------------------------------------------------------- 1 | from nltk.corpus.reader import VerbnetCorpusReader 2 | from nltk.corpus.util import LazyCorpusLoader 3 | 4 | verbnet = LazyCorpusLoader("verbnet3.4", VerbnetCorpusReader, r"(?!\.).*\.xml") 5 | print(verbnet.frames("escape-51.1-1")) 6 | try: 7 | print(verbnet.frames("escape-51.1-2")) 8 | except Exception as e: 9 | print(e) 10 | 11 | print(verbnet.frames("leave-51.2")) 12 | print(verbnet.subclasses("escape-51.1-1")) 13 | 14 | -------------------------------------------------------------------------------- /amr_verbnet_semantics/test/test_ground_text_to_verbnet.py: -------------------------------------------------------------------------------- 1 | import json 2 | from amr_verbnet_semantics.core.amr_verbnet_enhance \ 3 | import ground_text_to_verbnet 4 | 5 | if __name__ == '__main__': 6 | text = "You're in a corridor. You can see a shoe cabinet." 7 | ret = ground_text_to_verbnet(text, use_coreference=False, verbose=True) 8 | # print(json.dumps(ret, indent=2)) 9 | print(ret['sentence_parses'][0]['amr']) 10 | print(ret['sentence_parses'][1]['amr']) 11 | 12 | -------------------------------------------------------------------------------- /KG/config_sparql.yaml: -------------------------------------------------------------------------------- 1 | # THIS IS THE CONFIGURATION NEEDED FOR THE STANDALONE UL_KG 2 | 3 | 4 | 5 | # To run within the AMR-to-CS logic, use the project-wide config.yaml. The config.py 6 | # file in this directory will attempt to read it first. 7 | # Configure RDF store 8 | # Use this if you are going to use the server 9 | # SPARQL_ENDPOINT = "http://goedel.sl.cloud9.ibm.com:9999/blazegraph/namespace/UL_KB_V4" 10 | #Use this if you install the server locally 11 | SPARQL_ENDPOINT: "http://localhost:9999/blazegraph/namespace/UL_KB_V4_PUB" 12 | 13 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | requests==2.25.1 2 | nltk==3.6.2 3 | jsonlines==2.0.0 4 | grpcio==1.37.1 5 | grpcio-tools==1.37.1 6 | penman==1.2.0 7 | fuzzywuzzy==0.17.0 8 | networkx==2.4 9 | #spacy==2.1.0 10 | spacy==2.0.12 11 | neuralcoref==3.1 12 | gensim==3.8.1 13 | protobuf==3.17.3 14 | 15 | python-Levenshtein 16 | flask 17 | matplotlib 18 | graphviz 19 | ipykernel 20 | SPARQLWrapper 21 | ipdb 22 | pyyaml 23 | notebook 24 | autoflake 25 | isort 26 | prettytable 27 | 28 | # third party dependencies 29 | torch==1.3.0 30 | # fairseq==0.8.0 31 | packaging 32 | -------------------------------------------------------------------------------- /KG/service/config_sparql.yaml: -------------------------------------------------------------------------------- 1 | # THIS IS THE CONFIGURATION NEEDED FOR THE STANDALONE UL_KG 2 | 3 | 4 | 5 | # To run within the AMR-to-CS logic, use the project-wide config.yaml. The config.py 6 | # file in this directory will attempt to read it first. 7 | # Configure RDF store 8 | #Use this if you are going to use the server 9 | #SPARQL_ENDPOINT = "http://goedel.sl.cloud9.ibm.com:9999/blazegraph/namespace/UL_KB_V5" 10 | #Use this if you install the server locally 11 | SPARQL_ENDPOINT: "http://localhost:9999/blazegraph/namespace/UL_KB_V5" 12 | 13 | -------------------------------------------------------------------------------- /amr_verbnet_semantics/service/abstract_kb.py: -------------------------------------------------------------------------------- 1 | """ 2 | Unified query interfaces for the KB. 3 | """ 4 | 5 | 6 | class AbstractKb: 7 | def __init__(self): 8 | pass 9 | 10 | def query_semantics( 11 | self, verbnet_id, verbnet_version=None, verbose=False): 12 | raise NotImplementedError() 13 | 14 | def query_propbank_verbnet_class_mapping( 15 | self, propbank_id, verbnet_version=None, verbose=False): 16 | raise NotImplementedError() 17 | 18 | def query_verbnet_semantic_roles(self, propbank_id, verbose=False): 19 | raise NotImplementedError() 20 | 21 | 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # amr-verbnet-semantics 2 | 3 | ## Install 4 | Please read [INSTALLATION.md](./INSTALLATION.md) 5 | 6 | ## Note 7 | - How to clear a cache 8 | - This implementation uses a cache to speed up the AMR parser. A snapshot is stored on disk to store the cache permanently. The file name of the snapshot takes this format `snapshot.pickle_%Y-%m-%d_%H-%M-%S`. Here is an example of a file name; `snapshot.pickle_2021-10-06_14-52-41` 9 | - If you want to delete snapshot files, use `bash scripts/remove_snapshot.sh /`. Then please start up the server again to clear a cache. The method is written in [INSTALLATION.md](./INSTALLATION.md). 10 | -------------------------------------------------------------------------------- /amr_verbnet_semantics/test/test_query_semantics.py: -------------------------------------------------------------------------------- 1 | from amr_verbnet_semantics.service.verbnet import query_semantics 2 | 3 | verbnet_version = 'verbnet3.4' 4 | 5 | verbnet_id = 'escape-51.1' 6 | semantics = query_semantics(verbnet_id, verbnet_version) 7 | print(semantics) 8 | 9 | verbnet_id = 'spray-9.7-2' 10 | semantics = query_semantics(verbnet_id, verbnet_version) 11 | print(semantics) 12 | 13 | verbnet_id = 'build-26.1-1' 14 | semantics = query_semantics(verbnet_id, verbnet_version) 15 | print(semantics) 16 | 17 | verbnet_id = 'stop-55.4-1-1' 18 | semantics = query_semantics(verbnet_id, verbnet_version) 19 | print(semantics) 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /scripts/download_third_party.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # this is called from setup.py in the main project dir 4 | 5 | echo "** Installing third_party **" 6 | rm -rf third_party 7 | mkdir third_party 8 | 9 | rm -rf transition-amr-parser-0.4.2 10 | wget https://github.com/IBM/transition-amr-parser/archive/refs/tags/v0.4.2.zip 11 | unzip v0.4.2.zip 12 | pushd transition-amr-parser-0.4.2 13 | sed -I "" 's/torch<=1.2,<=1.3/torch==1.3.0/g' setup.py # -i doesn't work on osx 14 | pip install -e . 15 | popd 16 | rm v0.4.2.zip 17 | 18 | wget https://dl.fbaipublicfiles.com/fairseq/models/roberta.large.tar.gz 19 | tar -zxvf roberta.large.tar.gz 20 | mv roberta.large third_party 21 | rm roberta.large.tar.gz 22 | -------------------------------------------------------------------------------- /amr_verbnet_semantics/test/test_reification.py: -------------------------------------------------------------------------------- 1 | """ 2 | Test module for reification using penman package. 3 | """ 4 | 5 | from penman.codec import PENMANCodec 6 | from penman.models.amr import model 7 | from penman.transform import reify_edges 8 | 9 | codec = PENMANCodec(model=model) 10 | g = codec.decode(""" 11 | (d / dress 12 | :ARG1-of (r / red-02) 13 | :ARG1-of (c / clean-04) 14 | :location (n / nightstand)) 15 | """) 16 | g = reify_edges(g, model) 17 | print(codec.encode(g)) 18 | 19 | 20 | """ 21 | Output: 22 | 23 | (d / dress 24 | :ARG1-of (r / red-02) 25 | :ARG1-of (c / clean-04) 26 | :ARG1-of (_ / be-located-at-91 27 | :ARG2 (n / nightstand))) 28 | """ 29 | 30 | -------------------------------------------------------------------------------- /KG/README: -------------------------------------------------------------------------------- 1 | The KG is in RDF/OWL. The files are as follows: 2 | 3 | UL_KB_V3_PUB.ttl --> the graph file is self-contained. It can be installed in any triplestore and queried with SPARQL 4 | The two previous versions, V2 and V1 are added here for completeness 5 | 6 | KBAccess_RDF --> is the Jupyter notebook with examples of the JSON API and the underlying SPARQL queries. The API calls use the ulkb_access code and 7 | the images in the directory. 8 | 9 | ulkb_access.py --> the implementation of the JSON API (v3) 10 | 11 | ulkb_access_v4.py --> Access for version V4. Make sure the yaml configuration file points to the right place 12 | 13 | 14 | Any issues or questions about the graph, contact rosariou@us.ibm.com 15 | 16 | -------------------------------------------------------------------------------- /scripts/download_propbank.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Download Propbank 4 | # Example: sh scripts/download_propbank.sh [save_dir] 5 | # Usage: sh scripts/download_propbank.sh ~/nltk_data/corpora/ 6 | 7 | set -e 8 | save_dir=$1 9 | 10 | # Propbank frames 3.1 11 | mkdir -p ${save_dir} 12 | wget -O ${save_dir}/propbank-frames-3.1.zip https://github.com/propbank/propbank-frames/archive/refs/tags/v3.1.zip 13 | echo "Unzip ..." 14 | unzip ${save_dir}/propbank-frames-3.1.zip -d ${save_dir} 15 | echo "Saved to ${save_dir}/propbank-frames-3.1" 16 | 17 | # Replace with the latest propbank frames folder 18 | cp -R ${save_dir}/propbank ${save_dir}/propbank-latest 19 | rm -rf ${save_dir}/propbank-latest/frames/ 20 | cp -R ${save_dir}/propbank-frames-3.1/frames/ ${save_dir}/propbank-latest 21 | 22 | echo "DONE." -------------------------------------------------------------------------------- /amr_verbnet_semantics/core/models.py: -------------------------------------------------------------------------------- 1 | """ 2 | Models defined for the services 3 | """ 4 | 5 | 6 | class PredicateCalculus(object): 7 | def __init__(self, predicate, arguments, is_negative=False): 8 | self.predicate = predicate 9 | self.arguments = arguments 10 | self.is_negative = is_negative 11 | 12 | def __repr__(self): 13 | stmt = self.predicate + "(" + ", ".join(self.arguments) + ")" 14 | if self.is_negative: 15 | stmt = "NOT({})".format(stmt) 16 | return stmt 17 | 18 | def to_json(self): 19 | return { 20 | "predicate": self.predicate, 21 | "arguments": self.arguments, 22 | "is_negative": self.is_negative 23 | } 24 | 25 | 26 | if __name__ == "__main__": 27 | pass 28 | 29 | -------------------------------------------------------------------------------- /app_config.py: -------------------------------------------------------------------------------- 1 | """ 2 | Load configuration from .yaml file. 3 | """ 4 | import os 5 | 6 | import yaml 7 | from amr_verbnet_semantics.utils.format_util import DictObject, to_json 8 | 9 | config_file_path = os.path.join(os.path.dirname(__file__), "config.yaml") 10 | if not os.path.exists(config_file_path): 11 | raise \ 12 | Exception("Please create a config.yaml file " 13 | "following the config_template.yaml " 14 | "in the project root dir ...") 15 | 16 | with open(config_file_path, "r") as f: 17 | config = yaml.safe_load(f) 18 | 19 | config = DictObject(config) 20 | 21 | env_port_num = os.environ.get('AMR_LOCAL_SERVICE_PORT', '') 22 | if env_port_num != '': 23 | config.LOCAL_SERVICE_PORT = env_port_num 24 | 25 | if __name__ == "__main__": 26 | print(to_json(config)) 27 | print(config.SPARQL_ENDPOINT) 28 | -------------------------------------------------------------------------------- /amr_verbnet_semantics/test/test_amr_parsing.py: -------------------------------------------------------------------------------- 1 | 2 | from amr_verbnet_semantics.grpc_clients import AMRClientTransformer 3 | 4 | amr_host = "mnlp-demo.sl.cloud9.ibm.com" 5 | amr_port = 59990 6 | amr_client = AMRClientTransformer(f"{amr_host}:{amr_port}") 7 | 8 | 9 | if __name__ == "__main__": 10 | """ 11 | list_text = [ 12 | "The contractor builds houses for $100,000.", 13 | "$100,000 builds a house.", 14 | "$100,000 will build you a house.", 15 | "$100,000 builds a house out of sticks.", 16 | "$100,000 builds you a house out of sticks.", 17 | "The dresser is made out of maple carefully finished with Danish oil." 18 | ] 19 | """ 20 | list_text = [ 21 | "I loved him writing novels.", 22 | "I admired him for his honesty." 23 | ] 24 | for text in list_text: 25 | amr = amr_client.get_amr(text) 26 | print(amr) 27 | 28 | -------------------------------------------------------------------------------- /amr_verbnet_semantics/test/test_visualize_dep_parse.py: -------------------------------------------------------------------------------- 1 | 2 | """ 3 | import spacy 4 | from spacy import displacy 5 | 6 | nlp = spacy.load("en_core_web_sm") 7 | doc = nlp("This is a sentence.") 8 | displacy.serve(doc, style="dep") 9 | """ 10 | 11 | import spacy 12 | from spacy import displacy 13 | from pathlib import Path 14 | 15 | nlp = spacy.load("en_core_web_sm") 16 | sentences = ["For the fifth grade play , the chairs have been put into 27 rows with 16 chairs in each row .", 17 | "How many chairs have been put out for the play ?"] 18 | for sent in sentences: 19 | doc = nlp(sent) 20 | svg = displacy.render(doc, style="dep", jupyter=False) 21 | Path("./test-output/").mkdir(parents=True, exist_ok=True) 22 | file_name = '-'.join([w.text for w in doc if not w.is_punct]) + ".svg" 23 | output_path = Path("./test-output/" + file_name) 24 | output_path.open("w", encoding="utf-8").write(svg) 25 | 26 | -------------------------------------------------------------------------------- /amr_verbnet_semantics/test/test_visualize_sem_graph.py: -------------------------------------------------------------------------------- 1 | 2 | import requests 3 | import json 4 | 5 | from amr_verbnet_semantics.core.amr_verbnet_enhance import ( 6 | build_graph_from_amr, visualize_semantic_graph) 7 | 8 | host = "0.0.0.0" 9 | port = 5000 10 | 11 | # text = "For the fifth grade play , the chairs have been put into 27 rows with 16 chairs in each row . How many chairs have been put out for the play ?" 12 | text = "You are carrying : a bronze-hilted dagger, a clay ocarina, armor, and silks ( worn ) ." 13 | res = requests.get("http://{}:{}/amr_parsing".format(host, port), params={'text': text}) 14 | res = json.loads(res.text) 15 | print(res) 16 | for i, amr in enumerate(res["result"]): 17 | graph, amr_obj = build_graph_from_amr(amr=amr) 18 | visualize_semantic_graph(graph, graph_name="amr_semantic_graph_{}".format(i), 19 | out_dir="./test-output/") 20 | print("amr_semantic_graph DONE ...") 21 | 22 | -------------------------------------------------------------------------------- /KG/ingestion/README.md: -------------------------------------------------------------------------------- 1 | This directory contains both code and input data for the generation of ULKB. 2 | 3 | Code in ulkb_generate_unified_mapping.py generates the excel ULKB_UnifiedMapping_V1.xlsx directly from the input files. It carries out a phantom generation of the graph that, instead of writing to .ttl, it produces an excel that can be easily modified and changed. This allows us to correct the generation quickly. 4 | 5 | The file ULKB_UnifiedMapping_V1.xlsx contains the analysis of mappings of verbs and constraints from the version V4. Of particular interest is the red column in UM_RAW_V1, which indicates vocabulary in the mapping that doesn't exist in the target class. This means the mapping cannot lead to a successful grounding. 6 | 7 | Once this mapping is curated, the excel is used in the ingestion, instead of Semlink versions and Propbank. The code in ulkb_ingest_PB_VN.py does this. It takes the excel above, plus all the versions of Verbnet and Propbank to generate UL_KB_V5. 8 | -------------------------------------------------------------------------------- /scripts/download_verbnet.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Download VerbNet 4 | # Example: sh scripts/download_verbnet.sh [save_dir] 5 | # Usage: sh scripts/download_verbnet.sh ~/nltk_data/corpora/ 6 | 7 | set -e 8 | save_dir=$1 9 | 10 | # VerbNet 3.2 11 | wget -P ${save_dir} http://verbs.colorado.edu/verb-index/vn/verbnet-3.2.tar.gz 12 | echo "Unzip ..." 13 | tar xvzf ${save_dir}/verbnet-3.2.tar.gz -C ${save_dir} && mv ${save_dir}/new_vn ${save_dir}/verbnet3.2 14 | echo "Saved to ${save_dir}/verbnet3.2" 15 | 16 | # VerbNet 3.3 17 | wget -P ${save_dir} http://verbs.colorado.edu/verb-index/vn/verbnet-3.3.tar.gz 18 | echo "Unzip ..." 19 | tar xvzf ${save_dir}/verbnet-3.3.tar.gz -C ${save_dir} 20 | echo "Saved to ${save_dir}/verbnet3.3" 21 | 22 | # VerbNet 3.4 23 | git clone https://github.com/cu-clear/verbnet.git ./verbnet_clone 24 | cp -R ./verbnet_clone/verbnet3.4 ${save_dir} 25 | rm -rf ./verbnet_clone 26 | echo "Saved to ${save_dir}/verbnet3.4" 27 | 28 | echo "Download & unzip DONE." 29 | 30 | -------------------------------------------------------------------------------- /amr_verbnet_semantics/test/test_propbank.py: -------------------------------------------------------------------------------- 1 | 2 | from xml.etree import ElementTree 3 | 4 | from nltk.corpus import propbank 5 | 6 | """ 7 | pb_instances = propbank.instances() 8 | inst = pb_instances[103] 9 | print(inst.fileid, inst.sentnum, inst.wordnum) 10 | print(inst.tagger) 11 | print(inst.inflection) 12 | print(inst.roleset) 13 | print(inst.predicate) 14 | print(inst.arguments) 15 | print("rolesets:", propbank.roleset("enter.01")) 16 | """ 17 | 18 | enter_01 = propbank.roleset("enter.01") 19 | for role in enter_01.findall("roles/role"): 20 | vn_role = role.find('vnrole') 21 | print("vncls:", vn_role.attrib["vncls"]) 22 | print("vntheta:", vn_role.attrib["vntheta"]) 23 | print(ElementTree.tostring(vn_role).decode('utf8').strip()) 24 | print(role.attrib['n'], role.attrib['descr']) 25 | 26 | print("====================") 27 | print(ElementTree.tostring(enter_01).decode('utf8').strip()) 28 | # print(ElementTree.tostring(enter_01.find('roles')).decode('utf8').strip()) 29 | 30 | -------------------------------------------------------------------------------- /scripts/install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Install dependencies and setups 4 | # Usage: sh ./install.sh 5 | 6 | set -e 7 | 8 | echo "** ensuring latest version of pip is installed **" 9 | pip install --upgrade pip 10 | 11 | if [[ ${CONDA_DEFAULT_ENV:-''} != amr-verbnet ]]; then 12 | echo Create and activate the amr-verbnet env 13 | echo "Sorry, we can't do that for you in this script, at least, not on a Mac" 14 | exit 1 15 | fi 16 | 17 | pip install numpy 18 | 19 | echo "** installing packages **" 20 | pip install -e . 21 | 22 | echo "** Downloading spaCy corpus **" 23 | python -m spacy download en 24 | 25 | # For constituency parsing 26 | # pip install benepar --no-cache-dir 27 | # python -c "import benepar; benepar.download('benepar_en3')" 28 | 29 | # Install PyTorch 1.3 30 | # pip install torch==1.3 31 | 32 | echo "** Downloading Blazegraph for storing the KG **" 33 | mkdir -p blazegraph 34 | wget -O ./blazegraph/blazegraph.jar https://github.com/blazegraph/database/releases/download/BLAZEGRAPH_2_1_6_RC/blazegraph.jar 35 | -------------------------------------------------------------------------------- /KG/config.py: -------------------------------------------------------------------------------- 1 | """ 2 | Config file for isolating endpoint configuration difference. Checks for 3 | config.yaml in the project OR a config_sparql.yaml in the current directory 4 | """ 5 | import os 6 | 7 | import yaml 8 | 9 | SPARQL_ENDPOINT = "" 10 | config = "" 11 | 12 | thisDir = os.path.join(os.getcwd()) 13 | 14 | try: 15 | from app_config import config 16 | SPARQL_ENDPOINT = config.SPARQL_ENDPOINT 17 | except ImportError: 18 | try : 19 | with open(thisDir + "/config_sparql.yaml", "r") as f: 20 | config = yaml.safe_load(f) 21 | if type(config) is dict : 22 | SPARQL_ENDPOINT = config['SPARQL_ENDPOINT'] 23 | else : 24 | SPARQL_ENDPOINT = config 25 | SPARQL_ENDPOINT = SPARQL_ENDPOINT.split("\"")[1] 26 | except : 27 | raise Exception("Cannot find config_sparql.yaml in current directory (standalone) or config.yaml in root directory (AMR-CSlogic)") 28 | 29 | if __name__ == "__main__": 30 | print(str(config)) 31 | print(SPARQL_ENDPOINT) 32 | 33 | -------------------------------------------------------------------------------- /KG/service/config.py: -------------------------------------------------------------------------------- 1 | """ 2 | Config file for isolating endpoint configuration difference. Checks for 3 | config.yaml in the project OR a config_sparql.yaml in the current directory 4 | """ 5 | import yaml 6 | import json 7 | import os 8 | 9 | SPARQL_ENDPOINT = "" 10 | config = "" 11 | 12 | thisDir = os.path.join(os.getcwd()) 13 | 14 | try: 15 | from app_config import config 16 | SPARQL_ENDPOINT = config.SPARQL_ENDPOINT 17 | except ImportError: 18 | try : 19 | with open(thisDir + "/config_sparql.yaml", "r") as f: 20 | config = yaml.safe_load(f) 21 | if type(config) is dict : 22 | SPARQL_ENDPOINT = config['SPARQL_ENDPOINT'] 23 | else : 24 | SPARQL_ENDPOINT = config 25 | SPARQL_ENDPOINT = SPARQL_ENDPOINT.split("\"")[1] 26 | except : 27 | raise Exception("Cannot find config_sparql.yaml in current directory (standalone) or config.yaml in root directory (AMR-CSlogic)") 28 | 29 | if __name__ == "__main__": 30 | print(str(config)) 31 | print(SPARQL_ENDPOINT) 32 | 33 | -------------------------------------------------------------------------------- /amr_verbnet_semantics/test/test_issue_12.py: -------------------------------------------------------------------------------- 1 | from amr_verbnet_semantics.core.amr_verbnet_enhance import ground_amr 2 | import requests 3 | 4 | text = 'wet hoodie' 5 | endpoint = f'http://9.116.32.235:5000/verbnet_semantics' # temporary development server 6 | 7 | params = {'text': text, 'use_coreference': 0} 8 | r = requests.get(endpoint, params=params) 9 | r = r.json() 10 | amr = r['amr_parse'][0]['amr'] 11 | amr_cal_str = r['amr_parse'][0]['amr_cal_str'] 12 | 13 | 14 | print(f'***** amr *****') 15 | print(amr) 16 | print(f'***** amr_cal_str *****') 17 | print(amr_cal_str) 18 | 19 | g_res = ground_amr(amr) 20 | print(f'\n***** grounded_stmt_str *****') 21 | print(g_res['grounded_stmt_str']) 22 | 23 | ''' 24 | As of 15th Oct, the output 25 | 26 | ***** amr ***** 27 | # ::tok wet hoodie 28 | # ::node 1 wet-01 0-1 29 | # ::node 2 hoodie 1-2 30 | # ::root 2 hoodie 31 | # ::edge hoodie ARG1-of wet-01 2 1 32 | # ::short {1: 'w', 2: 'h'} 33 | (h / hoodie 34 | :ARG1-of (w / wet-01)) 35 | 36 | 37 | ***** amr_cal_str ***** 38 | [hoodie(h), wet-01(w), wet-01.arg1(w, h)] 39 | 40 | ***** grounded_stmt_str ***** 41 | {'wet.01': {'other_cos-45.4': [[NOT(HAS_STATE(e1, h, V_Final_State)), HAS_STATE(e2, h, V_Final_State)]]}} 42 | ''' -------------------------------------------------------------------------------- /amr_verbnet_semantics/test/test_cache_amr_client.py: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | from amr_verbnet_semantics.service.amr import CacheClient, LocalAMRClient 4 | 5 | if __name__ == "__main__": 6 | 7 | amr_client = LocalAMRClient() 8 | amr_client = CacheClient(amr_client, use_snapshot=False) 9 | 10 | list_text = ['You drop the peanut oil on the ground.', 11 | 'You pick up the peanut oil from the ground.', 12 | 'You take the peanut oil from the folding chair.', 13 | 'You drop the red hot pepper on the ground.', 14 | 'But the thing is empty.', 15 | 'The shelf is wooden.', 16 | 'Now why would someone leave that there?', 17 | 'When you stand up, you notice a shelf.', 18 | 'You bend down to tie your shoe.'] 19 | 20 | # before caching 21 | start = time.time() 22 | for text in list_text: 23 | amr = amr_client.get_amr(text) 24 | print(f'before caching: {time.time() - start:.3f} seconds') 25 | 26 | 27 | # after caching 28 | start = time.time() 29 | for text in list_text: 30 | amr = amr_client.get_amr(text) 31 | print(f'after caching: {time.time() - start:.3f} seconds') 32 | -------------------------------------------------------------------------------- /amr_verbnet_semantics/utils/format_util.py: -------------------------------------------------------------------------------- 1 | """ 2 | Utility functions for formatting 3 | """ 4 | import inspect 5 | 6 | 7 | def to_json(obj): 8 | if isinstance(obj, dict): 9 | results = {} 10 | for key in obj: 11 | results[key] = to_json(obj[key]) 12 | return results 13 | elif isinstance(obj, list): 14 | results = [] 15 | for elem in obj: 16 | results.append(to_json(elem)) 17 | return results 18 | elif isinstance(obj, DictObject): 19 | results = {} 20 | for key in dir(obj): 21 | if key.startswith('_'): 22 | continue 23 | results[key] = to_json(getattr(obj, key)) 24 | return results 25 | elif hasattr(obj, "to_json") and inspect.ismethod(getattr(obj, "to_json")): 26 | return obj.to_json() 27 | else: 28 | return obj 29 | 30 | 31 | class DictObject(object): 32 | def __init__(self, in_dict): 33 | for key, val in in_dict.items(): 34 | if isinstance(val, (list, tuple)): 35 | setattr(self, key, [DictObject(x) if isinstance(x, dict) else x for x in val]) 36 | else: 37 | setattr(self, key, DictObject(val) if isinstance(val, dict) else val) 38 | 39 | -------------------------------------------------------------------------------- /amr_verbnet_semantics/service/ulkb.py: -------------------------------------------------------------------------------- 1 | """ 2 | RDF query interfaces for the KB. 3 | """ 4 | 5 | from amr_verbnet_semantics.service.abstract_kb import AbstractKb 6 | from amr_verbnet_semantics.service.semlink import query_pb_vn_mapping_from_rdf 7 | from amr_verbnet_semantics.service.sparql import \ 8 | query_verbnet_semantic_roles_from_rdf 9 | from amr_verbnet_semantics.service.verbnet import query_semantics_from_rdf 10 | 11 | 12 | class UnifiedKb(AbstractKb): 13 | def __init__(self): 14 | super().__init__() 15 | 16 | def query_semantics(self, verbnet_id, verbnet_version=None, verbose=False): 17 | return query_semantics_from_rdf(verbnet_id, verbnet_version) 18 | 19 | def query_propbank_verbnet_class_mapping(self, propbank_id, 20 | verbnet_version=None, verbose=False): 21 | return query_pb_vn_mapping_from_rdf(propbank_id) 22 | 23 | def query_verbnet_semantic_roles(self, propbank_id, verbose=False): 24 | return query_verbnet_semantic_roles_from_rdf(propbank_id) 25 | 26 | 27 | if __name__ == '__main__': 28 | ulkb = UnifiedKb() 29 | print(ulkb.query_semantics("escape-51.1", verbnet_version="verbnet3.4")) 30 | print() 31 | print(ulkb.query_semantics("spray-9.7-2", verbnet_version="verbnet3.4")) 32 | print() 33 | print(ulkb.query_propbank_verbnet_class_mapping("enter.01")) 34 | print() 35 | print(ulkb.query_verbnet_semantic_roles("enter.01")) 36 | 37 | -------------------------------------------------------------------------------- /amr_verbnet_semantics/service/corpus.py: -------------------------------------------------------------------------------- 1 | """ 2 | Corpus query interfaces for the KB. 3 | """ 4 | 5 | from amr_verbnet_semantics.service.abstract_kb import AbstractKb 6 | from amr_verbnet_semantics.service.propbank import \ 7 | query_verbnet_semantic_roles_from_corpus 8 | from amr_verbnet_semantics.service.semlink import \ 9 | query_pb_vn_mapping_from_semlink 10 | from amr_verbnet_semantics.service.verbnet import query_semantics_from_corpus 11 | 12 | 13 | class CorpusKb(AbstractKb): 14 | def __init__(self): 15 | super().__init__() 16 | 17 | def query_semantics(self, verbnet_id, verbnet_version=None, verbose=False): 18 | return query_semantics_from_corpus(verbnet_id, verbnet_version) 19 | 20 | def query_propbank_verbnet_class_mapping( 21 | self, propbank_id, verbnet_version=None, verbose=False): 22 | return query_pb_vn_mapping_from_semlink(propbank_id) 23 | 24 | def query_verbnet_semantic_roles(self, propbank_id, verbose=False): 25 | return query_verbnet_semantic_roles_from_corpus(propbank_id, verbose) 26 | 27 | 28 | if __name__ == '__main__': 29 | corpus_kb = CorpusKb() 30 | print(corpus_kb.query_semantics("escape-51.1", verbnet_version="verbnet3.4")) 31 | print() 32 | print(corpus_kb.query_semantics("spray-9.7-2", verbnet_version="verbnet3.4")) 33 | print() 34 | print(corpus_kb.query_propbank_verbnet_class_mapping("enter.01")) 35 | print() 36 | print(corpus_kb.query_verbnet_semantic_roles("enter.01")) 37 | 38 | -------------------------------------------------------------------------------- /amr_verbnet_semantics/test/test_service.py: -------------------------------------------------------------------------------- 1 | # Test for services 2 | 3 | import argparse 4 | import json 5 | from pprint import pprint 6 | 7 | import requests 8 | 9 | parser = argparse.ArgumentParser() 10 | 11 | parser.add_argument('--ip', type=str, default='0.0.0.0') 12 | parser.add_argument('--port', type=int, default=5000) 13 | parser.add_argument('--text', type=str, default='You enter a kitchen.') 14 | 15 | args = parser.parse_args() 16 | 17 | host = args.ip 18 | port = args.port 19 | 20 | text = args.text 21 | 22 | res = requests.get("http://{}:{}/verbnet_semantics".format(host, port), 23 | params={'text': text}) 24 | 25 | print("\nres.text:") 26 | print(res.text) 27 | 28 | res = json.loads(res.text) 29 | if "amr_parse" in res: 30 | for i in range(len(res["amr_parse"])): 31 | print("\namr:") 32 | print(res["amr_parse"][i]["amr"]) 33 | print("\npb_vn_mappings:") 34 | pprint(res["amr_parse"][i]["pb_vn_mappings"]) 35 | 36 | print("\namr_cal:") 37 | print(res["amr_parse"][i]["amr_cal"]) 38 | 39 | print("\nsem_cal:") 40 | print(res["amr_parse"][i]["sem_cal"]) 41 | 42 | print("\ngrounded_stmt:") 43 | print(res["amr_parse"][i]["grounded_stmt"]) 44 | 45 | print("\namr_cal:") 46 | print(res["amr_parse"][i]["amr_cal_str"]) 47 | 48 | print("\nsem_cal:") 49 | print(res["amr_parse"][i]["sem_cal_str"]) 50 | 51 | print("\ngrounded_stmt:") 52 | print(res["amr_parse"][i]["grounded_stmt_str"]) 53 | -------------------------------------------------------------------------------- /config_template.yaml: -------------------------------------------------------------------------------- 1 | # Use this config template to create your own config yaml file 2 | 3 | # Configure AMR parsing 4 | AMR_MODEL_CHECKPOINT_PATH: "DATA/AMR2.0/models/exp_cofill_o8.3_act-states_RoBERTa-large-top24/_act-pos-grh_vmask1_shiftpos1_ptr-lay6-h1_grh-lay123-h2-allprev_1in1out_cam-layall-h2-abuf/ep60-seed44/checkpoint_wiki.smatch_best1.pt" 5 | ROBERTA_CACHE_PATH: "roberta.large" 6 | THIRD_PARTY_PATH: 'third_party' 7 | use_cuda: false 8 | 9 | # Configure RDF store 10 | SPARQL_ENDPOINT: "http://localhost:9999/blazegraph/namespace/UL_KB_V5_PUB" 11 | 12 | # Configure VerbNet 13 | VERBNET_VERSION: "verbnet3.2" 14 | 15 | # whether to remove contradicting statements 16 | # when the time dimension is ignored. 17 | FILTER_INVALID_STATEMENTS: true 18 | 19 | # Configure KB source 20 | # Set to "rdf" if you want to query from the RDF triple store 21 | # KB_SOURCE: "corpus" 22 | KB_SOURCE: "rdf" 23 | 24 | # Configure Conda home 25 | CONDA_HOME: "~/anaconda3" 26 | 27 | # Configure local Stanford NLP parser (deprecated) 28 | # STANFORD_CORENLP_PATH: "./stanford-corenlp-full-2018-10-05" 29 | # STANFORD_CORENLP_PATH: null 30 | # STANFORD_CORENLP_HOST: "http://localhost" 31 | # STANFORD_CORENLP_PORT: 9000 32 | 33 | # Configure AMR parsing 34 | # AMR_PARSING_MODEL: "remote" 35 | AMR_PARSING_MODEL: "local" 36 | 37 | # Configure remote AMR parsing service 38 | REMOTE_AMR_HOST: "mnlp-demo.sl.cloud9.ibm.com" 39 | REMOTE_AMR_PORT: 59990 40 | 41 | # Configure local FLASK service 42 | LOCAL_SERVICE_HOST: "0.0.0.0" 43 | LOCAL_SERVICE_PORT: 5000 44 | USE_FLASK: true -------------------------------------------------------------------------------- /amr_verbnet_semantics/corpus_readers/verbnet_reader.py: -------------------------------------------------------------------------------- 1 | """ 2 | An extension class based on NLTK VerbnetCorpusReader to capture the negation of statement. 3 | Reference: https://www.nltk.org/_modules/nltk/corpus/reader/verbnet.html 4 | """ 5 | from nltk.corpus.reader import VerbnetCorpusReader 6 | 7 | 8 | class VerbnetCorpusReaderEx(VerbnetCorpusReader): 9 | def __init__(self, root, fileids, wrap_etree=False): 10 | VerbnetCorpusReader.__init__(self, root, fileids, wrap_etree) 11 | 12 | def _get_semantics_within_frame(self, vnframe): 13 | """Returns semantics within a single frame 14 | 15 | A utility function to retrieve semantics within a frame in VerbNet 16 | Members of the semantics dictionary: 17 | 1) Predicate value 18 | 2) Arguments 19 | 20 | :param vnframe: An ElementTree containing the xml contents of 21 | a VerbNet frame. 22 | :return: semantics: semantics dictionary 23 | """ 24 | semantics_within_single_frame = [] 25 | for pred in vnframe.findall("SEMANTICS/PRED"): 26 | arguments = [ 27 | {"type": arg.get("type"), "value": arg.get("value")} 28 | for arg in pred.findall("ARGS/ARG") 29 | ] 30 | semantics_within_single_frame.append( 31 | { 32 | "predicate_value": pred.get("value"), 33 | "arguments": arguments, 34 | "is_negative": pred.get("bool", False) == "!" 35 | } 36 | ) 37 | return semantics_within_single_frame 38 | -------------------------------------------------------------------------------- /amr_verbnet_semantics/test/test_issue_8.py: -------------------------------------------------------------------------------- 1 | from amr_verbnet_semantics.core.amr_verbnet_enhance import ground_amr 2 | import requests 3 | 4 | text = 'He entered the room.' 5 | endpoint = f'http://9.116.32.235:5000/verbnet_semantics' # temporary development server 6 | 7 | params = {'text': text, 'use_coreference': 0} 8 | r = requests.get(endpoint, params=params) 9 | r = r.json() 10 | amr = r['amr_parse'][0]['amr'] 11 | amr_cal_str = r['amr_parse'][0]['amr_cal_str'] 12 | 13 | 14 | print(f'***** amr *****') 15 | print(amr) 16 | print(f'***** amr_cal_str *****') 17 | print(amr_cal_str) 18 | 19 | g_res = ground_amr(amr) 20 | print(f'\n***** grounded_stmt_str *****') 21 | print(g_res['grounded_stmt_str']) 22 | 23 | print(f'\n***** pb_vn_mappings *****') 24 | print(g_res['pb_vn_mappings']) 25 | 26 | 27 | 28 | ''' 29 | As of 15th Oct, the output 30 | 31 | ***** amr ***** 32 | # ::tok He entered the room . 33 | # ::node 1 he 0-1 34 | # ::node 2 enter-01 1-2 35 | # ::node 3 room 3-4 36 | # ::root 2 enter-01 37 | # ::edge enter-01 ARG0 he 2 1 38 | # ::edge enter-01 ARG1 room 2 3 39 | # ::short {1: 'h', 2: 'e', 3: 'r'} 40 | (e / enter-01 41 | :ARG0 (h / he) 42 | :ARG1 (r / room)) 43 | 44 | 45 | ***** amr_cal_str ***** 46 | [enter-01(e), he(h), room(r), enter-01.arg0(e, h), enter-01.arg1(e, r)] 47 | 48 | ***** grounded_stmt_str ***** 49 | {'enter.01': {'escape-51.1-2': [[MOTION(during(E), h), LOCATION(start(E), r, ?Initial_Location), LOCATION(end(E), r, ?Trajectory)]]}} 50 | 51 | ***** pb_vn_mappings ***** 52 | {'enter.01': [{'mapping': 'escape-51.1-2', 'source': 'verbnet3.4'}]} 53 | 54 | ''' -------------------------------------------------------------------------------- /amr_verbnet_semantics/web_app/__init__.py: -------------------------------------------------------------------------------- 1 | """ 2 | Package: service 3 | Package for the application models and service routes 4 | This module creates and configures the Flask app and sets up the logging 5 | """ 6 | import importlib 7 | import logging 8 | import os 9 | 10 | from flask import Flask 11 | 12 | # Create Flask application 13 | app = Flask(__name__) 14 | app.config.from_object("config") 15 | 16 | # Import the routes After the Flask app is created 17 | user_module_name = \ 18 | os.path.split( 19 | os.path.dirname(os.path.abspath(os.path.dirname(__file__))))[1] 20 | submodule_name = os.path.split(os.path.abspath(os.path.dirname(__file__)))[1] 21 | for file in os.listdir(os.path.dirname(__file__)): 22 | if file.endswith('.py') and not file.startswith('_'): 23 | module = file[:file.find('.py')] 24 | importlib.import_module(user_module_name + '.' + 25 | submodule_name + '.' + module) 26 | 27 | # Set up logging for production 28 | print("Setting up logging for {}...".format(__name__)) 29 | app.logger.propagate = False 30 | if __name__ != "__main__": 31 | gunicorn_logger = logging.getLogger("gunicorn.error") 32 | app.logger.handlers = gunicorn_logger.handlers 33 | app.logger.setLevel(gunicorn_logger.level) 34 | # Make all log formats consistent 35 | formatter = logging.Formatter( 36 | "[%(asctime)s] [%(levelname)s] [%(module)s] %(message)s", 37 | "%Y-%m-%d %H:%M:%S %z" 38 | ) 39 | for handler in app.logger.handlers: 40 | handler.setFormatter(formatter) 41 | app.logger.info("Logging handler established") 42 | 43 | app.logger.info("Service inititalized!") 44 | 45 | -------------------------------------------------------------------------------- /amr_verbnet_semantics/grpc_defs/enhanced_amr_pb2_grpc.py: -------------------------------------------------------------------------------- 1 | # Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT! 2 | import grpc 3 | 4 | from . import enhanced_amr_pb2 as enhanced__amr__pb2 5 | 6 | 7 | class AMR_EnhancerStub(object): 8 | # missing associated documentation comment in .proto file 9 | pass 10 | 11 | def __init__(self, channel): 12 | """Constructor. 13 | 14 | Args: 15 | channel: A grpc.Channel. 16 | """ 17 | self.get_enhanced_amr = channel.unary_unary( 18 | '/AMR_Enhancer/get_enhanced_amr', 19 | request_serializer=enhanced__amr__pb2.Sentence.SerializeToString, 20 | response_deserializer=enhanced__amr__pb2.Enhanced_amr.FromString, 21 | ) 22 | 23 | 24 | class AMR_EnhancerServicer(object): 25 | # missing associated documentation comment in .proto file 26 | pass 27 | 28 | def get_enhanced_amr(self, request, context): 29 | # missing associated documentation comment in .proto file 30 | context.set_code(grpc.StatusCode.UNIMPLEMENTED) 31 | context.set_details('Method not implemented!') 32 | raise NotImplementedError('Method not implemented!') 33 | 34 | 35 | def add_AMR_EnhancerServicer_to_server(servicer, server): 36 | rpc_method_handlers = { 37 | 'get_enhanced_amr': grpc.unary_unary_rpc_method_handler( 38 | servicer.get_enhanced_amr, 39 | request_deserializer=enhanced__amr__pb2.Sentence.FromString, 40 | response_serializer=enhanced__amr__pb2.Enhanced_amr.SerializeToString, 41 | ), 42 | } 43 | generic_handler = grpc.method_handlers_generic_handler( 44 | 'AMR_Enhancer', rpc_method_handlers) 45 | server.add_generic_rpc_handlers((generic_handler,)) -------------------------------------------------------------------------------- /amr_verbnet_semantics/core/extract_timeline.py: -------------------------------------------------------------------------------- 1 | import json 2 | import os 3 | import re 4 | 5 | import wikipedia 6 | 7 | 8 | def extract_time_line(page_title, output_dir): 9 | print("Crawling page <{}>".format(page_title)) 10 | 11 | timelines = dict() 12 | t = wikipedia.page(page_title) 13 | # print(t.content) 14 | # input() 15 | 16 | pattern = r"(===(?P