#Looking for a way to replace numeric words with numbers

70 messages ยท Page 1 of 1 (latest)

vernal cargo
#

Hey there! I'm looking for a way to replace "Chapter one" with "Chapter 1"
The issue is that there a bunch of chapters (64 for the one im trying to edit) and I'm looking for a script that could automate this process.

Desired Result:
Chapter one --> Chapter 1
Chapter four --> Chapter 4
Chapter Twenty Three --> Chapter 23
etc.

Another issue is that the text file I'm trying to edit has normal numeric words that I wouldn't like to chanf
ex.) Tommy had fifty four apples.

I would like to only change the Chapter numbers.

I would greatly appreciate some help ๐Ÿ™

flint sleet
#

There is powershell humanizer module that might help you.

vernal cargo
#

I have no idea how to write the script. I was hoping somebody would be able to help me out

flint sleet
#

Oh well good luck. Free contract / consultant work isnโ€™t typically something we do here.

#

I will say paid work isnโ€™t something done either.

carmine hazel
#

Which version of PowerShell are you targeting?

vernal cargo
carmine hazel
#

You can use [regex]::Replace to invoke a callback to a function/script that then needs to translate the individual numeric terms:

$originalText = Get-Content .\path\to\file.txt -Raw
$modifiedText = [regex]::Replace('(?<=\bChapter\s+)\w+\b', [System.Text.RegularExpressions.MatchEvaluator]{
  param($m)
  
  switch ($m.Value) {
    'one' { return '1' }
    'two' { return '2' }
    'three' { return '3' }
    # ... continue with remaining terms
    default { return "[[ERROR - NO TERMS FOR '$($m.Value)' at offset $($m.Index)]]" }
  }
})

For a start, you can fill in all the terms up to nine=9, then you just need to figure out how to parse "twenty three" as well ๐Ÿ™‚

vernal cargo
#

Thank you. I'll try to mess around with it and see if it works : )

#

Would it be necessary to include the word "Chapter" so that it only changes the Chapter numbers and not any random word that has "one" or "two" etc?

carmine hazel
#

Already included, look at the regex pattern used:

(?<=\bChapter\s+)\w+\b
#

It'll only replace words immediately following Chapter

vernal cargo
#

Am I missing a step?
I opened powershell via windows explorer while i was within the folder and I also tried changing the name of the file but it still cant find it

carmine hazel
#

Did you read the code and the error message?

vernal cargo
#

It says it couldnt find the path but I opened powershell within the folder so im not sure why its not able to find it

carmine hazel
#

Is your actual file located at .\path\to\ relative to the directory?

#

If not: change the path to be the actual path to the file

vernal cargo
#
$modifiedText = [regex]::Replace($originalText, '(?<=\bChapter\s+)\w+\b', [System.Text.RegularExpressions.MatchEvaluator]{
  param($m)
  
  switch ($m.Value) {
    'one' { return '1' }
    'two' { return '2' }
    'three' { return '3' }
    # ... continue with remaining terms
    default { return "[[ERROR - NO TERMS FOR '$($m.Value)' at offset $($m.Index)]]" }
  }
})
#

Didnt get the first error this time so i presume it worked?

carmine hazel
vernal cargo
#

Oh it mustve ended up like that cause i opened powershell in the file directory bar

carmine hazel
#

If you're already in the folder, and the file is named, say myFile.txt, then the path is just .\myFile.txt

#

$originalText = Get-Content .\myFile.txt -Raw

vernal cargo
#

Does that mean it cant find the term
"Chapter two"

carmine hazel
#

No, it means I forgot to add the input argument ๐Ÿ™ƒ , do this (notice $originalText now passed to [regex]::Replace):

$modifiedText = [regex]::Replace($originalText, '(?<=\bChapter\s+)\w+\b', [System.Text.RegularExpressions.MatchEvaluator]{
  param($m)
  
  switch ($m.Value) {
    'one' { return '1' }
    'two' { return '2' }
    'three' { return '3' }
    # ... continue with remaining terms
    default { return "[[ERROR - NO TERMS FOR '$($m.Value)' at offset $($m.Index)]]" }
  }
})
vernal cargo
#

and i would need to place the $originalText = Get-Content .\file.txt -Raw line in there as well correct?

carmine hazel
#

You'd need to run that first, yes

#

For testing you might want to use shorter strings

vernal cargo
#

Im not sure if its doing anything

carmine hazel
#

Inspect the output stored in $modifiedText

vernal cargo
#

How would I do that?

carmine hazel
#

Just write $modifiedText in the prompt and hit enter

#

PowerShell will evaluate it and output it to the screen

vernal cargo
carmine hazel
#

Here's an example with a number that isn't being parsed yet

vernal cargo
#

So I would just need to modify this

#

Until i get to the desired number

#

