| View previous topic :: View next topic |
| Author |
Message |
mondino Voice
Joined: 09 Jul 2018 Posts: 9 Location: Lebanon
|
Posted: Tue Nov 30, 2021 3:07 am Post subject: Text Format Conversion |
|
|
Hello geeks.
I need a script to change the text format from:
Question*Answer to Answer|Question?
Please note that, some lines have multiple "*", I'd want only the 1st word after "*" to be considered as "Answer" and be flipped to sit in the beginning of the line.
Briefly, I have a list of questions and answers that are written in the following format: Question*Answer and sometimes Question*Answer*Answer when a question requires more than an answer... that I need to change to this format: Answer|Question.
Thank you so much. |
|
| Back to top |
|
 |
SpiKe^^ Owner

Joined: 12 May 2006 Posts: 792 Location: Tennessee, USA
|
Posted: Tue Nov 30, 2021 11:50 am Post subject: |
|
|
this script replies to the message command .formatfile from a user with the global userfile flag(s) m, n, and/or o
rename the original file to old.file
put that file in the eggdrop scripts/ dir
message the bot with .formatfile
the new file will be at scripts/new.file
| Code: |
bind msg mno .formatfile format:file
proc format:file {nick uhost hand arg} {
set oldfilename "scripts/old.file"
set newfilename "scripts/new.file"
set openold [open $oldfilename r]
set opennew [open $newfilename w]
while {![eof $openold]} {
lassign [split [gets $openold] "*"] ques answ
if {$ques eq "" || $answ eq ""} { continue }
puts $opennew "${answ}|$ques"
}
close $openold
close $opennew
}
|
_________________ SpiKe^^
Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
. |
|
| Back to top |
|
 |
mondino Voice
Joined: 09 Jul 2018 Posts: 9 Location: Lebanon
|
Posted: Wed Dec 01, 2021 1:11 am Post subject: |
|
|
Thank you Spike^^ for the script. It works well.
I guess, we just need this script to be upgraded to add "?" to the end of each question line.
Note that, this tcl script doesn't work for "KAOS questions" type where the answer requires multiple words. It chose the first word after "*" as an answer which is not the case for KAOS type questions.
We might need to have a new tcl that would work for KAOS type questions in the future.
Thank you again, and I hope we work out to upgrade this TCL script to do the addition of "?" to the end of each question line.
Regards, |
|
| Back to top |
|
 |
SpiKe^^ Owner

Joined: 12 May 2006 Posts: 792 Location: Tennessee, USA
|
Posted: Wed Dec 01, 2021 2:19 pm Post subject: formatfile.tcl v0.1 by SpiKe^^ |
|
|
Try this updated script... | Code: |
### Trivia Question File Format Converter ***
# formatfile.tcl v0.1 by SpiKe^^ (1Dec2021) *
# This script will convert trivia files from Q*A to A|Q #
# Also convert Q*A1*A2*A3 to A1|Q (uses first answer) #
# Also adds a ? to the end of questions, if has none #
# Also removes any kaos and scramble questions #
# load this script to the eggdrop bot.
# rename the source file to old.file & put in scripts/ dir.
# private message the bot with .formatfile
bind msg mno .formatfile format:file
proc format:file {nick uhost hand arg} {
set oldfilename "scripts/old.file"
set newfilename "scripts/new.file"
if {![file exists $oldfilename]} {
putserv "PRIVMSG $nick :Error: File not found: $oldfilename"
return 0
}
set openold [open $oldfilename r]
set opennew [open $newfilename w]
set cnt 0
while {![eof $openold]} {
lassign [split [gets $openold] "*"] ques answ
set ques [string trim $ques]
set answ [string trim $answ]
if {$ques eq "" || $answ eq ""} { continue }
if {[string match -nocase "kaos:*" $ques]} { continue }
if {[regexp -nocase {^(scramble|uword)$} $ques]} { continue }
if {![string match {*[.?]} $ques]} { append ques "?" }
incr cnt
puts $opennew "${answ}|$ques"
}
close $openold
close $opennew
if {$cnt == 0} { file delete $newfilename
putserv "PRIVMSG $nick :Error: No valid questions found in $oldfilename"
} else {
putserv "PRIVMSG $nick :Saved $cnt reformatted questions to $newfilename"
}
return 0
}
putlog "formatfile.tcl ver 0.1 by SpiKe^^ Loaded."
|
_________________ SpiKe^^
Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
. |
|
| Back to top |
|
 |
