0

I have a piece of code:

paragraphs = ['The tablets are filled into cylindrically shaped bottles made of white coloured\npolyethylene. The volumes of the bottles depend on the tablet strength and amount of\ntablets, ranging from 20 to 175 ml. The screw type cap is made of white coloured\npolypropylene and is equipped with a tamper proof ring.', 'PVC/PVDC blister pack', 'Blisters are made in a cold-forming process from an aluminium base web. Each tablet is\nfilled into a separate blister and a lidding foil of aluminium is welded on. The blisters\nare opened by pressing the tablets through the lidding foil.', '\n']

final_ref = [['Blister', 'Foil', 'Aluminium'], ['Blister', 'Base Web', 'PVC/PVDC'], ['Bottle', 'Cylindrically shaped Bottles', 'Polyethylene'], ['Bottle', 'Screw Type Cap', 'Polypropylene'], ['Blister', 'Base Web', 'PVC'], ['Blister', 'Base Web', 'PVD/PVDC'], ['Bottle', 'Square Shaped Bottle', 'Polyethylene']]

colours = ['White', 'Yellow', 'Blue', 'Red', 'Green', 'Black', 'Brown', 'Silver', 'Purple', 'Navy blue', 'Gray', 'Orange', 'Maroon', 'pink', 'colourless', 'blue']

TEXT_WITHOUT_COLOUR = 'Stage {counter} : Package Description: {sen} Values: {values}'

TEXT_WITH_COLOUR = TEXT_WITHOUT_COLOUR + ' Colour: {colour}'

counter = 1
result = []


def is_missing(words, sen):
    for w in words:
        if w.lower() not in sen.lower():
            return True
    return False


for words in final_ref:
    for sen in paragraphs:
        if is_missing(words, sen):
            continue

        kwargs = {
            'counter': counter,
            'sen': sen,
            'values': str(words)
        }

        if words[0] == 'Bottle':
            for wd in colours:
                if wd.lower() in sen.lower():
                    kwargs['colour'] = wd
                    break
            text_const = TEXT_WITH_COLOUR
        else:
            text_const = TEXT_WITHOUT_COLOUR

        result.append(text_const.format(**kwargs).replace('\n', '').replace('\t', ''))
        counter += 1

print(result)

which returns output as:

["Stage 1 : Package Description: Blisters are made in a cold-forming process from an aluminium base web. Each tablet isfilled into a separate blister and a lidding foil of aluminium is welded on. The blistersare opened by pressing the tablets through the lidding foil. Values: ['Blister', 'Foil', 'Aluminium']",

"Stage 2 : Package Description: The tablets are filled into cylindrically shaped bottles made of white colouredpolyethylene. The volumes of the bottles depend on the tablet strength and amount oftablets, ranging from 20 to 175 ml. The screw type cap is made of white colouredpolypropylene and is equipped with a tamper proof ring. Values: ['Bottle', 'Cylindrically shaped Bottles', 'Polyethylene'] Colour: White",

"Stage 3 : Package Description: The tablets are filled into cylindrically shaped bottles made of white colouredpolyethylene. The volumes of the bottles depend on the tablet strength and amount oftablets, ranging from 20 to 175 ml. The screw type cap is made of white colouredpolypropylene and is equipped with a tamper proof ring. Values: ['Bottle', 'Screw Type Cap', 'Polypropylene'] Colour: White"]

What I want to do is to check the content of the 'Package Description' and if it is the same, I want to group all the different 'Values' under the same Group Number

So, I want the output to come in the following format:

["Group 1: Package Description: Blisters are made in a cold-forming process from an aluminium base web. Each tablet isfilled into a separate blister and a lidding foil of aluminium is welded on. The blistersare opened by pressing the tablets through the lidding foil. Values: ['Blister', 'Foil', 'Aluminium'], 


"Group 2: Package Description: The tablets are filled into cylindrically shaped bottles made of white colouredpolyethylene. The volumes of the bottles depend on the tablet strength and amount oftablets, ranging from 20 to 175 ml. The screw type cap is made of white colouredpolypropylene and is equipped with a tamper proof ring. Values: ['Bottle', 'Cylindrically shaped Bottles', 'Polyethylene']Colour: white", 

"Group 2: Package Description: The tablets are filled into cylindrically shaped bottles made of white colouredpolyethylene. The volumes of the bottles depend on the tablet strength and amount oftablets, ranging from 20 to 175 ml. The screw type cap is made of white colouredpolypropylene and is equipped with a tamper proof ring. Values: ['Bottle', 'Screw Type Cap', 'Polypropylene']Colour: white"]

Can anyone help me with the same.

2
  • why stage 2 and stage 3 are grouped into(Group 2) even their some content are not same Commented Nov 22, 2019 at 9:29
  • Because their Package Description is the same. I want to group by Package Description and not by the 'Values' Commented Nov 22, 2019 at 9:38

1 Answer 1

1

This solution is based mainly in regex and loop. The first regex finds the text pattern in between 'Package Description:' and 'Values:' and second one replaces Stage + number for Group and its respective group number.

import re

unique_desc = [] #every unique description is stored 
output      = [] 

