Highlight key words from an ordered text from a ready list

0

I have a very basic basis that I would like to highlight the main words of each row in a prioritization list, eg:

Basis:

Bom dia 
que dia lindo 
vamos embora 
vamos chutar o dia

Priority list: bom dia vamos lindo

Expected result:

Bom dia - Bom 
que dia lindo - Dia 
vamos embora - vamos 
vamos chutar o dia - dia

I was able to find only one variable and not a list. Code:

texto = 'dia' 

for lin in open(r"C:\Users\guilgig\Desktop\teste.txt"): 
    if texto in lin: 
        print (texto, lin)
    
asked by anonymous 21.02.2018 / 23:01

2 answers

2

The code that you have done is already very close to what you need, you simply define the prioritization list and scroll through it by searching for the words in the sentence:

with open('teste.txt') as stream:
    for line in stream:
        for word in ['bom', 'dia', 'vamos', 'lindo']:
            if word in line:
                print(line.strip(), '-', word)
                break

See working at Repl.it

As for with to open the file, you can read: What is with in Python? . For micro-optimizations, you can define your list of words with a tuple ( tuple ) or even a set ( set ), which, for this purpose, have advantages both in terms of memory storage and access speed to the elements.

    
21.02.2018 / 23:54
0

You can do this using two for :

principais_palavras = "bom dia vamos lindo"

base = """Bom dia
que dia lindo
vamos embora
vamos chutar o dia"""

lista_prioritaria = principais_palavras.lower().split(" ")
linhas = base.lower().split("\n")

for linha in linhas:
  for palavra in lista_prioritaria:
    if (palavra in linha):
      print(linha + " - " + palavra)
      break

The logic of reading the file is not part of the algorithm, so it was left out, but just adapt to your case.

    
21.02.2018 / 23:53