Abbreviations, automatic: Difference between revisions
m
syntax highlighting fixup automation
Alpha bravo (talk | contribs) (Added AutoHotkey) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 141:
{{trans|Python}}
<
V words = line.split(‘ ’)
V word_count = words.len
Line 162:
print()
automatic_abbreviations(‘daysOfWeek.txt’, 7)</
{{out}}
Line 269:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program abbrAuto64.s */
Line 743:
.include "../includeARM64.inc"
</syntaxhighlight>
<pre>
Nom du fichier : listDays.txt
Line 848:
=={{header|Ada}}==
<
with Ada.Strings.Fixed;
with Ada.Strings.Maps;
Line 918:
Process (Get_Line);
end loop;
end Abbreviations;</
{{out}}
<pre style="height: 20ex;">
Line 1,024:
=={{header|Amazing Hopper}}==
<
#include <jambo.h>
Line 1,053:
End If
End
</syntaxhighlight>
{{out}}
<pre>
Line 1,066:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<
/* ARM assembly Raspberry PI */
/* program abbrAuto.s */
Line 1,487:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
<pre>
Nom du fichier : listDays.txt
Line 1,592:
=={{header|AutoHotkey}}==
<
len := prev := 0
Days := StrSplit(line, " ")
Line 1,613:
}
return len
}</
Examples:<
(
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 1,634:
}
MsgBox % result
return</
{{out}}
<pre>2 > Su Mo Tu We Th Fr Sa
Line 1,644:
=={{header|AWK}}==
<
# syntax: GAWK -f ABBREVIATIONS_AUTOMATIC.AWK ABBREVIATIONS_AUTOMATIC.TXT
{ dow_arr[NR] = $0 }
Line 1,675:
}
function max(x,y) { return((x > y) ? x : y) }
</syntaxhighlight>
{{out}}
<pre style="height:45ex">
Line 1,801:
=={{header|C}}==
{{trans|C#}}
<
#include <stdlib.h>
#include <string.h>
Line 1,895:
return 0;
}</
{{out}}
<pre> 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 1,998:
=={{header|C sharp|C#}}==
<
using System.Collections.Generic;
Line 2,049:
}
}
}</
{{out}}
<pre style="height:45ex"> 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 2,152:
=={{header|C++}}==
{{trans|C#}}
<
#include <iostream>
#include <fstream>
Line 2,217:
return 0;
}</
{{out}}
<pre style="height:45ex"> 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 2,320:
=={{header|COBOL}}==
{{works with|GnuCOBOL|3.1.2.0}}
<
PROGRAM-ID. AUTO-ABBREVIATIONS.
Line 2,478:
CLOSE DOW.
STOP RUN.
</syntaxhighlight>
{{out}}
<pre style="height:45ex">
Line 2,587:
=={{header|Common Lisp}}==
It uses the standard library split-sequence to split the string into words.
<
(defun max-mismatch (list)
(if (cdr list)
Line 2,597:
((null row) t)
(format t "~d ~a~%" (1+ (max-mismatch (SPLIT-SEQUENCE:split-sequence #\Space row))) row) ))
</syntaxhighlight>
{{out}}
Line 2,703:
=={{header|D}}==
{{trans|Kotlin}}
<
import std.exception;
import std.range;
Line 2,738:
}
}
}</
{{out}}
<pre style="height:45ex"> 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 2,846:
{{libheader| System.IOUtils}}
{{Trans|Kotlin}}
<
program Abbreviations_Automatic;
Line 2,934:
end;
Readln;
end.</
=={{header|Erlang}}==
Line 2,941:
The output is of the set of abbreviations. These are not sorted correctly and some encoding errors remain.
<
-module(abbreviateweekdays).
-export([ main/0 ]).
Line 2,978:
{ok, Device} = (file:open("weekdays.txt", read)),
read_lines(Device, 1).
</syntaxhighlight>
{{out}}
<pre>
Line 3,011:
=={{header|F_Sharp|F#}}==
===The function===
<
let fN g=let rec fN n=if g|>List.map(fun(g:string)->g.[0..n])|>Set.ofList|>Set.count=(List.length g) then (n+1) else fN(n+1)
fN 0
</syntaxhighlight>
===The Task - Demonstrate the function===
<
fN ["Sunday"; "Monday"; "Tuesday"; "Wednesday"; "Thursday"; "Friday"; "Saturday"] // -> 2
fN ["Sondag"; "Maandag"; "Dinsdag"; "Woensdag"; "Donderdag"; "Vrydag"; "Saterdag"] // -> 2
Line 3,022:
fN ["Ehud"; "Segno"; "Maksegno"; "Erob"; "Hamus"; "Arbe"; "Kedame"] // -> 2
fN ["Al_Ahad"; "Al_Ithinin"; "Al_Tholatha'a"; "Al_Arbia'a"; "Al_Kamis"; "Al_Gomia'a"; "Al_Sabit";] // -> 5
</syntaxhighlight>
=={{header|Factor}}==
Line 3,029:
As a concatenative language, Factor is uniquely suited for factoring words into smaller words. Assuming lexical/dynamic variables are not used, factoring is a cut-and-paste job that can be performed almost anywhere there is whitespace.
<
sequences sets splitting ;
IN: rosetta-code.abbreviations-automatic
Line 3,057:
header "day-names.txt" utf8 [ body ] with-file-reader ;
MAIN: abbreviations</
{{out}}
<pre style="height:45ex">
Line 3,166:
=={{header|Go}}==
{{trans|Kotlin}}
<
import(
Line 3,240:
}
}
}</
{{out}}
Line 3,249:
=={{header|Groovy}}==
{{trans|Java}}
<
static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("days_of_week.txt"), "utf-8"))
Line 3,293:
}
}
}</
{{out}}
<pre> 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 3,395:
=={{header|Haskell}}==
<
import qualified Data.Set as S
Line 3,422:
]
. return
<$> lines s</
{{Out}}
<pre>2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 3,436:
=={{header|J}}==
<
abbreviation_length =: monad define
N =. # y
Line 3,454:
L =. abbreviation_length&> a
((' ',~":)&> L) ,"1 lines
)</
<pre>
Line 3,477:
=={{header|Java}}==
{{trans|D}}
<
import java.nio.file.Files;
import java.nio.file.Path;
Line 3,528:
}
}
}</
{{out}}
<pre style="height:45ex"> 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 3,632:
===Procedural===
The list of the names was edited and embedded in the HTML-Document with a <script>-tag, where it is accessible as an array called $list.
<
Array.prototype.hasDoubles = function() {
let arr = this.slice();
Line 3,662:
document.write(`<p>(${l}): ${days.join('. ')}.</p>`);
}
</syntaxhighlight>
{{out}}
Line 3,774:
{{Works with|MacOS JXA}}
<
"use strict";
Line 3,934:
// MAIN ---
return main();
})();</
{{Out}}
<pre>2: Su,Mo,Tu,We,Th,Fr,Sa
Line 3,948:
=={{header|Julia}}==
<
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Sondag Maandag Dinsdag Woensdag Donderdag Vrydag Saterdag
Line 4,074:
processweek(text)
</
(Showing first 10 lines.)
Sunday Monday Tuesday Wednesday Thursday Friday Saturday => ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"], which are length 2
Line 4,092:
===Original O(''n<sup>2</sup>'') solution===
where ''n'' is the number of strings in each line.
<
import java.io.File
Line 4,120:
}
}
}</
{{out}}
Line 4,226:
===Alternative O(''n log n'') solution based on sorting===
<
import kotlin.math.max
Line 4,259:
.forEach(::println)
}
}</
{{out}}
Line 4,364:
=={{header|Ksh}}==
<
#!/bin/ksh
Line 4,444:
(( $((RANDOM%100+1))<=5 )) && { printf "%d. " $((i+1)) ; langs[i].print_wk ;}
done</
{{out}}<pre>
15. (4) Dium Dill Dima Dime Dijo Dive Diss
Line 4,455:
=={{header|Lua}}==
<
local wa = {}
for i in string.gmatch(line, "%S+") do
Line 4,505:
end
end
end</
{{out}}
<pre style="height:45ex"> 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 4,607:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
(*This function gives all 'heads' of str of length 1 to maxLength. Since the input data was formatted to use underscores in place of spaces, there is an edge case where distinct abbreviations derived from the input data would actually not be distinct in the 'real' abbreviations. See further comments below regarding the difference between my output and the output of other solutions.*)
Abbreviations[maxLength_Integer][str_String]:=Array[StringTake[StringPadRight[str,maxLength,"_"],#]&,maxLength];
Line 4,621:
RequiredAbbreviationLength[""]="";
RequiredAbbreviationLength[input_String]:=Max[StringLength/@ShortestUniqueAbbreviations[StringSplit[input]]];
</syntaxhighlight>
{{out}}
Line 4,742:
We provide two solutions, one consisting to build the abbreviations for each language and stopping when all of them are different, the second sorting the list of day names for each language and adjusting the abbreviation length to make sure that two consecutive day names are different. The second method should be more efficient, but it doesn’t really matters here.
===Using a set===
<
import sets
import unicode
Line 4,774:
else:
echo line
</syntaxhighlight>
===Using a sorted list===
<
import algorithm
import unicode
Line 4,808:
else:
echo line
</syntaxhighlight>
{{out}}
<pre style="height:45ex">
Line 4,914:
=={{header|Objeck}}==
{{trans|Java}}
<
use Collection;
Line 4,979:
};
}
}</
{{out}}
Line 5,086:
Output is the same as for Raku.
{{trans|Raku}}
<
use utf8;
binmode STDOUT, ":utf8";
Line 5,106:
while ($_ = <$fh>) {
print "$.) " . auto_abbreviate($_) . ' ' . $_;
}</
=={{header|Phix}}==
The file abbrev_auto.txt was manually created from the task description. The following code
assumes a more recent version of get_text() that strips any leading utf8 bom by default (0.8.0+).
<!--<
<span style="color: #008080;">constant</span> <span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_text</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"abbrev_auto.txt"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">GT_LF_STRIPPED</span><span style="color: #0000FF;">)</span>
Line 5,131:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 5,144:
=={{header|Picat}}==
{{works with|Picat}}
<
import util.
Line 5,178:
end
end.
</syntaxhighlight>
{{out}}
<pre style="height:45ex">
Line 5,284:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<
sort(Day_names, Sorted_names),
minimum_abbreviation_length(Sorted_names, Min_length, 1).
Line 5,336:
main:-
process_file("days_of_week.txt").</
{{out}}
Line 5,442:
=={{header|PureBasic}}==
<
#TZ="|"
#FZ="@"
Line 5,487:
While CompareLetters(txt,letters) : letters+1 : Wend
ProcedureReturn letters
EndProcedure</
{{out}}
<pre style="height:45ex"> 2: Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 5,691:
{{works with|Python|3.6}}
{{trans|Kotlin}}
<
words = line.split()
word_count = len(words)
Line 5,718:
print()
automatic_abbreviations('daysOfWeek.txt', 7)</
{{out}}
Line 5,825:
===Functional===
In terms of generators:
<
from itertools import (accumulate,
repeat)
Line 5,878:
if __name__ == '__main__':
main()</
{{out}}
<pre style="height:45ex">
Line 5,988:
The generic primitives are curried, allowing for more flexible composition:
{{Works with|Python|3}}
<
from itertools import (accumulate, chain)
Line 6,111:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 6,129:
=={{header|Racket}}==
<
(require racket/set)
Line 6,146:
["" ""]
[(and s (app string-split ss)) (format "~a ~a" (abbr-length ss) s)]))
(for-each (compose displayln report-line) (take (file->lines "data.txt") 5)))</
{{out}}
Line 6,162:
Note that this is using a previous version of the date file that has erroneous duplicate day names (see line 90). Since the effort was already expended to catch such problems, it may as well be demonstrated.
<
return Nil unless my @words = $string.words;
return $_ if @words».substr(0, $_).Set == @words for 1 .. @words».chars.max;
Line 6,169:
# Testing
say ++$, ') ', .&auto-abbreviate, ' ', $_ for './DoWAKA.txt'.IO.lines;</
{{out}}
<pre style="height:40ex;overflow:scroll;">1) 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 6,273:
=={{header|Red}}==
<
Red []
;; read and convert data to a string - to char conversion is neccessary to avoid
Line 6,304:
print [min line] ;; print automatically reduces all words in block
]
</syntaxhighlight>
{{Out}}
<pre>
Line 6,326:
=={{header|REXX}}==
<
parse arg uw /*obtain optional arguments from the CL*/
iFID= 'ABBREV_A.TAB' /*name of the file that has the table. */
Line 6,354:
leave m /*a good abbreviation length was found.*/
end /*m*/
return m</
{{out|output|text= when using the default input file containing the complete list of words:}}
<pre style="height:45ex">
Line 6,463:
=={{header|Ruby}}==
<
File.read("daynames.txt").each_line do |line|
Line 6,470:
puts "Minimum size: #{abbr.values.max_by(&:size).size}", abbr.inspect, "\n"
end
</syntaxhighlight>
{{out}}
<pre>Minimum size: 2
Line 6,505:
=={{header|Rust}}==
<
* Abbreviations from tintenalarm.de
*/
Line 6,557:
}
num
}</
{{out}}
<pre style="height:45ex">
Line 6,663:
===Functional programmed===
====build.sbt====
<
scalaVersion := "2.13.0"
version := "0.1"
Line 6,669:
homepage := Some(url("http://rosettacode.org/wiki/Abbreviations,_automatic#Scala"))
libraryDependencies += "com.lihaoyi" %% "os-lib" % "0.3.0"</
====AbbreviationsAuto.scala====
<
private val wd = os.pwd
Line 6,693:
.foreach(line => println(processLine(line)))
}</
{{out}}
<pre style="height:45ex"> ""
Line 6,794:
=={{header|SenseTalk}}==
<
put 1 into abbreviationLength
put the number of items in days into len
Line 6,811:
add 1 to abbreviationLength
end repeat
end AbbreviationsAutomatic</
<
put the defaultStringEncoding into savedEncoding
Line 6,833:
close file myFile
put savedEncoding into the defaultStringEncoding</
{{out}}
<pre>
Line 6,851:
=={{header|Tcl}}==
<
set f [open abbreviations_automatic_weekdays.txt]
set lines [split [read -nonewline $f] \n]
Line 6,869:
incr i
puts "$i $days"
}</
{{out}}
<pre>2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 6,886:
=={{header|Transd}}==
<
MainModule : {
Line 6,898:
(lout (+ len 1) " " days))
) ) )
}</
<pre style="height:45ex">
2 [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
Line 7,002:
=={{header|TSE SAL}}==
<
STRING PROC FNStringGetExpressionRegularCharacterMetaEscapeS( STRING inS )
STRING s[255] = inS
Line 7,099:
GotoBufferId( bufferI )
END
</syntaxhighlight>
{{out}} <pre>
2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 7,203:
=={{header|VBA}}==
Function :
<
Dim myVar As Variant, I As Integer, Flag As Boolean, myColl As Collection, Count As Integer
myVar = Split(strLine, " ")
Line 7,221:
Loop While Not Flag
MinimalLenght = Count
End Function</
To call it :
<
Dim Nb As Integer, s As String, Result() As String, c As Integer
Nb = FreeFile
Line 7,238:
Wend
Close #Nb
End Sub</
{{out}}
<pre style="height:45ex">2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 7,340:
=={{header|VBScript}}==
<syntaxhighlight lang=vb>
sub print(s) wscript.stdout.writeline s :end sub
Line 7,378:
f.close
</syntaxhighlight>
{{out}}
<pre>
Line 7,411:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Sub Main()
Line 7,459:
End Sub
End Module</
{{out}}
<pre> 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 7,562:
=={{header|Vlang}}==
{{trans|Go}}
<
fn distinct_strings(strs []string) []string {
Line 7,620:
}
}
}</
{{out}}
Line 7,632:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<
import "/pattern" for Pattern
import "/seq" for Lst
Line 7,660:
}
i = i + 1
}</
{{out}}
Line 7,766:
=={{header|Yabasic}}==
<
a = open("days_of_week.txt", "r")
Line 7,794:
until(s)
return d
end sub</
=={{header|zkl}}==
zkl doesn't grok UTF-8 so I'm using a byte by byte check. It works surprisingly well.
<
.pump(List,Void.Filter,fcn(day){
d,N,m := day.split(),d.len(),(0).max(d.apply("len")); // N==7
Line 7,808:
return(m,day); // no match nowhere
});
foreach n,s in (nds){ println("%3d %s".fmt(n,s)); }</
{{out}}
<pre style="height:45ex">
|