2

I'm trying to create a Sublime Text plugin (using python) that reverses the order of words in a selected string. I've got the main functionality working but now my problem is that every symbol at the end of a word (periods, commas, question marks etc) stays where it is, and my goal is to get everything reversed properly so the symbols should move to the start of the word.

def run(self, edit):
    selections = self.view.sel()

    # Loop through multiple text selections
    for location in selections:

        # Grab selection
        sentence = self.view.substr(location)

        # Break the string into an array of words
        words = sentence.split()

        # The greasy fix
        for individual in words:
            if individual.endswith('.'):
                words[words.index(individual)] = "."+individual[:-1]

        # Join the array items together in reverse order
        sentence_rev = " ".join(reversed(words))

        # Replace the current string with the new reversed string
        self.view.replace(edit, location, sentence_rev) 

    # The quick brown fox, jumped over the lazy dog.
    # .dog lazy the over jumped ,fox brown quick The

I've been able to loop through each word and use the endswith() method for a quick fix but this will not find multiple symbols (without a long list of if statements) or account for multiple symbols and move them all.

I've been playing around with regex but still haven't got a solution that works, and I've been looking for a way to change the index of the symbol but still nothing...

If I can give any more details please let me know.

Thanks!

Sintyche
  • 392
  • 1
  • 3
  • 8

2 Answers2

0

If you import re you can change your split() line to split on the word break \b instead:

words = re.sub(r'\b', '\f', sentence).split('\f')

See this for why you can't just split(r'\b'). The above will give you:

['', 'The', ' ', 'quick', ' ', 'brown', ' ', 'fox', ', ', 'jumps', ' ', 'over', ' ', 'the', ' ', 'lazy', ' ', 'dog', '.']

Which you can then easily reverse and get your symbols in the correct place.

Community
  • 1
  • 1
mVChr
  • 49,587
  • 11
  • 107
  • 104
  • 1
    Thanks! That's really helpful and a way of doing it that I would of never thought of (or known). Now when then text is translated it adds all of the extra white space from the empty arrays. Is there a way to take them out? (I'm new to regex and I'm still trying to work out exactly what everything means) – Sintyche Dec 14 '12 at 21:02
  • @Sintyche You can remove the extra whitespaces in the result with a list-comprehension. Something like `[elem for elem in re.sub(r'\b', '\f', sentence).split('\f') if elem.strip()]`. – Bakuriu Dec 14 '12 at 22:23
0

I expect regex is a better way to go, but in case it helps ...

You could have a function that you call in place of using endswith ...

def ends_with_punctuation(in_string):
    punctuation = ['.', ',', ':', ';', '!', '?']
    for p in punctuation:
        if in_string.endswith(p):
            return True
    return False
jcfollower
  • 3,103
  • 19
  • 25