ローマ数字を使ってフレーズ番号を付ける

ローマ数字を使ってフレーズ番号を付ける

次のように、ローマ数字を使用して市の凧を順番に番号付けできます。

       
       Injurious love, why still to mar accord
       Between desires has been thy favourite feat?
       Why does it please thee so, perfidious lord,
       Two hearts should with a different measure beat?
       Thou wilt not let me take the certain ford,
       Dragging me where the stream is deep and fleet.
       Her I abandon who my love desires,
       While she who hates, respect and love inspires.
       
       Thou to Rinaldo show'st the damsel fair,
       While he seems hideous to that gentle dame;
       And he, who when the lady's pride and care,
       Paid back with deepest hate her amorous flame,
       Now pines, himself, the victim of despair,
       Scorned in his turn, and his reward the same.
       By the changed damsel in such sort abhorred,
       She would choose death before that hated lord.
       
       He to the Pagan cries: "Forego thy theft,
       And down, false felon, from that pilfer'd steed;
       I am not wont to let my own be reft.
       And he who seeks it dearly pays the deed.
       More -- I shall take from thee yon lovely weft;
       To leave thee such a prize were foul misdeed;
       And horse and maid, whose worth outstrips belief,
       Were ill, methinks, relinquished to a thief."
       
       "Thou liest," the haughty Saracen retorts,
       As proud, and burning with as fierce a flame,
       "A thief thyself, if Fame the truth reports:
       But let good deeds decide our dubious claim,
       With whom the steed or damsel fair assorts:
       Best proved by valiant deeds: though, for the dame,
       That nothing is so precious, I with thee
       (Search the wide world throughout) may well agree."
        

次のように入力します。ローマ大文字はスタンザの最後のスタンザと整列していますか?

       Injurious love, why still to mar accord
       Between desires has been thy favourite feat?
       Why does it please thee so, perfidious lord,
       Two hearts should with a different measure beat?
       Thou wilt not let me take the certain ford,
       Dragging me where the stream is deep and fleet.
       Her I abandon who my love desires,
I      While she who hates, respect and love inspires.
       
       Thou to Rinaldo show'st the damsel fair,
       While he seems hideous to that gentle dame;
       And he, who when the lady's pride and care,
       Paid back with deepest hate her amorous flame,
       Now pines, himself, the victim of despair,
       Scorned in his turn, and his reward the same.
       By the changed damsel in such sort abhorred,
II     She would choose death before that hated lord.
       
       He to the Pagan cries: "Forego thy theft,
       And down, false felon, from that pilfer'd steed;
       I am not wont to let my own be reft.
       And he who seeks it dearly pays the deed.
       More -- I shall take from thee yon lovely weft;
       To leave thee such a prize were foul misdeed;
       And horse and maid, whose worth outstrips belief,
III    Were ill, methinks, relinquished to a thief."
       
       "Thou liest," the haughty Saracen retorts,
       As proud, and burning with as fierce a flame,
       "A thief thyself, if Fame the truth reports:
       But let good deeds decide our dubious claim,
       With whom the steed or damsel fair assorts:
       Best proved by valiant deeds: though, for the dame,
       That nothing is so precious, I with thee
IV     (Search the wide world throughout) may well agree."
        

テキストユーティリティを使用してLinuxでこれを達成する方法があるかどうか疑問に思います。 awkがローマスタイルの数字を生成するのは適していないかもしれませんが、インターネットのどこかでローマ数字を生成するbashスクリプトを見つけました。

#/bin/bash
# roman.sh
#
#   Function
#
    num2roman() { # NUM
    # Returns NUM in roman letters
    #
        input=$1    # input num
        output=""   # Clear output string
        len=${#input}   # Initial length to count down
        
        roman_val() { # NUM one five ten
        # This sub does the basic 'roman' algorythm
        #
            N=$1
            one=$2
            five=$3
            ten=$4
            out=""
            
            case $N in
            0)  out+="" ;;
            [123])  while [[ $N -gt 0 ]]
                do  out+="$one"
                    N=$(($N-1))
                done
                ;;
            4)  out+="$one$five"    ;;
            5)  out+="$five"    ;;
            [678])  out+="$five"
                N=$(($N-5))
                while [[ $N -gt 0 ]]
                do  out+="$one"
                    N=$(($N-1))
                done
                ;;
            9)  while [[ $N -lt 10 ]]
                do  out+="$one"
                    N=$(($N+1))
                done
                out+="$ten"
                ;;
            esac
            echo $out
        }
        
        while [[ $len -gt 0  ]]
        do  # There are letters to add
            num=${input:0:1}
            # Do action according position
            case $len in
            1)  # 1
                output+="$(roman_val $num I V X)"
                ;;
            2)  # 10
                output+="$(roman_val $num X L C)"
                ;;
            3)  # 100
                output+="$(roman_val $num C D M)"
                ;;
            *)  # 1000+
                # 10'000 gets a line above, 100'000 gets a line on the left.. how to?
                num=${input:0:(-3)}
                while [[ $num -gt 0 ]]
                do  output+="M"
                    num=$(($num-1))
                done
                
                ;;
            esac
            input=${input:1} ; len=${#input}
        done
        echo $output
    }
