Package: cltl.triple-extraction
cltl-knowledgeextractionA knowledge extraction service (aka Leolani's Triple Extractor package). This service performs Natural Language
Understanding through Grammars natural language textual data and outputs structured data.DescriptionThis package allows extracting structured information, in the form of SPO triples, from natural language textual data.
It features:An Utterance is analyzed with the help of the Analyzer class. It extracts structured data in the form of a list of
triples, where each triple has a subject, predicate and object.If an Utterance is a statement, it also has a Perspective object which consists of a polarity, certainty, sentiment
and emotion value.The Analyzer class is the API for triple extractors. At this moment there are two Analyzers implemented: CFGAnalyzer
(which uses Context Free Grammar parsing), and OIE Analyzer (which uses Stanford OIE library)The CFGAnalyzer consists of a hierarchy of classes, topmost class is the abstract general class Analyzer, which is
separated into two abstract classes StatementAnalyzer and QuestionAnalyzer, which consist of the concrete classes
GeneralStatementAnalyzer, WhQuestionAnalyzer and VerbQuestionAnalyzer.Triple extraction implementationsThe triples consist of subject, predicate and object alongside with their semantic types. In case of a statement, the
triple is accompanied by a perspective. In the case of a question the triple is incomplete. Below are a few examples of
the triples which are the output of analyzers:“My sister enjoys eating cakes” lenka-sister_enjoy_eating-cakes“What does my sister enjoy?” lenka-sister_enjoy_?The elements of the triple are separated with underscore; while dash is used to separate elements of multiword
expressions. When a multiword expression is actually a collocation, the multiword expression is marked with apostrophes
during the analysis (e.g. ”mexico-city”)to ensure that subparts of collocations are not analyzed separately.
implementationsCFGAnalyzerBasic rules that the CFGAnalyzer follows are:predicates are lemmatized verbs, with possible prepositions connected to the verb“live-in”, “come-from”, etcmodal verbs are analyzed using the lexicon and their modality is stored as one of the perspective values“might-come”- {'polarity': 1, 'certainty': '0.5', 'sentiment': 0}negation is removed after processing and stored within the perspective object as polarityI think selene doesn't like cheese = “selene_like_cheese” - {'polarity': -1, ' certainty': '0.75', 'sentiment': ’0.75'}(I think selene hates cheese = “selene_hate_cheese” - {'polarity': 1, 'certainty': '0.75', 'sentiment': '-1'}properties end with “-is”(this way it is quite easy for NLG)My favorite color is green = lenka_favorite-color-is_greenwords that refer to a person are grouped together in the subject unless the verb is just “be”, in this case they are
processed like properties (“sister-is”)My best friend is Selene = lenka_best-friend-is_seleneMy best friend’s name is Selene = lenka-best-friend_name-is_seleneadjectives, determiners and numbers are joined with the noun“a-dog”, “the-blue-shirt”, etc.Below is a short summary of NLP that happens during the CFG utterance analysis:Tokenization and replacing contractions with long variants of aux verbsPOS tagging (NLTK and Stanford + would be good to add an additional tagger to use when the two have a mismatch)CFG parsing using the grammar which is manually designedAnalyzer class maps the output of CFG parsing to the subject-predicate-object triple, following the rules which are
mentioned aboveLemmatization using NLTKModal verbs are analyzed using the lexicon and this is stored within PerspectiveChecking whether some of the multi-word elements are actually collocations such as New York or ice-cream (these
should be processed as one word)Getting semantic types of each element of the triple, and its subparts, using the manually made lexicon, WordNet
lexname, Stanford NERConversationalAnalyzerExtract triples taking the conversational context into account.ModelsModels for the ConversationalAnalyzer can be downloaded
fromResearchDrive. These files must be under the
folderresources/conversational_triples/Sample outputHere is a sample output for sentence“I have three white cats”:{"subject":{"text":"Lenka","type":["person"]},"predicate":{"text":"have","type":["verb.possession"]},"object":{"text":"three-white-cats","type":["adj.all","noun.animal"]},"utterance type":"STATEMENT","perspective":{"polarity":1,"certainty":1,"sentiment":0}}Getting startedPrerequisitesThis repository uses Python >= 3.7Be sure to run in a virtual python environment (e.g. conda, venv, mkvirtualenv, etc.)InstallationIn the root directory of this repo runpipinstall-e.
python-c"import nltk; nltk.download('wordnet'); nltk.download('punkt'); nltk.download('averaged_perceptron_tagger')"python-mspacydownloaden_core_web_smIn case you want to use this package in the EventBus infrastructure, then install using:pipinstall-e.[service]python-c"import nltk; nltk.download('wordnet'); nltk.download('punkt'); nltk.download('averaged_perceptron_tagger')"python-mspacydownloaden_core_web_smIn case you want to run the OpenIE function from StanfordCoreNLP, you need to download "stanford-corenlp-4.1.0" and
unpack it in the folder
~/.stanfordnlp_resources.UsageFor using this repository as a package different project and on a different virtual environment, you mayinstall a published version from PyPI:pipinstallcltl.triple_extractoror, for the latest snapshot, run:pipinstallgit+git://github.com/leolani/cltl-knowledgeextraction.git@mainThen you can import it in a python script as:fromcltl.triple_extraction.apiimportChatfromcltl.triple_extraction.cfg_analyzerimportCFGAnalyzerfromcltl.triple_extraction.utils.helper_functionsimportutterance_to_capsuleschat=Chat("Leolani","Lenka")analyzer=CFGAnalyzer()chat.add_utterance("I have three white cats")analyzer.analyze_in_context(chat)capsules=utterance_to_capsules(chat.last_utterance)ExamplesPlease take a look at the example scripts provided to get an idea on how to run and use this package. Each example has a
comment at the top of the script describing the behaviour of the script.For these example scripts, you needTo change your current directory to ./examples/Run some examples (e.g. python test_with_triples.py)ContributingContributions are what make the open source community such an amazing place to learn, inspire, and create. Any
contributions you make aregreatly appreciated.Fork the ProjectCreate your Feature Branch (git checkout -b feature/AmazingFeature)Commit your Changes (git commit -m 'Add some AmazingFeature')Push to the Branch (git push origin feature/AmazingFeature)Open a Pull RequestLicenseDistributed under the MIT License.
SeeLICENSEfor more information.AuthorsSelene Báez SantamaríaThomas BaierPiek Vossen