How to access intent name from my custom component

Hello All, I am creating my custom component to handle misspelling, I want to correct the misspelling based on the intent value but I cannot access it, its name value = ‘null’.

[

 from rasa.nlu.components import Component
 from rasa.nlu import utils
 from rasa.nlu.model import Metadata
 import os

 import typing
 from typing import Any, Optional, Text, Dict, List
 import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords

import json
import jsonpickle

from fuzzywuzzy import fuzz
from fuzzywuzzy import process

from spellchecker import SpellChecker
spell = SpellChecker()

class FuzzyExtractor(Component):
name = "FuzzyExtractor"
requires = ["message"]
defaults = {}
language_list = ["en"]
threshold = 90

def __init__(self, component_config=None, *args):
    super(FuzzyExtractor, self).__init__(component_config)

def train(self, training_data, cfg, **kwargs):
    pass

def process(self, message, **kwargs):
    
    is_season = False
    is_crop = False
    entity = ""
    seasons = []
    crops = []
    
    entities = message.get('entities')
    
    intent = message.data
    print("intent: ", intent)
    print("\n")
    
    stop_words = set(stopwords.words('english'))
    
    with open('seasons.txt', 'r') as file:
        seasons = file.read().splitlines()
        file.close()
    
    with open('crops.txt', 'r') as file:
        crops = file.read().splitlines()
        file.close()
        
    tokens = message.get('tokens')
    
    for token in tokens:
        
        print("token json: ")
        print(jsonpickle.encode(token))
        print("\n")
        corrected_token = spell.correction(token.text)
        
        print(corrected_token)
        print("\n")
        
        for key, value in token.data["pattern"].items():
            if key.strip() == "seasons" and value == True:
                is_season = True
                entity = "seasons"
                lookup_data = seasons
                print("is_season: ", is_season)
            elif key.strip() == "crops" and value == True:
                is_crop = True
                entity = "crops"
                lookup_data = crops
                print("is_crop: ", is_crop)

        is_entity = is_season or is_crop
        
        if corrected_token not in stop_words:
            fuzzy_results = process.extract(
                                     corrected_token,
                                     crops,
                                     processor=lambda a: a['value']
                                         if isinstance(a, dict) else a,
                                     limit=3)
                                     
            print("fuzzy_results: ")
            print(jsonpickle.encode(fuzzy_results))
            print("\n")
            
            for result, confidence in fuzzy_results:
                if confidence >= self.threshold:
                    end = token.start + len(corrected_token)
                    entities.append({
                        "start": token.start,
                        "end": end,
                        "value": corrected_token,
                        "fuzzy_value": result,
                        "confidence": confidence,
                        "entity": entity
                    })
            is_season = False
            is_crop = False

    print(entities)
    print("\n")
    message.set("entities", entities, add_to_output=True)
    print(jsonpickle.encode(message))
    print("\n")

]

1 Like