0

I am trying to remove suffixes but for some reason, it is not working. Code is:

# Stemming
    suffix_list = ['-ed', '-ing', '-s']

    for word in range(len(output)):  # loop
        # range returns the sequence, len checks the lenght
        for suffix in range(len(suffix_list)):
            # .endswith checks x in both
            if output[word].endswith(suffix_list[suffix]):
                # .removesuffix removes from output if in suffix_list
                print(output[word].removesuffix(suffix_list[suffix]))

    return output

print(textPreprocessing("I'm gathering herbs."))
print(textPreprocessing("When life gives you lemons, make lemonade"))

Outcome is:

gather
herb
['im', 'gathering', 'herbs']
give
lemon
['life', 'gives', 'you', 'lemons', 'make', 'lemonade']

Where it should be:

['im', 'gather', 'herbs']
['life', 'give', 'you', 'lemon', 'make', 'lemonade']

Any help? I feel like I am missing something obvious...

martineau
  • 119,623
  • 25
  • 170
  • 301

2 Answers2

0
output[word] = output[word].removesuffix(suffix_list[suffix])
sandeep.kgp
  • 847
  • 2
  • 10
0

This will work:-

str_sample = "When life gives you lemons make lemonade"
suffixes, words = ["s"], str_sample.split()

for i in range(len(suffixes)):
    for j in range(len(words)):
        if words[j].endswith(suffixes[i]):
            words[j] = words[j].replace(f"{suffixes[i]}", "")

print(words)

It can be compressed to a List Comprehension as :-

str_sample = "When life gives you lemons make lemonade"
suffixes, words = ["s"], str_sample.split()

res = [words[j].replace(f"{suffixes[i]}", "") if words[j].endswith(suffixes[i]) else words[j] for j in range(len(words)) for i in range(len(suffixes))]

print(words)
MK14
  • 481
  • 3
  • 9