0

I would like the program to be able to select or identify a sentence automatically (from one point to the next or from one point to the question mark or to the exclamation mark) and to order each sentence one by the number of words that each one has. So that the sentence with fewer words would be at one end and the one with more words at the other.

For exemple:

Normal text: The day was very difficult today. I hope the next few days get better. Do you hope the same? I hope you hope the same as me!

Sorted text:

  1. Do you hope the same?
  2. The day was very difficult today.
  3. I hope the next few days get better.
  4. I hope you hope the same as me!

it could also be: Do you hope the same? The day was very difficult today. I hope the next few days get better. I hope you hope the same as me!

I don’t know how to do this, can someone help me?

Thanks in advance

1
  • What is a "word" for you? Only alphabetics? Alphanumerics? Word character class [A-Za-z0-9_]? Alphanumerics with non latin characters? Everything that is not a space or punctuation? Something else? – Toto May 7 '20 at 10:29
0

The Replace dialog (Ctrl+H) in Notepad++ has the very powerful Regular expression search mode. It is possible to achieve all of what you want with that and two other standard Notepad++ functions.

First, in case any of your sentences are split across lines, remove all line endings by replacing [\r\n] with (a single space). Example:

The day was very difficult today.   I hope the next few days get better.   Do you hope the same?   I hope you hope the same as me!

Then put each sentence on its own line by replacing ([^.!?]+[.!?][ "])\s* with \1\r\n. This makes some assumptions about what kind of sentences you have and how well formatted they are, but it might be a good enough approximation. Notably though, "She looks... tired." will be messed up because of the three dots. Example:

The day was very difficult today. 
I hope the next few days get better. 
Do you hope the same? 
I hope you hope the same as me!

Regex does not support counting, so now we will have to be a little tricky. Call the current content file A. Make a copy, called file B.

In file B, replace # (in case there are any) with (a single space) and then replace \w[^\W]+\W with #. This is not perfect since regex word boundaries is not the same linguistic word boundaries. You can use a regex that matches words better in whatever language you have, eg [-_'0-9a-zA-Z]+ for English. Example:

# # # # # #. 
# # # # # # # #. 
# # # # #? 
# # # # # # # #!

Replace everything else, [^#\r\n]+, with nothing. Then add a space at the end of each line with by replacing $ with . Example:

###### 
######## 
##### 
######## 

Now block copy the entire file B. This is achieved by placing the cursor at the very beginning of the file. Then scroll to the very bottom, taking care not to move the cursor. Hold Shift+Alt and click somewhere far enough to right that you capture at least the longest line. Ctrl-C.

Paste the block at the very beginning of file A. You should now have each line prefixed with hashes indicating the word count. Example:

###### The day was very difficult today. 
######## I hope the next few days get better. 
##### Do you hope the same? 
######## I hope you hope the same as me!

Now use the menu, Edit -> Line Operations -> Sort Lines Lexicographically Ascending. Example:

##### Do you hope the same? 
###### The day was very difficult today. 
######## I hope the next few days get better. 
######## I hope you hope the same as me!

Finally, remove the leading hashes by replacing ^#* with nothing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.