Skip to content

Instantly share code, notes, and snippets.

@desis123
Last active November 6, 2022 15:03
Show Gist options
  • Save desis123/66f94ec9553d1a649bd0c1206a6760cc to your computer and use it in GitHub Desktop.
Save desis123/66f94ec9553d1a649bd0c1206a6760cc to your computer and use it in GitHub Desktop.
Removing n repetition of words from sentence
text = """
<span><span>Jenny.</span></span> <span><span>Jennie comes from a wealthy family, but she has also generated her own income.</span></span> <span><span>She is the image of brands such as Calvin Klein, Hera, Samsung, adidas, Marie Claire, Vogue and Cosmopolitan.</span></span> <span><span>Her of her of her of her of her of her of her of her of her of her of her estate of her of her of her of her of her of her of her of her of her of her of her of her of her of her of her of her of her of her of her of her of her of her of her of her of her of her of her of her of her of her of her of her of her of her of her of her of her of her of her of it amounts to more than 10 million dollars.</span></span></p>
"""
def chunks(lst, n):
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(lst), n):
yield " ".join(lst[i:i + n])
def removeConsecutiveDuplicateWors(s):
#splitting to words
ntext = s.split()
st = (list(chunks(ntext, 2)))
print(st)
if len(st) < 2:
return " ".join(st)
if st[0] != st[1]:
nw = ("".join(st[0])) +" "+ removeConsecutiveDuplicateWors(" ".join(st[1:]))
return nw
return removeConsecutiveDuplicateWors(" ".join(st[1:]))
print(removeConsecutiveDuplicateWors(text))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment