Sign in to follow this  
Followers 0
augustspies

read file - convert words to numbers

13 posts in this topic

Hey guys,

I was wondering if it were possible to read a .rtf/.txt file

then search for spelled out numbers and convert them into digits.

example:

"your numbers are five one seven six nine again five one seven six nine"

want to search that string then convert to: 5169

if possible where should I start?

do I need to use a switch?

thanks

Share this post


Link to post
Share on other sites



StringInStr() is where you want to start()

After FileRead() that is!

thanks for the reply johnone

I've been playing around w/ it and I just can't seem to get it.

I ended up getting the words to actual digits, So i no longer need

to replace words to digits.

What i'm trying to do now is just pull the actual string of #'s from

the file.

Here's the issues i'm not sure how to address though:

- the string of #'s range from 4 - 5 digits in length how do set that up?

- how do i split the first set of digits from the 2nd set?

here's what I got so far:

$MyString = "there are 1 2 3 4 pens again 1 2 3 4"

$NumString=StringRegExpReplace($MyString,"\D","",0)
MsgBox(0,"Numbers in Sring",$NumString)
; consolewrite($numString)

thanks

Share this post


Link to post
Share on other sites

I'm a complete SRE buffoon, but I'm sure someone will help you with that.

lol no worries :huh2: i'm in the same boat

i've been staring at the sre page in the help file for the past day or so :/

hopefully someone is able to chime in.

thx JohnOne

Share this post


Link to post
Share on other sites

What i'm trying to do now is just pull the actual string of #'s from

the file.

Here's the issues i'm not sure how to address though:

- the string of #'s range from 4 - 5 digits in length how do set that up?

- how do i split the first set of digits from the 2nd set?

Try this:

#include <array.au3>
$MyString = "there are 1 2 3 4 pens again 1 2 3 4 5"
$NumString=StringRegExpReplace($MyString,"\s","",0)
$aNumString=StringRegExp($numString,"\d{4,5}",3)
_ArrayDisplay($aNumString)

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Well I wanted to write some code to help you out. It is rare that I even try to write example code, even rarer that it works.. Anyway, here is an example that works, just not correctly..

Local $MyString_Array[10] = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"], _
      $MyString = "there are 1 2 3 4 pens again 1 2 3 4 ", _
      $NewString, $MyNewString

For $a = 0 to 9
    If StringinStr($MyString, String($a)) Then
        $NewString = StringReplace($MyString, String($a), $MyString_Array[$a])
        $MyNewString &amp;= $NewString &amp; @LF
    EndIf
Next

ConsoleWrite($MyNewString &amp; @LF)

This is what it outputs

there are one 2 3 4 pens again one 2 3 4 
there are 1 two 3 4 pens again 1 two 3 4 
there are 1 2 three 4 pens again 1 2 three 4 
there are 1 2 3 four pens again 1 2 3 four

I'm going to work on this later, I have another idea to try, but maybe this will somehow help you, maybe somebody will be able to get this code to work before I do..<br><br>Edit: I figured it out and kicked myself.. Try this code:<br>

<br>Local $MyString_Array[10] = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"], _<br>&nbsp;&nbsp;&nbsp; &nbsp; $MyString = "there are 1 2 3 4 pens again 1 2 3 4 "<br><br>For $a = 0 to 9<br>&nbsp;&nbsp;&nbsp; If StringinStr($MyString, String($a)) Then $MyString = StringReplace($MyString, String($a), $MyString_Array[$a])<br>Next<br><br>ConsoleWrite($MyString &amp; @LF)<br>
<br> Edited by somdcomputerguy

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

:huh2:

Local $MyString_Array[10] = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"], _
      $MyString = "there are 1 2 3 4 pens again 1 2 3 4 "

For $a = 0 to 9
    $MyString = StringReplace($MyString, String($a), $MyString_Array[$a])
Next

ConsoleWrite($MyString &amp; @LF)
Edited by somdcomputerguy

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Hey guys,

I was wondering if it were possible to read a .rtf/.txt file