Also is it case sensitive? Sometimes its called
Chapter one
Chapter Two
Chapter Thirty three
Chapter Fourty Five
etc

carmine hazel
#

\w+ describes a word consisting on either upper-case letters, lower-case letters, underscores, or numeric digits, so that'll be fine

#

The switch statement in PowerShell defaults to case-insentitive comparisons, so a case like 'one' { '1' } will be satisfied even if $m.Value is oNe or One

vernal cargo
#

Ahh I see

#

Is there a line to save the output?

carmine hazel
#

You can use Set-Content to write data back to disk

#

$modifiedText |Set-Content output.txt

vernal cargo
#

Would it be possible to combine all of that into one command?

#
$originalText = Get-Content .\file.txt -Raw
$modifiedText = [regex]::Replace($originalText, '(?<=\bChapter\s+)\w+\b', [System.Text.RegularExpressions.MatchEvaluator]{
  param($m)
  
  switch ($m.Value) {
    'one' { return '1' }
    'two' { return '2' }
    'three' { return '3' }
    # ... continue with remaining terms
    default { return "[[ERROR - NO TERMS FOR '$($m.Value)' at offset $($m.Index)]]" }
  }
})
$modifiedText |Set-Content output.txt
carmine hazel
#

Because you want to reuse it interactively, or because you need to execute it in a context where all you can supply is command line arguments?

#

For the latter: put your code in a script file and then run the script with powershell -File path\to\script.ps1, or use powershell -EncodedCommand <encoded version of your command here> to embed the script contents in the command line

vernal cargo
#

Oh theres no special reason I just wanted to see if I could do it all in one instead of running multiple commands

#

Also I just had chatgpt print out the rest of the numbers. Would this be fine?

$originalText = Get-Content .\file.txt -Raw
$modifiedText = [regex]::Replace($originalText, '(?<=\bChapter\s+)[\w\s]+?(?=\r?\n)', [System.Text.RegularExpressions.MatchEvaluator]{
  param($m)
  
  switch ($m.Value) {
    'one' { return '1' }
    'two' { return '2' }
    'three' { return '3' }
    'four' { return '4' }
    'five' { return '5' }
    'six' { return '6' }
    'seven' { return '7' }
    'eight' { return '8' }
    'nine' { return '9' }
    'ten' { return '10' }
    'eleven' { return '11' }
    'twelve' { return '12' }
    'thirteen' { return '13' }
    'fourteen' { return '14' }
    'fifteen' { return '15' }
    'sixteen' { return '16' }
    'seventeen' { return '17' }
    'eighteen' { return '18' }
    'nineteen' { return '19' }
    'twenty' { return '20' }
    'twenty one' { return '21' }
    'twenty two' { return '22' }
    'twenty three' { return '23' }
    'twenty four' { return '24' }
    'twenty five' { return '25' }
    # ... continue with remaining terms
    default { return "[[ERROR - NO TERMS FOR '$($m.Value)' at offset $($m.Index)]]" }
  }
})
$modifiedText
$modifiedText |Set-Content output.txt

carmine hazel
#

That's the least efficient way of doing it, but if you only have a maximum of 26 chapters then it won't matter much

vernal cargo
#

oh i had 64 i just didnt have space to paste all of it

#

but in the future, i might have hundreds

carmine hazel
#

Right, so you'll want to implement an algorithm that can parse twenty two and figure out the "twenty" translates to a preceding 2 and not 20 for example

#

As an aside: I would personally recommend getting into the habit of trying to write code that writes code yourself (as opposed to burning brain cells and oceans with ChatGPT), but that's subjective ๐Ÿ™‚

vernal cargo
#

Yea I would definitely prefer to customize things myself but I have no experience coding and it wouldnt really be practical to learn just for tasks like this

vernal cargo
carmine hazel
#

No, I'm saying that if you want a script that can scale to an infinite amount of chapters then you need to approach the problem differently than just translating "twenty three million seventeen thousand four hundred thirty nine" to 23017439 - because a 1-to-1 translation would require millions of lines of cases in your switch

vernal cargo
#

Ah I see. I think at most it'll go up to a couple hundred so that wouldnt really be an issue

carmine hazel
#

Again, I'd strongly recommend ditching GPT's and trying to write down such an algorithm on a piece of paper - thereby forcing yourself to think about the problem being solved = you'll become a better problem solver ๐Ÿ™‚

vernal cargo
#

Alright well I appreciate you taking the time out of your day to help me. Have a great rest of your day : )

#

In the original text file, its listed like this.

carmine hazel
#

So then you'll need to change the regex pattern to consume multiple words, eg. change '(?<=\bChapter\s+)\w+\b' to '(?<=\bChapter\s+)[\w\s]+?(?=>\r?\n)'

flint sleet
#

there is also ConvertTo-Words from install-psresource powershellhumanizer Welp, that is the wrong direction.

#

didn't consider the [regex] piece though.

flint sleet