mondino Voice
Joined: 09 Jul 2018 Posts: 9 Location: Lebanon
|
Posted: Thu Dec 02, 2021 2:12 am Post subject: |
|
|
Thank you again Spike^^
You have done a great job. The updated TCL script is working perfectly. It just added a "?" to the end of each question line. It did the job.
Thank you for your effort and quick reply.
Regards, |
|
| Back to top |
|
 |
mondino Voice
Joined: 09 Jul 2018 Posts: 9 Location: Lebanon
|
Posted: Mon Dec 06, 2021 12:18 am Post subject: |
|
|
Hi again.
That's getting challenging.
I need to convert this example:
1. Question: What year did Disneyland open?
Answer: 1955.
Into this:
ANSWER|Category-Name: Question?
Sometimes, the question is without numbering.
Thank you in advance. |
|
| Back to top |
|
 |
SpiKe^^ Owner

Joined: 12 May 2006 Posts: 792 Location: Tennessee, USA
|
Posted: Tue Dec 07, 2021 12:31 pm Post subject: formatfile2.tcl v0.3 by SpiKe^^ |
|
|
| Code: |
### Trivia Question File Format Converter 2 ###
# formatfile2.tcl v0.3 by SpiKe^^ (7Dec2021) #
### Public command: .format2 :will convert trivia files from...
#
# 1. Question: What year did Disneyland open?
# Answer: 1955.
#
# ...to something more like:
# 1955|What year did Disneyland open?
### You can also specify a Question Category for the current file.
# Public command: .format2 Disney Trivia :will convert to...
#
# 1955|Disney Trivia: What year did Disneyland open?
# Script will also add a ? to the end of questions, if has none #
### load this script to the eggdrop bot.
### rename the source file to old.file & put in scripts/ dir.
### private message the bot with .format2 |or| .format2 Category-Name
bind msg mno .format2 format:file2
proc format:file2 {nick uhost hand arg} {
set oldfilename "scripts/old.file"
set newfilename "scripts/new.file"
if {![file exists $oldfilename]} {
putserv "PRIVMSG $nick :Error: File not found: $oldfilename"
return 0
}
set arg [string trim $arg]
if {$arg ne ""} {
if {![string match "*:" $arg]} { append arg ":" }
append arg " "
}
set openold [open $oldfilename r]
set opennew [open $newfilename w]
set cnt 0
set ques "" ; set answ ""
while {![eof $openold]} {
set line [string trim [gets $openold]]
if {$line eq ""} { continue }
if {$ques eq ""} {
if {![string match -nocase "answer: *" $line]} { set ques $line }
continue
}
if {[lsearch -nocase [lrange [split $line] 0 1] "question:"] > -1} {
set ques $line ; continue
}
set answ $line
set x [lsearch -nocase [lrange [split $ques] 0 1] "question:"]
if {$x > -1} {
regexp -nocase {question:(.*?)$} $ques - ques
set ques [string trim $ques]
}
if {[string tolower [lindex [split $answ] 0]] eq "answer:"} {
regexp -nocase {answer:(.*?)$} $answ - answ
set answ [string trim $answ ". "]
}
if {$ques ne "" && $answ ne ""} { incr cnt
if {![string match {*[.?]} $ques]} { append ques "?" }
puts $opennew "${answ}|${arg}$ques"
}
set ques "" ; set answ ""
}
close $openold
close $opennew
if {$cnt == 0} { file delete $newfilename
putserv "PRIVMSG $nick :Error: No valid questions found in $oldfilename"
} else {
putserv "PRIVMSG $nick :Saved $cnt reformatted questions to $newfilename"
}
return 0
}
putlog "formatfile2.tcl ver 0.3 by SpiKe^^ Loaded."
|
_________________ SpiKe^^
Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
. |
|
| Back to top |
|
 |
mondino Voice
Joined: 09 Jul 2018 Posts: 9 Location: Lebanon
|
Posted: Wed Dec 08, 2021 12:24 am Post subject: |
|
|
Thank you for your support, Spike^^
The script is working perfectly. It just had some Chars error while converting, which is easy to fix.
Thank you again.
Regards, |
|
| Back to top |
|
 |
|