-1

私は次のことに苦労しています。

特定の形式でmoodle(オンライン質問サイト)にインポートする必要がある質問と回答を含むWordファイルがあります。すべてが黒で、正しい答えを受け入れます。これらは緑です。開始形式は次のとおりです。

1. Question example

a. Wrong

b. Wrong

C. Wrong

D. Right

出力は次のようになります。

:Question example

:Question example

{

~ Wrong

~ Wrong

~ Wrong

= Right

}

Word でファイルを開き、赤い段落記号をすべて * に置き換えます (グループで置き換えることはできません)。その後、.docx ファイルをテキストにエクスポートします。Linux コンピューターで開き、次の正規表現をスローします。

sed -i -e 's/^\r/\n/g' tmp #OS X white line replacement                    
sed -i -e 's/\r//g' tmp #remove white lines                           
sed -i -e 's:^[a-z]\.:~:' tmp #Replace Leading question letters with tilde                                                                                               
sed -i -e 's/\(^[0-9]*\.\ \)\(.*\)/}\n::\2\n::\2\n{/' tmp #regenerate tittle                    
sed -i -n '${p;q};N;/\n\*/{s/"\?\n//p;b};P;D' tmp #next line starts with * append to front of current                                                              
sed -i -e 's:^~\(.*\)\(\*.*\)$:=\1:' tmp #move * from back to = to front
sed -i -e 's:^\*:=:' tmp #replace any remaining * with =        
sed '/^$/d' tmp #delete any remaining white lines 

これは素晴らしいものではありませんが、うまく機能します。質問は手作りで、多くのエラーがあるため、これを手動で処理する必要があります。難しいのは、複数の正解がある場合です。出力は次のようになります。

:Question example

:Question example

{

~%-100% Wrong

~%-100% Wrong

~%50% Right

~%50% Right

}

