Skip to content

Context matching

melisa-qordoba edited this page Sep 23, 2020 · 1 revision

Context matching

Currently replaCy only supports string-based context using match hooks. The way to do this is by using the match hooks part_of_phrase and sentence_has. The match hook part_of_phrase checks if the matched phrase is part of the given input phrase to the hook, and sentence_has checks whether or not the sentence including the matched phrase contains the word or words given as input. An example would be if you wanted to suggest that the word "apples" be changed to its scientific name, except when it's a Rick and Morty reference. To do this you could use the following:

{
    ...,
    "apples-example": {
        "patterns": [
            {
                "LEMMA": "apple",
                "TEMPLATE_ID": 1
            }
        ],
        "suggestions": [
            [
                {
                    "TEXT": "malus"
                },
                {
                    "TEXT": "domestica",
                    "FROM_TEMPLATE_ID": 1
                }
            ]
        ],
        "match_hook": [
            {
                "name": "part_of_phrase",
                "args": "hungry for apples",
                "match_if_predicate_is": false
            },
            {
                "name": "sentence_has",
                "args": [
                    "rick",
                    "morty",
                    "jerry",
                    "wubba lubba"
                ],
                "match_if_predicate_is": false
            }
        ],
        "comment": "Change apple to its scientific name unless part of a Rick and Morty reference"
    }
}
Clone this wiki locally