Detect financial entities

Description

Extract key entities in financial contracts using pretrained NER model.

Predicted Entities

ORG, PER, MISC, LOC

Open in Colab Copy S3 URI

How to use


document_assembler = DocumentAssembler()\
    .setInputCol("text")\
    .setOutputCol("document")

sentenceDetector = SentenceDetectorDLModel.pretrained("sentence_detector_dl_healthcare","en","clinical/models")\
    .setInputCols(["document"])\
    .setOutputCol("sentence")

tokenizer = Tokenizer()\
    .setInputCols(["sentence"])\
    .setOutputCol("token")

embeddings = WordEmbeddingsModel.pretrained("glove_6B_300", "xx")\
    .setInputCols("sentence", "token")\
    .setOutputCol("embeddings")

clinical_ner = MedicalNerModel.pretrained("ner_financial_contract", "en", "clinical/models")\
    .setInputCols(["sentence", "token", "embeddings"])\
    .setOutputCol("ner")

ner_converter = NerConverter()\
    .setInputCols(["sentence", "token", "ner"])\
    .setOutputCol("ner_chunk")

nlpPipeline = Pipeline(stages=[document_assembler, sentenceDetector, tokenizer, embeddings, clinical_ner, ner_converter])

empty_data = spark.createDataFrame([[""]]).toDF("text")

model = nlpPipeline.fit(empty_data)

text = """Hans is a professor at the Norwegian University of Copenhagen, and he is a true Copenhagener."""

result = model.transform(spark.createDataFrame([[text]]).toDF("text"))


document_assembler = new DocumentAssembler()
    .setInputCol("text")
    .setOutputCol("document")

sentenceDetector = SentenceDetectorDLModel.pretrained("sentence_detector_dl_healthcare","en","clinical/models")
    .setInputCols("document")
    .setOutputCol("sentence")

tokenizer = new Tokenizer()
    .setInputCols("sentence")
    .setOutputCol("token")

embeddings = WordEmbeddingsModel.pretrained("glove_6B_300", "xx")
    .setInputCols(Array("sentence", "token"))
    .setOutputCol("embeddings")

clinical_ner = MedicalNerModel.pretrained("ner_financial_contract", "en", "clinical/models")
    .setInputCols(Array("sentence", "token", "embeddings"))
    .setOutputCol("ner")

ner_converter = new NerConverter()
    .setInputCols(Array("sentence", "token", "ner"))
    .setOutputCol("ner_chunk")

val pipeline = new Pipeline().setStages(Array(document_assembler, sentenceDetector, tokenizer, embeddings, clinical_ner, ner_converter))

val data = Seq("""Hans is a professor at the Norwegian University of Copenhagen, and he is a true Copenhagener.""").toDS.toDF("text")

val result = pipeline.fit(data).transform(data)
import nlu
nlu.load("en.med_ner.financial_contract").predict("""Put your text here.""")

Results

+--------------------+---------+
|chunk               |ner_label|
+--------------------+---------+
|professor           |PER      |
|Norwegian University|PER      |
|Copenhagen          |LOC      |
+--------------------+---------+

Model Information

Model Name: ner_financial_contract
Compatibility: Healthcare NLP 3.0.0+
License: Licensed
Edition: Official
Input Labels: [sentence, token, embeddings]
Output Labels: [ner]
Language: en