理想的には、{ の間の = の数をカウントし、それらを ~%50% に置き換える sed または perl 正規表現があります。そして、すべての ~ は %-100% で歌います。このコードは、すべての正解が ~%33% になる 3 つの正解に対しても使用できます。

これは実行可能ですか?1000 を超える質問がありますが、これを自動化すると確実に役立ちます。sed による複数行の置換は 2 行ではややこしいので、4 行以上は perl が必要になると思いますか? 私はPerlの経験がありません。

誰かがこれで私を助けてくれますか? 私の悪い英語を許してください。私は非ネイティブ スピーカーです。

4

4 に答える 4

1
my $file = do { local $/; <> };
my @questions = split /(?<=.)(?=[0-9]+\.)/s, $file;
for (@questions) {
   my @lines = split /^/m;

   my $title = shift(@lines);
   $title =~ s/^\S+\s*/:/;

   my $num_right;
   my $num_wrong;
   for (@lines) {
      if    (/Right/) { ++$num_right; }
      elsif (/Wrong/) { ++$num_wrong; }
   }

   my $num_answers = $num_right + $num_wrong;

   my $right_pct = sprintf('%.0f', $num_right/$num_answers*100);
   my $right_prefix = $num_right == 1 ? "=" : "~%$right_pct%";
   my $wrong_prefix = $num_right == 1 ? "~" : "~%-100%";

   for (@lines) {
      if    (/Right/) { s/^\S+/$right_prefix/; }
      elsif (/Wrong/) { s/^\S+/$wrong_prefix/; }
   }

   print(
      $title,
      "\n",
      $title,
      "\n{\n",
      @lines,
      "\n}\n",
   );
}

/Right/とを適切なものに置き換え/Wrong/ます。

于 2012-04-14T23:03:02.620 に答える
1

これはあなたのために働くかもしれません:

cat <<\! >file.sed
> # On encountering a digit in the first character position
> /^[0-9]/{
>   # Create a label to cater for last line processing
>   :end
>   # Swap to hold space
>   x
>   # Check hold space for contents.
>   # If none delete it and begin a new cycle
>   # This is to cater for the first question line
>   /./!d
>   # Remove any carriage returns
>   s/\r//g
>   # Remove any blank lines
>   s/\n\n*/\n/g
>   # Double the question line, replacing the question number by a ':'
>   # Also append a { followed by a newline
>   s/^[0-9]*\.\([^\n]*\n\)/:\1:\1{\n/
>   # Coalesce lines beginning with a * and remove optional preceeding "
>   s/"\?\n\*/*/g
>   # Replace the wrong answers a,b,c...  with ~%-100%
>   s/\n[a-zA-z]*\. \(Wrong\)/\n~%-100% \1/g
>   # Replace the right answers a,B,c... with ~%100%
>   s/\n[a-zA-Z]*\. \(Right\)/\n~%100% \1/g
>   # Assuming no more than 4 answers:
>   # Replace 4 correct answers prefix with ~%25%
>   s/\(~%100%\)\(.*\)\1\(.*\)\1\(.*\)\1/~%25%\2~%25%\3~%25%\4~%25%/
>   # Replace 3 correct answers prefix with ~%33%
>   s/\(~%100%\)\(.*\)\1\(.*\)\1/~%33%\2~%33%\3~%33%/
>   # Replace 2 correct answers prefix with ~%50%
>   s/\(~%100%\)\(.*\)\1/~%50%\2~%50%/
>   # Append a newline and a }
>   s/$/\n}/
>   # Break and so print newly formatted string
>   b
>   }
> # Append pattern space to hold space
> H
> # On last line jump to end label
> $b end
> # Delete all lines from pattern space
> d
> !

次に実行します。

sed -f file.sed file
于 2012-04-15T08:09:48.330 に答える
1

以下のプログラムは、何が必要かについての私の最善の推測に従って動作します。すべての情報を配列に読み取ってからフォーマットすることで機能します。

現状では、データはソースに組み込まれ、DATAファイルハンドルから読み取られます。ループを に変更するwhile (<>) { ... }と、コマンド ラインでデータ ファイルを指定できるようになります。

私の推測が間違っている場合は、訂正してください。

use strict;
use warnings;

my @questions;

while (<DATA>) {
  next unless /\S/;
  s/\s+$//;
  if (/^\d+\.\s*(.+)/) {
    push @questions, [$1];
  }
  elsif (/^[A-Za-z]\.\s*(.+)/i) {
    push @{$questions[-1]}, $1;
  }
}

for my $question (@questions) {

  my ($text, @answers) = @$question;

  print "::$text\n" for 1, 2;

  my $correct = grep /right/i, @answers;
  my $percent = int(100/$correct);

  print "{\n";

  if ($correct == 1) {
    printf "%s %s\n", /right/i ? '=' : '~', $_ for @answers;
  }
  else {
    my $percent = int(100/$correct);
    printf "~%%%d%%~ %s\n", /right/i ? $percent : -100, $_ for @answers;
  }

  print "}\n";
}

__DATA__
1. Question one

a. Wrong

b. Wrong

c. Right

d. Wrong

2. Question two

a. Right

b. Wrong

c. Right

d. Wrong

3. Question three

a. Right

b. Right

c. Wrong

d. Right

出力

::Question one
::Question one
{
~ Wrong
~ Wrong
= Right
~ Wrong
}
::Question two
::Question two
{
~%50%~ Right
~%-100%~ Wrong
~%50%~ Right
~%-100%~ Wrong
}
::Question three
::Question three
{
~%33%~ Right
~%33%~ Right
~%-100%~ Wrong
~%33%~ Right
}
于 2012-04-14T23:45:35.827 に答える
0

あなたの例はこのドキュメントと一致しません: http://docs.moodle.org/22/en/GIFT。質問のタイトルと質問は、1 つのコロンではなく 2 つのコロンで区切られます。

//Comment line 
::Question title 
:: Question {
=A correct answer
~Wrong answer1
#A response to wrong answer1
~Wrong answer2
#A response to wrong answer2
~Wrong answer3
#A response to wrong answer3
~Wrong answer4
#A response to wrong answer4
}

一部の人々は、実際の仕様を見つけるのではなく、あなたの例に基づいて素朴に答えました。

あなたのフォーマットではどれが正解か分からないため、あなたの質問に答えることができません。つまり、次のようになります。

1. Question

a. Is this right?

b. Or this?

c. Or this?

これらは元の Word 文書の色を使用して識別され、情報を保持するためにそれを置き換えているとのことです。ただし、この例は示していません。おっとっと ...

于 2012-04-16T06:02:16.617 に答える