Сопоставление всех элементов списка с каждой записью ввода - PullRequest
0 голосов
/ 29 апреля 2020

У меня есть два текстовых файла. Одним из них является список (пары ключ-значение) элементов, а другим - входной файл, которому должны соответствовать пары ключ-значение. Если совпадение найдено, оно помечается соответствующим значением во входном файле.

Например:

мой список файлов:

food = ###food123
food microbiology = ###food mircobiology456
mirco organism = ###micro organims789
geo tagging = ###geo tagging614
gross income = ###gross income630
fermentation = fermentation###929
contamination = contamination##878
Salmonella species = Salmonella species###786
Lactic acid bacteria = Lactic acid bacteria###654

входной файл:

There are certain attributes necessary for fermentation of meat. 
It should be fresh, with low level of contamination, the processing should be hygienic and the refrigeration should be resorted at different stages.
Elimination of pathogens like Coliform, Staphylococci, Salmonella species may be undertaken either by heat or by irradiation. There is need to lower the water activity and this can be achieved by either drying or addition of the salts. 
Inoculation with an effective, efficient inoculum consisting of Lactic acid bacteria and, or Micrococci which produces lactic acid and also contributes to the flavor development of the product. 
Effective controlled time, temperature humidity during the production is essential.
And, Salt ensures the low pH value and extends the shelf-life of the fermented meats like Sausages.

Ожидаемый результат:

There are certain attributes necessary for ((fermentation###929)) of meat. 
It should be fresh, with low level of ((contamination##878)), the processing should be hygienic and the refrigeration should be resorted at different stages.
Elimination of pathogens like Coliform, Staphylococci, ((Salmonella species###786)) may be undertaken either by heat or by irradiation. There is need to lower the water activity and this can be achieved by either drying or addition of the salts. 
Inoculation with an effective, efficient inoculum consisting of ((Lactic acid bacteria###654)) and, or Micrococci which produces lactic acid and also contributes to the flavor development of the product. 
Effective controlled time, temperature humidity during the production is essential.
And, Salt ensures the low pH value and extends the shelf-life of the fermented meats like Sausages.

Для этого я использую python3, анализ файла списка и сохранение его в ха sh. Ха sh имеет все элементы списка в виде пар ключ-значение. Затем каждая строка входного файла сопоставляется со всеми ключами, присутствующими в ha sh, и когда совпадение найдено, соответствующее значение ha sh заменяется, как показано в выходных данных.

Этот метод отлично работает, когда Размер входных данных и списка невелик, но когда размер списка и входных данных увеличивается, на это уходит много времени.

Как улучшить временную сложность этого метода сопоставления?

Я использую алгоритм:

#parse list and store in hash
for l in list:
  ll = l.split("=")
  hash[ll[0]] = ll[1]

#iterate input and match with each key
keys = hash.keys()
for line in lines:
    if(line != ""):
        for key in keys:
            my_regex = r"([,\"\'\( \/\-\|])" + key + r"([ ,\.!\"।\'\/\-)])"
            if((re.search(my_regex, line, re.IGNORECASE|re.UNICODE))):
                 line = re.sub(my_regex, r"\1" + "((" + hash[key] + "))" + r"\2",line)
...