then search for spelled out numbers and convert them into digits.

example:

"your numbers are five one seven six nine again five one seven six nine"

want to search that string then convert to: 5169

if possible where should I start?

do I need to use a switch?

thanks

Try this.

#include <Array.au3>

Local $sNumbers = "zero|naught|nought|nil|zilch|zip|ought|aught,one,two,three,four,five,six,seven,eight,nine"
Local $sStrOrig = "your numbers are five one seven six nine again five one seven six nine"
Local $sStr = $sStrOrig

Local $aNum = StringSplit($sNumbers, ",", 2)

;Convert spelt out numbers to digits.
For $i = 0 To UBound($aNum) - 1
    $sStr = StringRegExpReplace($sStr, "\b(" & $aNum[$i] & ")\b", $i)
Next

; Spaces between digits removed
;$sStr = StringRegExpReplace(StringRegExpReplace($sStr, "(\d+?) +(\d)", "\1\2"), "(\d+?) (\d)", "\1\2")
$sStr = StringRegExpReplace($sStr, "(?<=\d) (?=\d)",  "")
ConsoleWrite($sStr & @CRLF)

$sStr = StringStripWS(StringRegExpReplace(StringStripWS($sStr, 8), "([^\d]+)", " "), 7) ; Remove all non-digits.
$sStr = StringReplace($sStr, "7", "") ; Remove 7

;Put all numbers found into an array.
Local $aNumOnly = StringSplit($sStr, " ", 2)
Local $aNumRet = _ArrayUnique($aNumOnly) ; Remove duplicate numbers.

;Convert array to string
Local $sRetString = $aNumRet[1] & " "
For $i = 2 To UBound($aNumRet) - 1
    $sRetString &= " " & $aNumRet[$i]
Next

MsgBox(0, "Result", '"' & $sStrOrig & '"' & @CRLF & @CRLF & _
        "That string is searched and converted to: " & $sRetString)

Edit: Replaced

$sStr = StringRegExpReplace(StringRegExpReplace($sStr, "(\d+?) +(\d)", "\1\2"), "(\d+?) (\d)", "\1\2")

with

$sStr = StringRegExpReplace($sStr, "(?<=\d) (?=\d)", "")

Edited by Malkey

Share this post


Link to post
Share on other sites

For the first version (spelled out digits):

Local $str = "your numbers are five one seven six nine again five one seven six nine"

Local $s = StringRegExpReplace($str, "(?iU)(?:\s*\w+\s+)*\b(zero|one|two|three|four|five|six|seven|eight|nine)\b\s+", "$1")
$s = StringLeft($s, StringLen($s) / 2)
$s = StringReplace($s, "zero", "0")
$s = StringReplace($s, "one", "1")
$s = StringReplace($s, "two", "2")
$s = StringReplace($s, "three", "3")
$s = StringReplace($s, "four", "4")
$s = StringReplace($s, "five", "5")
$s = StringReplace($s, "six", "6")
$s = StringReplace($s, "seven", "7")
$s = StringReplace($s, "eight", "8")
$s = StringReplace($s, "nine", "9")
Local $n = Number($s)
ConsoleWrite($n & @LF)

For the second version (digits as digits):

Local $str = "your numbers are 5 1 7 6 9 again 5 1 7 6 9"

Local $s = StringRegExpReplace($str, "[^\d]", "")
$s = StringLeft($s, StringLen($s) / 2)
Local $n = Number($s)
ConsoleWrite($n & @LF)

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

awesome, sorry late reply

thank you:

sahsanu

bruce

Malkey

jchd

after looking through your examples i was able to piece what i needed together!

appreciate your time

do any of you know how to run a script at random invtervals? I've been using scheduled task

but looking for a way to randomize the running times.

i thought of putting the script in a

while/wend statement w/ a random sleep at the end of the script however that would

have the script running in the background all the time.

Share this post


Link to post
Share on other sites

Lookup AdlibRegister in the help file; randomize delay at each run by re-registering the same function.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0