This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Text Format Conversion

Requests for complete scripts or modifications/fixes for scripts you didn't write. Response not guaranteed, and no thread bumping!
Post Reply
User avatar
mondino
Voice
Posts: 12
Joined: Mon Jul 09, 2018 8:11 am
Location: Lebanon
Contact:

Text Format Conversion

Post by mondino »

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.
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

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: Select all

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
.
User avatar
mondino
Voice
Posts: 12
Joined: Mon Jul 09, 2018 8:11 am
Location: Lebanon
Contact:

Post by mondino »

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,
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

formatfile.tcl v0.1 by SpiKe^^

Post by SpiKe^^ »

Try this updated script...

Code: Select all

### 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
.
User avatar
mondino
Voice
Posts: 12
Joined: Mon Jul 09, 2018 8:11 am
Location: Lebanon
Contact:

Post by mondino »

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,
User avatar
mondino
Voice
Posts: 12
Joined: Mon Jul 09, 2018 8:11 am
Location: Lebanon
Contact:

Post by mondino »

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.
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

formatfile2.tcl v0.3 by SpiKe^^

Post by SpiKe^^ »

Code: Select all

### 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
.
User avatar
mondino
Voice
Posts: 12
Joined: Mon Jul 09, 2018 8:11 am
Location: Lebanon
Contact:

Post by mondino »

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,
Post Reply