#
#   Call it
#
    num2roman $1

次の構文を使用して呼び出します。

 for N in `seq 1 10`;do ./roman.sh $N; done

出力は次のとおりです。

I
II
III
IV
V
VI
VII
VIII
IX
X

したがって、別の観点から見ると、これは生成されたローマ数字のリストから項目を選択し、その項目をすべてのセクションの最後のセクションに並べ替えることです。

答え1

各ブロックの後の新しい行にレコード番号を印刷する方が簡単な場合があります。空の行が完全に空の場合(つまり空でない場合\n\n)、AWKの「短絡モード」を使用できます\n \n(空の文字列は次のようになります)。RS

function d2r(n,    m) {
    m = sprintf("%*s", int(n/1000), "")
    gsub(/ /, "M", m)
    return m r100[int(n%1000/100)] r10[int(n%100/10)] r1[int(n%10)]
}

BEGIN {
    split("C,CC,CCC,CD,D,DC,DCC,DCCC,CM", r100, ",")
    split("X,XX,XXX,XL,L,LX,LXX,LXXX,XC", r10, ",")
    split("I,II,III,IV,V,VI,VII,VIII,IX", r1, ",")

    RS = ""
}

{
    print
    print d2r(NR) " (" NR ")"
    print ""
}

上記の内容を次のように保存してください。roman_numeral_blocks.awk

$ printf '%s\n\n' foo bar | awk -f roman_numeral_blocks.awk
foo
I (1) 

bar
II (2) 

このセクションでは、正しい結果が生成されたことを" (" NR ")"確認できます。与えられた10進数のローマ数字を生成してみてください。 1000個のバッチごとに「M」を繰り返します。d2r()d2r()

ブロックの最後の行と同じ行に数字を印刷するには、元のインデントを維持する方法と、余白の空きスペースが必要なスペースより少ないときにどのようにすべきかを調べる必要があります。番号を印刷してください。

POSIX awkを使用して上記の内容をOPの例に適用するには:

$ cat roman_numeral_blocks.awk
function d2r(n,    m) {
    m = sprintf("%*s", int(n/1000), "")
    gsub(/ /, "M", m)
    return m r100[int(n%1000/100)] r10[int(n%100/10)] r1[int(n%10)]
}

BEGIN {
    split("C,CC,CCC,CD,D,DC,DCC,DCCC,CM", r100, ",")
    split("X,XX,XXX,XL,L,LX,LXX,LXXX,XC", r10, ",")
    split("I,II,III,IV,V,VI,VII,VIII,IX", r1, ",")
}

NR > 1 { prt(0) }
{ prev = $0 }
END { prt(1) }

function prt(isEnd,     pfx) {
    if ( (!NF || isEnd) && (match(prev, /[^[:space:]]/)) ) {
        prev = substr(prev, RSTART)
        pfx = sprintf("%-*s", RSTART-1, d2r(++numParas) " ")
    }
    print pfx prev
}

$ awk -f roman_numeral_blocks.awk file

       Injurious love, why still to mar accord
       Between desires has been thy favourite feat?
       Why does it please thee so, perfidious lord,
       Two hearts should with a different measure beat?
       Thou wilt not let me take the certain ford,
       Dragging me where the stream is deep and fleet.
       Her I abandon who my love desires,
I      While she who hates, respect and love inspires.

       Thou to Rinaldo show'st the damsel fair,
       While he seems hideous to that gentle dame;
       And he, who when the lady's pride and care,
       Paid back with deepest hate her amorous flame,
       Now pines, himself, the victim of despair,
       Scorned in his turn, and his reward the same.
       By the changed damsel in such sort abhorred,
II     She would choose death before that hated lord.

       He to the Pagan cries: "Forego thy theft,
       And down, false felon, from that pilfer'd steed;
       I am not wont to let my own be reft.
       And he who seeks it dearly pays the deed.
       More -- I shall take from thee yon lovely weft;
       To leave thee such a prize were foul misdeed;
       And horse and maid, whose worth outstrips belief,
