RosettaCodeData/Task/Word-frequency/Bracmat/word-frequency.bracmat

45 lines
2.7 KiB
Plaintext

( 10-most-frequent-words
= MergeSort { Local variable declarations. }
types
sorted-words
frequency
type
most-frequent-words
. ( MergeSort { Definition of function MergeSort. }
= A N Z pivot
. !arg:? [?N { [?N is a subpattern that counts the number of preceding elements }
& ( !N:>1 { if N at least 2 ... }
& div$(!N.2):?pivot { divide N by 2 ... }
& !('($arg:?A [($pivot) ?Z)) { split list in two halves A and Z ... }
& MergeSort$!A+MergeSort$!Z { sort each of A and Z and return sum }
| !arg { else just return a single element}
)
)
& MergeSort { Sort }
$ ( vap { Split second argument at each occurrence of third character and apply first argument to each chunk. }
$ ( (=.low$!arg) { Return input, lowercased. }
. str
$ ( vap { Vaporize second argument in UTF-8 or Latin-1 characters and apply first argument to each of them. }
$ ( (
=
. upp$!arg:low$!arg&\n { Return newline instead of non-alphabetic character. }
| !arg { Return (Euro-centric) alphabetic character.}
)
. get$(!arg,NEW STR) { Read input text as a single string. }
)
)
. \n { Split at newlines }
)
)
: ?sorted-words { Assign sum of (frequency*lowercasedword) terms to sorted-words. }
& :?types { Initialize types as an empty list. }
& whl { Loop until right hand side fails. }
' ( !sorted-words:#?frequency*%@?type+?sorted-words { Extract first frequency*type term from sum. }
& (!frequency.!type) !types:?types { Prepend (frequency.type) pair to types list}
)
& MergeSort$!types { Sort the list of (frequency.type) pairs. }
: (?+[-11+?most-frequent-words|?most-frequent-words) { Pick the last 10 terms from the sum returned by MergeSort. }
& !most-frequent-words { Return the last 10 terms. }
)
& out$(10-most-frequent-words$"135-0.txt") { Call 10-most-frequent-words with name of inout file and print result to screen. }