blqk Posted June 14, 2015 Share Posted June 14, 2015 (edited) Hello,I need a little help.I have a txt file "test.txt" with words: Cat, Dog, Lion. etc.(one word per line). I have txt file "input.txt" with free text inside. I need a way to search "input.txt" for every word from "test.txt" and if it find it to replace it and write only the new word in a new file - "output.txt". Every word will have different replacement. For example if there is a Cat in the text it will be replaced with Cats, and if there is a Dog it will be replaced with Dogs. I need this to be set on shortkey so when pressed just to read the files and do the job.I think i can use StringReplace if it was just a single string but i am not sure how to do it for multiples words and multiple replacements I need to specify the replacement for every word, so probably a excel file would be better, with two colums. The first will include the text that needs to be searched and in the second there will be the replacement. Something like bellow.column1 column2Cat CatsDog DogsLion Lions Thanks in advance Edited June 14, 2015 by blqk Link to comment Share on other sites More sharing options...
mikell Posted June 14, 2015 Share Posted June 14, 2015 I'd use a csv file with lines like this :Cat,CatsDog,Dogsetcthen read it to a 2D array using _FileReadToArrayand use this for replacementsFor $i = 1 to $array[0][0] $newtext = StringReplace($oldtext, $array[$i][0], $array[$i][1]) Next Link to comment Share on other sites More sharing options...
jguinch Posted June 14, 2015 Share Posted June 14, 2015 mikell : it seems to be a case where a regex could be useful using \b because Dog => Dogs (OK), but Dogs >= Dogss Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
iamtheky Posted June 15, 2015 Share Posted June 15, 2015 (edited) Im sure there was more to pluralizing them, or simply adding an extension to found words. But just in case you can read your file in as a pipe delimited string and alter matching words:$sFile = "cat|dog|horse" _Plural("The cat played") _Plural("The dog played") _Plural("The horse played") _Plural("The cat and dog and horse played") Func _Plural($string) msgbox(0 , '' , StringRegExpReplace($string , "(" & $sFile & ")" , "$1s")) EndFunc mikell and JG should stomp a mudhole in this example in 3, 2, ... Edited June 15, 2015 by boththose ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
blqk Posted June 15, 2015 Author Share Posted June 15, 2015 I'd use a csv file with lines like this :Cat,CatsDog,Dogsetcthen read it to a 2D array using _FileReadToArrayand use this for replacementsFor $i = 1 to $array[0][0] $newtext = StringReplace($oldtext, $array[$i][0], $array[$i][1]) Next The problem here is that $newtext will be the same $oldtext only with the replaced words. I dont need the whole old text and i need only the replaced words. The $oldtext is always random and do not follow any pattern, so i need a way to exctract only the matching word.Example:$oldtext??????????The Cat eat the mouse ......?????????????$newtextDog Link to comment Share on other sites More sharing options...
blqk Posted June 15, 2015 Author Share Posted June 15, 2015 Im sure there was more to pluralizing them, or simply adding an extension to found words. But just in case you can read your file in as a pipe delimited string and alter matching words:$sFile = "cat|dog|horse" _Plural("The cat played") _Plural("The dog played") _Plural("The horse played") _Plural("The cat and dog and horse played") Func _Plural($string) msgbox(0 , '' , StringRegExpReplace($string , "(" & $sFile & ")" , "$1s")) EndFunc mikell and JG should stomp a mudhole in this example in 3, 2, ...I am not sure why but it looks like this is not working as expected. I removed cat and dog from $sFile but the result is the same. All four messages are displayed even the words cat and dog are not there.$sFile = "horse" _Plural("The cat played") _Plural("The dog played") _Plural("The horse played") _Plural("The cat and dog and horse played") Func _Plural($string) msgbox(0 , '' , StringRegExpReplace($string , "(" & $sFile & ")" , "$1s")) EndFunc Link to comment Share on other sites More sharing options...
jguinch Posted June 15, 2015 Share Posted June 15, 2015 You can use a CSV file to store the list of words , use_FileReadToArray with ";" as separator, and StringRegExp to find the wordCat;Cats Dog;Dogs Lion;Lions#Include <File.au3> Local $aWords _FileReadToArray("words.txt", $aWords, 1, ";") _Findwords("The cat and dog and horse played") Func _Findwords($sText) For $i = 1 To $aWords[0][0] If StringRegExp($sText, "(?i)\b\Q" & $aWords[$i][0] & "\E\b") Then MsgBox(0, "", $aWords[$i][1] & @CRLF) Next EndFunc Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
mikell Posted June 15, 2015 Share Posted June 15, 2015 blqk,Your code should be done like this$sFile = "horse" _Plural("The cat played") _Plural("The dog played") _Plural("The horse played") _Plural("The cat and dog and horse played") Func _Plural($string) $res = StringRegExpReplace($string , ".*(" & $sFile & ").*" , "$1s") If @extended Then msgbox(0 , '' , $string & @crlf & $res) EndFuncBut as it can't be used to extract several words in one shot jguinch's code is better#Include <File.au3> Local $aWords, $sOutput = "" _FileReadToArray("words.txt", $aWords, 1, ";") $newtext = _Findwords("The cat and dog and horse played") Msgbox(0,"", $newtext) ; or write it to a file Func _Findwords($sText) For $i = 1 To $aWords[0][0] If StringRegExp($sText, "(?i)\b\Q" & $aWords[$i][0] & "\E\b") Then $sOutput &= $aWords[$i][1] & @CRLF Next Return $sOutput EndFunc Link to comment Share on other sites More sharing options...
blqk Posted June 15, 2015 Author Share Posted June 15, 2015 Awasome, thanks a lot guys, i think that was what i needed. One small problem - After running the script, the first time i execute the shortkey, the mssgbox displays what i needed (Dogs), however pressing the shortkey for the second time it displays two rows with "Dogs", pressing it again it display it 3 times and so on. If i exit the script and run it again it starts from the beggining and repeats the proccess over again. So it looks the var $sOutput is not clearing the value after the func exits. I need every time when I press the shortkey to display only this search and the seocnd time i press it to display new search. Probably its something in the arrays that messes it up, but i am very poor with them expandcollapse popup#include <MsgBoxConstants.au3> #include <File.au3> #include <Array.au3> Global $file = "..\file.txt", Global $hFile = FileOpen($file) Global $aWords, $sOutput = "", $fileread Opt("WinWaitDelay", 0) AutoItSetOption("WinDetectHiddenText", 1) While 1 HotKeySet ( "!q" ,"_tool" ) Wend Func _tool() Local $sText = WinGetText ( "Tab 1", "") FileOpen("..\file.txt", 2) FileWrite("..\file.txt",$sText) FileClose("..\file.txt") _FileReadToArray("..\words.txt", $aWords, 1, ";") $fileread = FileRead("..\file.txt") $newtext = _Findwords($fileread) Msgbox(0,"", $newtext) ; or write it to a file FileClose($fileread) EndFunc Func _Findwords($sText) For $i = 1 To $aWords[0][0] If StringRegExp($sText, "(?i)\b\Q" & $aWords[$i][0] & "\E\b") Then $sOutput &= $aWords[$i][1] & @CRLF Next Return $sOutput EndFunc Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now