Skip to content

This function highlights specified entities in a text string with specified background colors, font colors, and optional labels. Additionally, it allows setting a specific font type for highlighted text.

Usage

highlight_text(text, entities_mapping, font_family = "Arial")

Arguments

text

A character string containing the text to highlight.

entities_mapping

A named list of lists, with each sub-list containing:

  • words: A character vector of words to highlight.

  • background_color: A character string specifying the CSS color for the highlight background.

  • font_color: A character string specifying the CSS color for the highlighted text.

  • label: A character string specifying a label to append after each highlighted word.

  • label_color: A character string specifying the CSS color for the label text.

font_family

A character string specifying the CSS font family for the highlighted text and label. Default is "Arial".

Value

An HTML object containing the text with highlighted entities.

Examples

library(flaiR)
data("uk_immigration")
uk_immigration <- head(uk_immigration, 1)
tagger_ner <- load_tagger_ner("ner")
results <- get_entities(uk_immigration$text,
                        uk_immigration$speaker,
                        tagger_ner,
                        show.text_id = FALSE)

highlighted_text <- highlight_text(uk_immigration$text, map_entities(results))
print(highlighted_text)
#> <div style="text-align: justify; font-family: Arial">I thank Mr. Speaker for giving me permission to hold this debate today. I welcome the Minister-I very much appreciate the contact from his office prior to today-and the <span style="background-color: pink; color: black; font-family: Arial">Conservative</span> <span style="color: pink; font-family: Arial">(ORG)</span> and <span style="background-color: pink; color: black; font-family: Arial">Liberal Democrat Front Benchers</span> <span style="color: pink; font-family: Arial">(ORG)</span> to the debate. I also welcome my hon. Friends on the <span style="background-color: yellow; color: black; font-family: Arial">Back Benches</span> <span style="color: orange; font-family: Arial">(MISC)</span>. Immigration is the most important issue for my constituents. I get more complaints, comments and suggestions about immigration than about anything else. In the <span style="background-color: lightblue; color: black; font-family: Arial">Kettering</span> <span style="color: blue; font-family: Arial">(LOC)</span> constituency, the number of immigrants is actually very low. There is a well-settled <span style="background-color: yellow; color: black; font-family: Arial">Sikh</span> <span style="color: orange; font-family: Arial">(MISC)</span> community in the middle of <span style="background-color: lightblue; color: black; font-family: Arial">Kettering</span> <span style="color: blue; font-family: Arial">(LOC)</span> town itself, which has been in <span style="background-color: lightblue; color: black; font-family: Arial">Kettering</span> <span style="color: blue; font-family: Arial">(LOC)</span> for some 40 or 50 years and is very much part of the local community and of the fabric of local life. There are other very small migrant groups in my constituency, but it is predominantly made up of indigenous <span style="background-color: yellow; color: black; font-family: Arial">British</span> <span style="color: orange; font-family: Arial">(MISC)</span> people. However, there is huge concern among my constituents about the level of immigration into our country. I believe that I am right in saying that, in recent years, net immigration into the <span style="background-color: lightblue; color: black; font-family: Arial">United Kingdom</span> <span style="color: blue; font-family: Arial">(LOC)</span> is the largest wave of immigration that our country has ever known and, proportionately, is probably the biggest wave of immigration since the <span style="background-color: yellow; color: black; font-family: Arial">Norman</span> <span style="color: orange; font-family: Arial">(MISC)</span> conquest. My contention is that our country simply cannot cope with immigration on that scale-to coin a phrase, we simply cannot go on like this. It is about time that mainstream politicians started airing the views of their constituents, because for too long people have muttered under their breath that they are concerned about immigration. They have been frightened to speak out about it because they are frightened of being accused of being racist. My contention is that immigration is not a racist issue; it is a question of numbers. I personally could not care tuppence about the ethnicity of the immigrants concerned, the colour of their skin or the language that they speak. What I am concerned about is the very large numbers of new arrivals to our country. My contention is that the <span style="background-color: lightblue; color: black; font-family: Arial">United Kingdom</span> <span style="color: blue; font-family: Arial">(LOC)</span> simply cannot cope with them.</div>