Macro array owerload ?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
BubikolRamios
Posts: 91
Joined: Sat Jan 04, 2014 1:28 pm

Macro array owerload ?

Post by BubikolRamios »

Attached odt & macro inside.

in macro

Code: Select all

  matrix = Array(_ 
               ...
              Array("wurzelnde","AAA "),_
               ....
Goes and finds all 'wurzelnde' and replaces them with 'AAA' and makes that 'AAA' BOLD.

Great, does not work in all cases , example text inside odt. One occurance of 'wurzelnde' does not get replaced.

Cure 1 which I don't see any logic and gets me nowhere: erase all Array elemnts above

Code: Select all

Array("wurzelnde","AAA "),_
Any thought ?
Attachments
forum.odt
(17.81 KiB) Downloaded 201 times
OPen office 4.1.5/ win 7
User avatar
Lupp
Volunteer
Posts: 3542
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Macro array owerload ?

Post by Lupp »

You surely won't hire me to search >300 lines of an Array assignment for a missing comma or something alike.
I therefor reduced the array and slightly regularized the code.

This done it worked as expected in AOO 4.1.5 and in LibO 6.0.4 as well.

Reworked example:
Attachments
a0094191searchReplaceArray_0.odt
(11.62 KiB) Downloaded 207 times
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
BubikolRamios
Posts: 91
Joined: Sat Jan 04, 2014 1:28 pm

Re: Macro array owerload ?

Post by BubikolRamios »

You surely won't hire me to search >300 lines of an Array assignment for a missing comma or something alike.
No. If there would be a missing coma there would be syntax error... Array is OK.


Now, if I use big array as it is and remove format code:

Code: Select all

    REM This is the attribute to find
    SrchAttributes(0).Name = "CharWeight"
    SrchAttributes(0).Value =com.sun.star.awt.FontWeight.NORMAL


    REM This is the attribute to replace it with
    ReplAttributes(0).Name = "CharWeight"
    ReplAttributes(0).Value =com.sun.star.awt.FontWeight.BOLD


    REM Set the attributes in the replace descriptor
    oReplace.SetSearchAttributes(SrchAttributes())
    oReplace.SetReplaceAttributes(ReplAttributes())

No 'wurzelnde' in replaced text ! So problem is somewhere within upper code or with what it does.
No need to say, original text is much bigger and other elements from array does not get replaced (randomly?).

i.e.

The logic is : replace 'wurzelnde' with AAA, make that bold so that with next loop searching for NORMAL, AAA would not be replaced again.
Looks like it thinks skipped 'wurzelnde' is already BOLD.

Of course there is also a strange thing with reducing array where it then works OK anyway.
Last edited by BubikolRamios on Thu Jul 05, 2018 1:14 am, edited 1 time in total.
OPen office 4.1.5/ win 7
User avatar
Lupp
Volunteer
Posts: 3542
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Macro array owerload ?

Post by Lupp »

Your original example also works for me as expected in AOO V4.1.5 and in LibO V 6.0.4 as well. All the due replacements are executed.

Did that example actually produce an error for you? If so:
Did you already consider a problem with your install of AOO or with the user profile? Did you test with a newly created user profile and with a portable version?

I cannot see any means to find a clue here.

===Editing===
Seizing the suggestions made by others I tried again and could not verify the above statement. I therefor striked it out.
Since the error in the second paragraph persists overwriting the "wurzelnde" with a copy of the first paragraph, formats included, but vanishes if the replacement for "Schlamm" is deleted from the array, and also if the serch for 'NORMAL' weight attribute is cancelled, I cannot but assume a very starnge bug. "Strange" the more as it is crawling on the heritage level (afflicting AOO V4.1.5 and LibO V6.0.4.2 as well).
If the search for attributes not is essential the omission of it will work . Till the next bug comes up?
Last edited by Lupp on Thu Jul 05, 2018 12:38 pm, edited 1 time in total.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
BubikolRamios
Posts: 91
Joined: Sat Jan 04, 2014 1:28 pm

Re: Macro array owerload ?

Post by BubikolRamios »

Your original example also works for me as expected in AOO V4.1.5 and in LibO V 6.0.4 as well.
will see if I can find anything about that ...
OPen office 4.1.5/ win 7
BubikolRamios
Posts: 91
Joined: Sat Jan 04, 2014 1:28 pm

Re: Macro array owerload ?

Post by BubikolRamios »

On newly installed portable, same thing.
AAA
2. kopenska rastlina oder im Blato wurzelnde Voda Rastlina ....................................................4
— Frei plavajoč , nicht im Boden AAA Voda Rastlina ......................................3
AAA
Just to check, in OP attachment, the red wurzelnde, after macro execution, does not exists for you, i.e. it is 'AAA' ?
If it is 'AAA', must be prob with java I assume.
OPen office 4.1.5/ win 7
BubikolRamios
Posts: 91
Joined: Sat Jan 04, 2014 1:28 pm

Re: Macro array owerload ?

Post by BubikolRamios »

Compressed odt attached. Array only with 'wurzelnde'. Could be foo realy.
Wherever in first line of text I put 'wurzelnde', gets replaced. On second line some 'wurzelnde' gets ignored . If putted at start of 2. line it gets replaced.

Looks like it thinks they are bold ...

Also if you change all text to BOLD and then to not BOLD. Macro will replace all OK.
Unfortunately that does not work on original document - tons of text, CTRL+A/klik on B button--> all BOLD/ Another click on B button --> no effect (stays BOLD), via menu format/character/....successfuly not BOLD.
Attachments
forum.odt
(10.32 KiB) Downloaded 213 times
OPen office 4.1.5/ win 7
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Macro array owerload ?

Post by RPG »

I think outcomment this line

Code: Select all

    'oReplace.SetSearchAttributes(SrchAttributes())
Maybe it is better to work with styles for the bold words. But I have not an idea for it. You can can define styles and use the name of the style in a macro.It seems style do not work in the replacedescriptor.

I think you need not to search for a Attribuut but you can set the atrribuut.

Romke
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
User avatar
Lupp
Volunteer
Posts: 3542
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Macro array owerload ?

Post by Lupp »

I did a lot of additional testing and it was annoying since there were no clear and reproducible results, but also interesting as it showed again to what extreme extent software everybody relies on is actually unreliable. (In my opinion this is even more true for commercial software.)

A general suggestion: Do not use direct formatting from the beginning.
A question based on a guess: Was the document originally created with an "alien" software (MS-Word, e.g.)?

A specialised suggestion - and a good and important one concerning enhancements and maintenance in such a use-cases as "user defined translation":
Never define huge arrays by direct assignment. Regarding their function they are intended to be used as tables, and only if kept in tables the data are maintainable and reusable without lots of problems. Using AOO or LibO and not introducing a database: Create an actual table in a spreadsheet document and read the data from there for whatever use you have to make of in any other document.
How to do so is demonstrated in the new attachmnts which are a pair of related files. For the simplified form I accessed the table from the text document it is necessary to have both the files in a common folder. Run "forum1.odt" then.

It seems you used the 'Bold' attribute to avoid a repeated replacement for an already replaced portion. Am I right?
Attachments
forum1.odt
(12.54 KiB) Downloaded 197 times
forumFRtable.ods
(57.29 KiB) Downloaded 229 times
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
BubikolRamios
Posts: 91
Joined: Sat Jan 04, 2014 1:28 pm

Re: Macro array owerload ?

Post by BubikolRamios »

Attached ods with some logical text repairs.
A question based on a guess: Was the document originally created with an "alien" software (MS-Word, e.g.)?
Copy paste from outer text editor, thought there is a problem vith Byte order mark which I know thta editor has, copied firts to notepad and then to writer.
No difference.
It seems you used the 'Bold' attribute to avoid a repeated replacement for an already replaced portion. Am I right?
Right.

Also order of elements in ods (before also in array), is important.

There are still some strange things .... sample text:

"123. Bl. nieren- oder herzförmig...........................................................56. Saxifragaceae"
Note that I underlined extra dot at the end.

the following is in ods, in that order
herzförmig/srčast
st./steblo

herzförmig translates to srčaSteblo


No other clue than, it does replace again on already replaced text which should not do.
(EDIT maybe it thinks there it is in REGEX mode ?! )

step 1.herzförmig becomes srčast (that is OK)
step2. because there is '.' already at the end of new 'srčast', there is now 'st.' at the end ---> 'srčast.'
step3. then it does replace again (should not because st part should be bold): st. becomes steblo

hence:
herzförmig --> srčast +'.' --> srčaSteblo
OK................OK.................BAD.....
Attachments
forumFRtable.ods
(54.5 KiB) Downloaded 186 times
OPen office 4.1.5/ win 7
User avatar
Lupp
Volunteer
Posts: 3542
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Macro array owerload ?

Post by Lupp »

I was aware of the fact that the order of listed replacements will be relevant as soon as already replaced text, most likely in combination with surrounding text may be found as a target for a second replacement. To avoid replacements of second (or even higher) order precautions are to take, and you decided to use a text character attribute. I also tried it with FontUnderline 0 (NONE) to 1 (SINGLE) and got the same problems as you did with FontWeight 100 (NORMAL) to 150 (BOLD). I cannot but suspect a ticklish bug in the search for attributes. It may live long since everybody will shy back from even reporting it. I wouldn't know how to do it.

"RPG" already mentioned that searching for named styles might be more reliable. However, we cannot search for named character styles. The mode only works with paragraph styles (and then no search strings).

The only way (except waiting for a bugfix or the "crazy" idea mentioned in the postscript) I can see is to mark a already made replacement with a pair of otherwise not occurring characters used as parentheses. This will, on the other hand, require to enable RegEx in the search to test for the absence of those special characters using LookBehind/LookAhead assertions. In return this will require to "esacpe" in the SearchString the common special characters of RegEx if they occur in the literal strings to search for. Your examples contain the point e.g. in this role. The replace stings must not contain characters or sequences (& $1 \n \t e.g.) used there for special purposes.

You are patient and persevering? Excellent!
Good luck!

PS
Another idea, though sounding crazy: Insert a 'ZERO WIDTH SPACE' (U+200B) between any two consecutive characters of the replacing literal before you set it as the value of the ReplaceString. In a second run you may delete them again after the translation. This way you won't need RegEx.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Lupp
Volunteer
Posts: 3542
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Macro array owerload ?

Post by Lupp »

As I came to the conclusion that the "crazy" way was rather smart, I implemented it for your example.

It is contained (in a raw version) in the new attachment.
If you don't want to remove the interleaved U+200B characters at the end of the "translation", you have to remove a few lines. Of course you can apply attributes to the replacing strimgs in addition as you originally did. (The respective lines are removed from the code, however, in the current state.)

The trick does not work for "words" of less than two characters.

=== Editing ===
The table to use with the new example is the same as attached to a previous post: download/file.php?id=35366 .
It must be in the same folder as the new example .odt.
Attachments
forum2.odt
(11.54 KiB) Downloaded 195 times
Last edited by Lupp on Thu Jul 12, 2018 3:02 pm, edited 1 time in total.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
BubikolRamios
Posts: 91
Joined: Sat Jan 04, 2014 1:28 pm

Re: Macro array owerload ?

Post by BubikolRamios »

OPen office 4.1.5/ win 7
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Macro array owerload ?

Post by RPG »

I do not know if the method of Lupp is working. But maybe change the method of working. Look for each word in your text-document if it must be changed. I knew it is not fast but it has to be done once.
When your text-document is real text-document it can maybe done more easy in an other language. If it is not a simple text-document it will be more difficult. I think the next code will work for a real simple text-document in LibreOffice. You have to set the complete table but test it first on some words.

The table in Wordlist is a kind of dictionary or collection.

Romke

Code: Select all

sub LookForAllWords
dim oDoc
oDoc=thiscomponent
dim oText
dim oTextCursor
dim sInvoeg
dim LengteOud
dim lengteNieuw
dim MoveCursor
dim oWordlist as object

oWordlist = com.sun.star.container.EnumerableMap.create("string", "string")
oWordlist.put("Klaar","Appel gebeuren flap")
oWordlist.put("wurzelnde","aaa")
'print oWordlist.containsKey("wurzelnde")
'print oWordlist.get("Klaar")
dim x


oText=thiscomponent.getText
oTextCursor=oText.createTextCursorByRange(oText.start)

oTextCursor.gotoEndOfWord(true)



for x=0 to 50


if oWordlist.containsKey(trim(oTextCursor.string)) 	then
	'Vervang het woord en ga verder
		oTextCursor.gotoStartOfWord(false)
		oTextCursor.gotoEndOfWord(true)
		'print "M" & oTextCursor.string & "M"
		LengteOud=len(oTextCursor.string) ' Bewaar de lengte van de string
		sInvoeg = oWordlist.get(oTextCursor.string)
		 oTextCursor.setstring(sInvoeg)
		 oTextCursor.CharWeight=200 
		 MoveCursor=len(sInvoeg) '-LengteOud
		 select case sgn(MoveCursor)
			 case -1 :	oTextCursor.goleft(MoveCursor,false)
			 case 0 :	beep
			 case 1 :	oTextCursor.goRight(MoveCursor,false)
			 			oTextCursor.gotoendofword(false)
			 			'print len(oTextCursor.string)
		 end select
	end if

do 
oTextCursor.goright(1,false)
'print "|" & oTextCursor.string & "|"

loop until oTextCursor.isStartOfWord
'print "MM" & oTextCursor.string & "MM"
oTextCursor.gotoEndOfWord(true)
print "MMM" & oTextCursor.string & "MMM"

if oTextCursor.string="GeheelStop" then beep : exit for
	
next
print "alles gedaan"
end sub
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
User avatar
Lupp
Volunteer
Posts: 3542
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Macro array owerload ?

Post by Lupp »

RPG wrote:I do not know if the method of Lupp is working.
It is . Simply try. It works in the sense that it actually replaces found occurrences of strings listed left in the lookup table (spreadsheet) and replaces them with the respective string at the right - and that it avoids the inclusion of part of the text already created by a replacement during a subsequent pass within the same run. If there occur ZWS (U+200B) characters in the text or in any of the original strings in the replacement tables, a different character, assured to not occur there, must replace it in its role.

The selectivity may be enhanced by using RegEx and therein word boundaries and assertions.

No use, IMO, in trying to also regard upper case / lower case and such things: As with any translation, the result must be proofread. Spreadsheets aren't KI, though someone using them that way will as splendidly fail as "real KI" applications will - hopefully. (Play a few minutes with a translation software of the x-th generation, and hope is here again.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Macro array owerload ?

Post by RPG »

Lupp wrote: RPG wrote:
I do not know if the method of Lupp is working.

It is . Simply try.
Lupp I do believe it works.

Romke
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
Post Reply