Skip to content

Instantly share code, notes, and snippets.

@netcarver
Last active October 18, 2021 06:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netcarver/0ba957e6a52f6d1760db94d6bb4d8413 to your computer and use it in GitHub Desktop.
Save netcarver/0ba957e6a52f6d1760db94d6bb4d8413 to your computer and use it in GitHub Desktop.
Custom Rasa NLU Pipeline Component
# -*- coding: utf-8 -*-
from rasa_nlu.components import Component
"""
About
-----
The Spacy tokenizer uses lowercase by default, so training an NLU model using
ner_crf or ner_spacy to collect case sensitive data can be a problem if you
want to keep the rest of the tokenizer lowercase. This pipeline action restores
the case sensitivity for any of the entities you are interested in keeping
title-cased.
Also useful for correcting people's names when they don't bother typing
their own name in titlecase.
To Use
------
Add the following to your NLU pipeline configuration after entity extraction...
- name: "TitlecaseNamedEntities.TitlecaseNamedEntities"
entities:
- name
Where 'name' is the entity being post-processed.
License
-------
Copyright 2018-2019 Netcarver
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
class TitlecaseNamedEntities(Component):
name = "titlecase_named_entities"
requires = ["entities"]
provides = ["enitites"]
entities_to_convert = []
def __init__(self, component_config=None):
super(TitlecaseNamedEntities, self).__init__(component_config)
self.entities_to_convert = component_config['entities']
if (len(self.entities_to_convert) == 0):
self.entities_to_convert = ['name']
def process(self, message, **kwargs):
ents = message.get("entities", [])
if (len(ents)):
for ent in ents:
if ent['entity'] in self.entities_to_convert:
source_txt = message.text[ent['start']:ent['end']]
replacement = source_txt.title()
original = ent['value']
ent['value'] = replacement
ent['modifiers'] = [self.name]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment