Using Raku (formerly known as Perl_6)
raku -e 'my @a.=push: .comb(/<alpha>+/)[*-1] if .chars for lines; \
.put for @a.unique>>.flip.sort( *.fc.trans: "àèéìòù" => "aeeiou" )>>.flip;'
OR
raku -e 'my @a.=push: .comb(/<alpha>+/)[*-1] if .chars for lines; \
.put for @a.unique.map(*.flip).sort( *.fc.trans: "àèéìòù" => "aeeiou" ).map(*.flip);'
Above are answers coded in Raku, which is built to handle Unicode by default. Briefly, if lines
contain chars
characters (i.e. are not blank), push
the [*-1]
last-element of a line that has been comb
ed through via regex looking for <alpha>+
one-or-more alphabetic characters (positive-selection for desired elements, as opposed to a destructive split
). [Note, here words
would be a close approximation, except words
only splits on whitespace and thus would still leave punctuation characters in the resultant elements].
Once @a
array is filled ( here my @a.=push( … )
de-sugars to my @a = @a.push( … )
), elements of @a are unique
-ified, flip
ped, sort
ed, and flip
ped back.
Sorting is accomplished with the routine/parameters .sort( *.fc.trans: "àèéìòù" => "aeeiou" )
, meaning that *
elements are sorted on characters that have been fc
case-folded as well as the six accented characters trans
lated: "àèéìòù" => "aeeiou"
. Without the trans
routine, words ending in those six accented characters sort to the end of the list.
Sample Input:
Se a ciascun l'interno affanno
Si leggesse in fronte scritto
Quanti mai, che invidia fanno
Ci farebbero pietà!
Sample Output:
pietà
fanno
affanno
scritto
I took the liberty of testing another poem by Pietro Metastasio entitled La libertà. Sample Output is shown below, however I added .join(", ")
at the end of the code to return comma-separated output (instead of one-word-per-line). First answer below with the call to trans: "àèéìòù" => "aeeiou"
, second answer below without the call to trans: "àèéìòù" => "aeeiou"
:
~$ perl6 -e 'my @a.=push: .comb(/<alpha>+/)[*-1] if .chars for lines; .put for @a.unique.map(*.flip).sort( *.fc.trans: "àèéìòù" => "aeeiou" ).map(*.flip).join(", "); put("");' file.txt
fa, ha, bella, quella, pena, catena, ragiona, sprona, ancora, talora, pietà, beltà, sciolta, volta, libertà, rinnova, prova, è, piace, spiace, infelice, Nice, ingannatrice, fé, me, penne, avvenne, core, ardore, colore, rossore, te, amante, incostante, fai, mai, guai, spezzai, dì, miei, sei, sdegni, segni, suoi, tuoi, così, dico, antico, parlando, dimando, umano, vano, sdegno, segno, hanno, sanno, dono, ragiono, sono, sincero, primiero, impero, altero, vero, aggiro, miro, curo, procuro, so, passò, appresso, oppresso, stesso, istesso, ingrato, prato, ascolto, volto, cimento, rammento, sento, contento, estinto, istinto, difetto, aspetto, consolar, parlar, sdegnar, trovar, piacer, pensier, soffrir, morir, cor, amor, favor
~$ perl6 -e 'my @a.=push: .comb(/<alpha>+/)[*-1] if .chars for lines; .put for @a.unique.map(.flip).sort( .fc ).map(*.flip).join(", "); put("");' file.txt
fa, ha, bella, quella, pena, catena, ragiona, sprona, ancora, talora, sciolta, volta, rinnova, prova, piace, spiace, infelice, Nice, ingannatrice, me, penne, avvenne, core, ardore, colore, rossore, te, amante, incostante, fai, mai, guai, spezzai, miei, sei, sdegni, segni, suoi, tuoi, dico, antico, parlando, dimando, umano, vano, sdegno, segno, hanno, sanno, dono, ragiono, sono, sincero, primiero, impero, altero, vero, aggiro, miro, curo, procuro, so, appresso, oppresso, stesso, istesso, ingrato, prato, ascolto, volto, cimento, rammento, sento, contento, estinto, istinto, difetto, aspetto, consolar, parlar, sdegnar, trovar, piacer, pensier, soffrir, morir, cor, amor, favor, pietà, beltà, libertà, è, fé, dì, così, passò
Caveat: because all punctuation gets removed, hyphenated words (if any) are split into component words during analysis.
tr -cs
is to split things likel'interno
intol
andinterno
. – Stéphane Chazelas May 14 '22 at 18:02