3

I got two text files d.txt containing paragraph text and phrase.txt containing multi worded phrases like State-of-the-art, counter productive, Fleet Dynamism and like some found in link below

https://en.wikipedia.org/wiki/List_of_buzzwords

I need to color font matching phrase in d.txt if it is found in phrase.txt

Efforts so far:

phrases = open("phrase.txt").readlines()
words = open("d.txt").read()

for phrase in phrases:
    all_words_found = False
    phrase_words = phrase.lower().split(" ")
    for word in phrase_words:
        if word in words:
            all_words_found = True
            break

    if all_words_found:
        print (phrase)

Expected output: enter image description here

Please Help!

thanks for the help:

4
  • How is this text going to be displayed? In an html document? Commented Sep 17, 2020 at 0:37
  • I would ask the same question as MVB76 - what output are you looking for? If you are looking at printing out within python, look into ansi color escape codes. Commented Sep 17, 2020 at 0:48
  • I want as HTML output. Sorry I should have mentioned it. Commented Sep 17, 2020 at 8:45
  • If you want HTML output, just replace each phrase with an html tag that represents the color change you want rather than using ansi escape codes. I'll update my answer with an example. I'd personally want to make a css class in my larger project and apply that but for the example I woudl just as <span style="color:Red;"> PHRASE </span> Commented Sep 17, 2020 at 16:00

1 Answer 1

1

Update: to create html output

To change the code above to create html output add a tag around the word during replace rather than ansi. The example here will use a simple span tag

words = ["catch phrase", "codeword"]
phrase = "He said a catch phrase. And a codeword was written on a wall."

new_phrase = phrase
for word in words:
    new_phrase = new_phrase.replace(i, f'<span style="color:Red;">{word}</span>')
print(new_phrase) #Rather than printing, send this wherever you want it.

Solution for inline print

However, to answer your basic question, which is how to replace a set of words in a given paragraph with the same word in a different color, try using .replace() and ansi color escape codes. This will work if you want to print out the words in your python environment.

Here's a simple example of a way to turn certain words in a line of text red:

words = ["catch phrase", "codeword"]
phrase = "He said a catch phrase. And a codeword was written on a wall."

new_phrase = phrase
for i in words:
    new_phrase = new_phrase.replace(i, f'\033[91m{i}\033[0;0m')
print(new_phrase)

Here is another stackoverflow post which talks about ANSI escape codes and colors in python output: How to print colored text in Python? ANSI escape codes are a way to change the color of your output - google them to find more options/colors.

In this example here are the codes I used: First to change the color to red:

\033[91m

Afer setting the color, you also have to change it back or the rest of the output will also be that color:

\033[0;0m
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.