III    Were ill, methinks, relinquished to a thief."

       "Thou liest," the haughty Saracen retorts,
       As proud, and burning with as fierce a flame,
       "A thief thyself, if Fame the truth reports:
       But let good deeds decide our dubious claim,
       With whom the steed or damsel fair assorts:
       Best proved by valiant deeds: though, for the dame,
       That nothing is so precious, I with thee
IV     (Search the wide world throughout) may well agree."

答え2

次のPerlスクリプトはPerlを使用します。ローマ基準寸法。使用しているUNIXの種類によっては、オペレーティングシステムに合わせて事前にパッケージ化されている場合があります。たとえば、DebianやUbuntuやMintなどの派生製品ではsudo apt-get install libroman-perl。それ以外の場合は、次のコマンドを使用してインストールしますcpan

$ perl -MRoman -00 -ne '
    @para = split /\n/, $_;
    foreach my $l (0..$#para-1) {
      $para[$l] = sprintf "%6s  %s", "", $para[$l]
    };

    $para[$#para] = sprintf "%6s  %s", roman(++$stanza), $para[$#para];

    print join("\n", @para),"\n\n"' poem2.txt 
        Injurious love, why still to mar accord
        Between desires has been thy favourite feat?
        Why does it please thee so, perfidious lord,
        Two hearts should with a different measure beat?
        Thou wilt not let me take the certain ford,
        Dragging me where the stream is deep and fleet.
        Her I abandon who my love desires,
     i  While she who hates, respect and love inspires.

        Thou to Rinaldo show'st the damsel fair,
        While he seems hideous to that gentle dame;
        And he, who when the lady's pride and care,
        Paid back with deepest hate her amorous flame,
        Now pines, himself, the victim of despair,
        Scorned in his turn, and his reward the same.
        By the changed damsel in such sort abhorred,
    ii  She would choose death before that hated lord.

        He to the Pagan cries: "Forego thy theft,
        And down, false felon, from that pilfer'd steed;
        I am not wont to let my own be reft.
        And he who seeks it dearly pays the deed.
        More -- I shall take from thee yon lovely weft;
        To leave thee such a prize were foul misdeed;
        And horse and maid, whose worth outstrips belief,
   iii  Were ill, methinks, relinquished to a thief."

        "Thou liest," the haughty Saracen retorts,
        As proud, and burning with as fierce a flame,
        "A thief thyself, if Fame the truth reports:
        But let good deeds decide our dubious claim,
        With whom the steed or damsel fair assorts:
        Best proved by valiant deeds: though, for the dame,
        That nothing is so precious, I with thee
    iv  (Search the wide world throughout) may well agree."

これは、段落モードでファイルを読み込み(段落境界は1つ以上の空白を減らす)、各段落を配列(@para)に分割し、@paraの最後の行を8行インデントします。 @paraの最後の行は、6文字の幅のローマ数字(セクション番号用)と2つの余分なスペースにインデントされます。次に配列を印刷します。

大文字のローマ数字では、ucfunction をroman()function と一緒に使用します。つまり、交換します。第二行には以下が含まれますsprintf

$para[$#para] = sprintf "%6s  %s", uc(roman(++$stanza)), $para[$#para];

ローマ数字を左揃えにするには、%-6sたとえば。

$para[$#para] = sprintf "%-6s  %s", roman(++$stanza), $para[$#para];

または

$para[$#para] = sprintf "%-6s  %s", uc(roman(++$stanza)), $para[$#para];

答え3

使用幸せ(以前のPerl_6)

~$ raku -MMath::Roman -e '
 my @para = .split( /^^ \h**7 "\n" /, :skip-empty ) given slurp;
 for @para.kv -> $k,$v { "\n".print; for $v.lines.kv -> $l_key,$l_value {
 put(sprintf( "%*s  ", 5, "{to-roman($k+1) if $l_key == 7}") ~ "{$l_value.trim-leading}")
 };};' Ariosto.txt

入力例(上記のOP投稿から直接コピーして貼り付け):

   Injurious love, why still to mar accord
   Between desires has been thy favourite feat?
   Why does it please thee so, perfidious lord,
   Two hearts should with a different measure beat?
   Thou wilt not let me take the certain ford,
   Dragging me where the stream is deep and fleet.
   Her I abandon who my love desires,
   While she who hates, respect and love inspires.
   
   Thou to Rinaldo show'st the damsel fair,
   While he seems hideous to that gentle dame;
   And he, who when the lady's pride and care,
   Paid back with deepest hate her amorous flame,
   Now pines, himself, the victim of despair,
   Scorned in his turn, and his reward the same.
   By the changed damsel in such sort abhorred,
   She would choose death before that hated lord.
   
   He to the Pagan cries: "Forego thy theft,
   And down, false felon, from that pilfer'd steed;
   I am not wont to let my own be reft.
   And he who seeks it dearly pays the deed.
   More -- I shall take from thee yon lovely weft;
   To leave thee such a prize were foul misdeed;
   And horse and maid, whose worth outstrips belief,
   Were ill, methinks, relinquished to a thief."
   
   "Thou liest," the haughty Saracen retorts,
   As proud, and burning with as fierce a flame,
   "A thief thyself, if Fame the truth reports:
   But let good deeds decide our dubious claim,
   With whom the steed or damsel fair assorts:
   Best proved by valiant deeds: though, for the dame,
   That nothing is so precious, I with thee
   (Search the wide world throughout) may well agree."
   

出力例:

       Injurious love, why still to mar accord
       Between desires has been thy favourite feat?
       Why does it please thee so, perfidious lord,
       Two hearts should with a different measure beat?
       Thou wilt not let me take the certain ford,
       Dragging me where the stream is deep and fleet.
       Her I abandon who my love desires,
    I  While she who hates, respect and love inspires.

       Thou to Rinaldo show'st the damsel fair,
       While he seems hideous to that gentle dame;
       And he, who when the lady's pride and care,
       Paid back with deepest hate her amorous flame,
       Now pines, himself, the victim of despair,
       Scorned in his turn, and his reward the same.
       By the changed damsel in such sort abhorred,
   II  She would choose death before that hated lord.

       He to the Pagan cries: "Forego thy theft,
       And down, false felon, from that pilfer'd steed;
       I am not wont to let my own be reft.
       And he who seeks it dearly pays the deed.
       More -- I shall take from thee yon lovely weft;
       To leave thee such a prize were foul misdeed;
       And horse and maid, whose worth outstrips belief,
  III  Were ill, methinks, relinquished to a thief."

       "Thou liest," the haughty Saracen retorts,
       As proud, and burning with as fierce a flame,
       "A thief thyself, if Fame the truth reports:
       But let good deeds decide our dubious claim,
       With whom the steed or damsel fair assorts:
       Best proved by valiant deeds: though, for the dame,
       That nothing is so precious, I with thee
   IV  (Search the wide world throughout) may well agree."

上記のRakuコードは、既存のインデントにかなり堅牢で、split以下に説明するように、隣接するセクション間のさまざまな間隔に対応するようにコードを調整できます。

簡単に言うと、シーファイルは1)行の開始、2)水平間隔7つ、3)改行パターンを見つける正規表現を使用してslurp段落に挿入されます。split[セクションを区切る行が実際に空の場合は正規表現として使用されます。]この関数は、パラメータ/副詞を介して空の文字列を削除し、4つの段落(セクションなど)を残すように指示されます。段落にはキー値で番号が付けられ、最初にループに渡され、次にネストされたループに渡されます。入れ子になったループでは、値(テキスト)は(8)で除算され、0から7までの番号が付けられます(以下の各セクションの番号を付けるために使用されます)。@para/^^ \h**7 "\n" /^^\h**7\n/^^ $$ "\n" /split:skip-emptykvforforfor$vlineskv

印刷は、段落番号を文字列としてフォーマットしてから(テキスト)文字列をput呼び出すことによって行われます。文字列はを介して連結されます。段落番号付けは、3つの要素を持つ最小幅(右揃え)文字列書式設定ツールを使用して行われます。条件は、段落の7行目(インデックス0)にのみ番号が付けられています。インデントすることにするので、出力が正しく揃うように時の各行をトリミングする必要があります。sprintf$k+1$l_value~sprintf( "%*s ", 5, "{to-roman($k+1) if $l_key == 7}")if$l_key == 7sprintf$l_valuetrim-leading

最後に、サブルーチンはコマンドラインから呼び出されたモジュールを使用して段落カウンタをローマ数字にto-roman(…)変換します。$k+1[実際のサフィックスはここでも機能します]。 Rakuモジュールは以下から検索できます。Math::Roman-M(…)Rhttps://modules.raku.orgインストールしてzef以下を参照してください。https://github.com/ugexe/zef

引用:
https://modules.raku.org/dist/Math::Roman:cpan:TITSUKI
https://raku.org

関連情報