for desc in result:

    compare = re.search(r'Package Description:(.*?)Values:',desc).group(1).replace(' ','') #clean spaces

    if compare in unique_desc:  

        group = str(unique_desc.index(compare)+1) #index starts in 0 and group in 1     
        desc = re.sub('Stage \d','Group '+group, desc)
        output.append(desc)

    else: 

        unique_desc.append(compare)     
        group = str(len(unique_desc))    #new group

        desc = re.sub('Stage \d','Group '+group, desc)
        output.append(desc)

And the result (new one) in the list named output:

print(output)
["Group 1 : Package Description: Blisters are made in a cold-forming process from an aluminium base web. Each tablet isfilled into a separate blister and a lidding foil of aluminium is welded on. The blistersare opened by pressing the tablets through the lidding foil. Values: ['Blister', 'Foil', 'Aluminium']", "Group 2 : Package Description: The tablets are filled into cylindrically shaped bottles made of white colouredpolyethylene. The volumes of the bottles depend on the tablet strength and amount oftablets, ranging from 20 to 175 ml. The screw type cap is made of white colouredpolypropylene and is equipped with a tamper proof ring. Values: ['Bottle', 'Cylindrically shaped Bottles', 'Polyethylene'] Colour: White", "Group 2 : Package Description: The tablets are filled into cylindrically shaped bottles made of white colouredpolyethylene. The volumes of the bottles depend on the tablet strength and amount oftablets, ranging from 20 to 175 ml. The screw type cap is made of white colouredpolypropylene and is equipped with a tamper proof ring. Values: ['Bottle', 'Screw Type Cap', 'Polypropylene'] Colour: White"]

To sort. As the beginning of the 3 texts strings starts with Group # you can use sorted(). I placed a number 5 in the second element in the output list to show you how it goes:

output = ["Group 1 : Package Description: Blisters are made in a cold-forming process from an aluminium base web. Each tablet isfilled into a separate blister and a lidding foil of aluminium is welded on. The blistersare opened by pressing the tablets through the lidding foil. Values: ['Blister', 'Foil', 'Aluminium']", "Group 5 : Package Description: The tablets are filled into cylindrically shaped bottles made of white colouredpolyethylene. The volumes of the bottles depend on the tablet strength and amount oftablets, ranging from 20 to 175 ml. The screw type cap is made of white colouredpolypropylene and is equipped with a tamper proof ring. Values: ['Bottle', 'Cylindrically shaped Bottles', 'Polyethylene'] Colour: White", "Group 2 : Package Description: The tablets are filled into cylindrically shaped bottles made of white colouredpolyethylene. The volumes of the bottles depend on the tablet strength and amount oftablets, ranging from 20 to 175 ml. The screw type cap is made of white colouredpolypropylene and is equipped with a tamper proof ring. Values: ['Bottle', 'Screw Type Cap', 'Polypropylene'] Colour: White"]

output = sorted(output)

print(output)

And it prints:

["Group 1 : Package Description: Blisters are made in a cold-forming process from an aluminium base web. Each tablet isfilled into a separate blister and a lidding foil of aluminium is welded on. The blistersare opened by pressing the tablets through the lidding foil. Values: ['Blister', 'Foil', 'Aluminium']", "Group 2 : Package Description: The tablets are filled into cylindrically shaped bottles made of white colouredpolyethylene. The volumes of the bottles depend on the tablet strength and amount oftablets, ranging from 20 to 175 ml. The screw type cap is made of white colouredpolypropylene and is equipped with a tamper proof ring. Values: ['Bottle', 'Screw Type Cap', 'Polypropylene'] Colour: White", "Group 5 : Package Description: The tablets are filled into cylindrically shaped bottles made of white colouredpolyethylene. The volumes of the bottles depend on the tablet strength and amount oftablets, ranging from 20 to 175 ml. The screw type cap is made of white colouredpolypropylene and is equipped with a tamper proof ring. Values: ['Bottle', 'Cylindrically shaped Bottles', 'Polyethylene'] Colour: White"]

It works if your groups limited to 9. Remember when you order numbers as strings it goes like:

string_numbers = ['1','2','3','4','5','10','12','21']

sort = sorted(string_numbers)

print(sort)
['1', '10', '12', '2', '21', '3', '4', '5']

If you think it might be the case, then you shoud start a new question about how to order the list. It demands a new development, because it's not straightforward as using sorted().

Sign up to request clarification or add additional context in comments.

7 Comments

Getting an error in the line 'desc = re.sub('Stage \d','Group '+group)'. It says "TypeError: sub() missing 1 required positional argument: 'string'"
Yes, my bad, sorry I've just replaced the solution with the following desc = re.sub('Stage \d','Group '+group, desc)
No issues, thanks. But the output that I'm getting is : ["Group 2 : Package Description: The tablets are filled into cylindrically shaped bottles made of white colouredpolyethylene. The volumes of the bottles depend on the tablet strength and amount oftablets, ranging from 20 to 175 ml. The screw type cap is made of white colouredpolypropylene and is equipped with a tamper proof ring. Values: ['Bottle', 'Screw Type Cap', 'Polypropylene'] Colour: White"]
I need Group 1 values as well with the additional Package Description and values in Group 2. Right now just getting 1 value. Can you please check my expected output
@techsmart I made some mistakes with the code and paste from my code. I corrected them. The changes are after else statement. And I've printed out the output again. I hope it is allright now.
|

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.