Abbreviations, automatic: Difference between revisions

Content added Content deleted
(Added AutoHotkey)
m (syntax highlighting fixup automation)
Line 141: Line 141:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F shortest_abbreviation_length(line, list_size)
<syntaxhighlight lang=11l>F shortest_abbreviation_length(line, list_size)
V words = line.split(‘ ’)
V words = line.split(‘ ’)
V word_count = words.len
V word_count = words.len
Line 162: Line 162:
print()
print()


automatic_abbreviations(‘daysOfWeek.txt’, 7)</lang>
automatic_abbreviations(‘daysOfWeek.txt’, 7)</syntaxhighlight>


{{out}}
{{out}}
Line 269: Line 269:
=={{header|AArch64 Assembly}}==
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<lang AArch64 Assembly>
<syntaxhighlight lang=AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program abbrAuto64.s */
/* program abbrAuto64.s */
Line 743: Line 743:
.include "../includeARM64.inc"
.include "../includeARM64.inc"


</syntaxhighlight>
</lang>
<pre>
<pre>
Nom du fichier : listDays.txt
Nom du fichier : listDays.txt
Line 848: Line 848:


=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Containers.Indefinite_Vectors;
<syntaxhighlight lang=Ada>with Ada.Containers.Indefinite_Vectors;
with Ada.Strings.Fixed;
with Ada.Strings.Fixed;
with Ada.Strings.Maps;
with Ada.Strings.Maps;
Line 918: Line 918:
Process (Get_Line);
Process (Get_Line);
end loop;
end loop;
end Abbreviations;</lang>
end Abbreviations;</syntaxhighlight>
{{out}}
{{out}}
<pre style="height: 20ex;">
<pre style="height: 20ex;">
Line 1,024: Line 1,024:


=={{header|Amazing Hopper}}==
=={{header|Amazing Hopper}}==
<lang Amazing Hopper>
<syntaxhighlight lang=Amazing Hopper>
#include <jambo.h>
#include <jambo.h>


Line 1,053: Line 1,053:
End If
End If
End
End
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,066: Line 1,066:
=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
{{works with|as|Raspberry Pi}}
<lang ARM Assembly>
<syntaxhighlight lang=ARM Assembly>
/* ARM assembly Raspberry PI */
/* ARM assembly Raspberry PI */
/* program abbrAuto.s */
/* program abbrAuto.s */
Line 1,487: Line 1,487:
/***************************************************/
/***************************************************/
.include "../affichage.inc"
.include "../affichage.inc"
</syntaxhighlight>
</lang>
<pre>
<pre>
Nom du fichier : listDays.txt
Nom du fichier : listDays.txt
Line 1,592: Line 1,592:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>AutoAbbreviations(line){
<syntaxhighlight lang=AutoHotkey>AutoAbbreviations(line){
len := prev := 0
len := prev := 0
Days := StrSplit(line, " ")
Days := StrSplit(line, " ")
Line 1,613: Line 1,613:
}
}
return len
return len
}</lang>
}</syntaxhighlight>
Examples:<lang AutoHotkey>data := "
Examples:<syntaxhighlight lang=AutoHotkey>data := "
(
(
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 1,634: Line 1,634:
}
}
MsgBox % result
MsgBox % result
return</lang>
return</syntaxhighlight>
{{out}}
{{out}}
<pre>2 > Su Mo Tu We Th Fr Sa
<pre>2 > Su Mo Tu We Th Fr Sa
Line 1,644: Line 1,644:


=={{header|AWK}}==
=={{header|AWK}}==
<lang AWK>
<syntaxhighlight lang=AWK>
# syntax: GAWK -f ABBREVIATIONS_AUTOMATIC.AWK ABBREVIATIONS_AUTOMATIC.TXT
# syntax: GAWK -f ABBREVIATIONS_AUTOMATIC.AWK ABBREVIATIONS_AUTOMATIC.TXT
{ dow_arr[NR] = $0 }
{ dow_arr[NR] = $0 }
Line 1,675: Line 1,675:
}
}
function max(x,y) { return((x > y) ? x : y) }
function max(x,y) { return((x > y) ? x : y) }
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre style="height:45ex">
<pre style="height:45ex">
Line 1,801: Line 1,801:
=={{header|C}}==
=={{header|C}}==
{{trans|C#}}
{{trans|C#}}
<lang c>#include <stdio.h>
<syntaxhighlight lang=c>#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
Line 1,895: Line 1,895:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre> 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
<pre> 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 1,998: Line 1,998:


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<lang csharp>using System;
<syntaxhighlight lang=csharp>using System;
using System.Collections.Generic;
using System.Collections.Generic;


Line 2,049: Line 2,049:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:45ex"> 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
<pre style="height:45ex"> 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 2,152: Line 2,152:
=={{header|C++}}==
=={{header|C++}}==
{{trans|C#}}
{{trans|C#}}
<lang cpp>#include <iomanip>
<syntaxhighlight lang=cpp>#include <iomanip>
#include <iostream>
#include <iostream>
#include <fstream>
#include <fstream>
Line 2,217: Line 2,217:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:45ex"> 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
<pre style="height:45ex"> 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 2,320: Line 2,320:
=={{header|COBOL}}==
=={{header|COBOL}}==
{{works with|GnuCOBOL|3.1.2.0}}
{{works with|GnuCOBOL|3.1.2.0}}
<lang cobol> IDENTIFICATION DIVISION.
<syntaxhighlight lang=cobol> IDENTIFICATION DIVISION.
PROGRAM-ID. AUTO-ABBREVIATIONS.
PROGRAM-ID. AUTO-ABBREVIATIONS.


Line 2,478: Line 2,478:
CLOSE DOW.
CLOSE DOW.
STOP RUN.
STOP RUN.
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre style="height:45ex">
<pre style="height:45ex">
Line 2,587: Line 2,587:
=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
It uses the standard library split-sequence to split the string into words.
It uses the standard library split-sequence to split the string into words.
<lang lisp>
<syntaxhighlight lang=lisp>
(defun max-mismatch (list)
(defun max-mismatch (list)
(if (cdr list)
(if (cdr list)
Line 2,597: Line 2,597:
((null row) t)
((null row) t)
(format t "~d ~a~%" (1+ (max-mismatch (SPLIT-SEQUENCE:split-sequence #\Space row))) row) ))
(format t "~d ~a~%" (1+ (max-mismatch (SPLIT-SEQUENCE:split-sequence #\Space row))) row) ))
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 2,703: Line 2,703:
=={{header|D}}==
=={{header|D}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang D>import std.conv;
<syntaxhighlight lang=D>import std.conv;
import std.exception;
import std.exception;
import std.range;
import std.range;
Line 2,738: Line 2,738:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:45ex"> 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
<pre style="height:45ex"> 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 2,846: Line 2,846:
{{libheader| System.IOUtils}}
{{libheader| System.IOUtils}}
{{Trans|Kotlin}}
{{Trans|Kotlin}}
<lang Delphi>
<syntaxhighlight lang=Delphi>
program Abbreviations_Automatic;
program Abbreviations_Automatic;


Line 2,934: Line 2,934:
end;
end;
Readln;
Readln;
end.</lang>
end.</syntaxhighlight>


=={{header|Erlang}}==
=={{header|Erlang}}==
Line 2,941: Line 2,941:
The output is of the set of abbreviations. These are not sorted correctly and some encoding errors remain.
The output is of the set of abbreviations. These are not sorted correctly and some encoding errors remain.


<lang erlang>
<syntaxhighlight lang=erlang>
-module(abbreviateweekdays).
-module(abbreviateweekdays).
-export([ main/0 ]).
-export([ main/0 ]).
Line 2,978: Line 2,978:
{ok, Device} = (file:open("weekdays.txt", read)),
{ok, Device} = (file:open("weekdays.txt", read)),
read_lines(Device, 1).
read_lines(Device, 1).
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 3,011: Line 3,011:
=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
===The function===
===The function===
<lang fsharp>
<syntaxhighlight lang=fsharp>
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)
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
fN 0
</syntaxhighlight>
</lang>
===The Task - Demonstrate the function===
===The Task - Demonstrate the function===
<lang fsharp>
<syntaxhighlight lang=fsharp>
fN ["Sunday"; "Monday"; "Tuesday"; "Wednesday"; "Thursday"; "Friday"; "Saturday"] // -> 2
fN ["Sunday"; "Monday"; "Tuesday"; "Wednesday"; "Thursday"; "Friday"; "Saturday"] // -> 2
fN ["Sondag"; "Maandag"; "Dinsdag"; "Woensdag"; "Donderdag"; "Vrydag"; "Saterdag"] // -> 2
fN ["Sondag"; "Maandag"; "Dinsdag"; "Woensdag"; "Donderdag"; "Vrydag"; "Saterdag"] // -> 2
Line 3,022: Line 3,022:
fN ["Ehud"; "Segno"; "Maksegno"; "Erob"; "Hamus"; "Arbe"; "Kedame"] // -> 2
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
fN ["Al_Ahad"; "Al_Ithinin"; "Al_Tholatha'a"; "Al_Arbia'a"; "Al_Kamis"; "Al_Gomia'a"; "Al_Sabit";] // -> 5
</syntaxhighlight>
</lang>


=={{header|Factor}}==
=={{header|Factor}}==
Line 3,029: 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.
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.


<lang factor>USING: formatting io io.encodings.utf8 io.files kernel math
<syntaxhighlight lang=factor>USING: formatting io io.encodings.utf8 io.files kernel math
sequences sets splitting ;
sequences sets splitting ;
IN: rosetta-code.abbreviations-automatic
IN: rosetta-code.abbreviations-automatic
Line 3,057: Line 3,057:
header "day-names.txt" utf8 [ body ] with-file-reader ;
header "day-names.txt" utf8 [ body ] with-file-reader ;


MAIN: abbreviations</lang>
MAIN: abbreviations</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:45ex">
<pre style="height:45ex">
Line 3,166: Line 3,166:
=={{header|Go}}==
=={{header|Go}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang go>package main
<syntaxhighlight lang=go>package main


import(
import(
Line 3,240: Line 3,240:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,249: Line 3,249:
=={{header|Groovy}}==
=={{header|Groovy}}==
{{trans|Java}}
{{trans|Java}}
<lang groovy>class Abbreviations {
<syntaxhighlight lang=groovy>class Abbreviations {
static void main(String[] args) throws IOException {
static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("days_of_week.txt"), "utf-8"))
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("days_of_week.txt"), "utf-8"))
Line 3,293: Line 3,293:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre> 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
<pre> 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 3,395: Line 3,395:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.List (inits, intercalate, transpose)
<syntaxhighlight lang=haskell>import Data.List (inits, intercalate, transpose)
import qualified Data.Set as S
import qualified Data.Set as S


Line 3,422: Line 3,422:
]
]
. return
. return
<$> lines s</lang>
<$> lines s</syntaxhighlight>
{{Out}}
{{Out}}
<pre>2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
<pre>2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 3,436: Line 3,436:


=={{header|J}}==
=={{header|J}}==
<lang J>NB. y is words in boxes
<syntaxhighlight lang=J>NB. y is words in boxes
abbreviation_length =: monad define
abbreviation_length =: monad define
N =. # y
N =. # y
Line 3,454: Line 3,454:
L =. abbreviation_length&> a
L =. abbreviation_length&> a
((' ',~":)&> L) ,"1 lines
((' ',~":)&> L) ,"1 lines
)</lang>
)</syntaxhighlight>


<pre>
<pre>
Line 3,477: Line 3,477:
=={{header|Java}}==
=={{header|Java}}==
{{trans|D}}
{{trans|D}}
<lang Java>import java.io.IOException;
<syntaxhighlight lang=Java>import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Path;
Line 3,528: Line 3,528:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:45ex"> 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
<pre style="height:45ex"> 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 3,632: Line 3,632:
===Procedural===
===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.
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.
<lang javascript>
<syntaxhighlight lang=javascript>
Array.prototype.hasDoubles = function() {
Array.prototype.hasDoubles = function() {
let arr = this.slice();
let arr = this.slice();
Line 3,662: Line 3,662:
document.write(`<p>(${l}): ${days.join('. ')}.</p>`);
document.write(`<p>(${l}): ${days.join('. ')}.</p>`);
}
}
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 3,774: Line 3,774:


{{Works with|MacOS JXA}}
{{Works with|MacOS JXA}}
<lang javascript>(() => {
<syntaxhighlight lang=javascript>(() => {
"use strict";
"use strict";


Line 3,934: Line 3,934:
// MAIN ---
// MAIN ---
return main();
return main();
})();</lang>
})();</syntaxhighlight>
{{Out}}
{{Out}}
<pre>2: Su,Mo,Tu,We,Th,Fr,Sa
<pre>2: Su,Mo,Tu,We,Th,Fr,Sa
Line 3,948: Line 3,948:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>const text = """
<syntaxhighlight lang=julia>const text = """
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Sondag Maandag Dinsdag Woensdag Donderdag Vrydag Saterdag
Sondag Maandag Dinsdag Woensdag Donderdag Vrydag Saterdag
Line 4,074: Line 4,074:


processweek(text)
processweek(text)
</lang> {{output}}<pre>
</syntaxhighlight> {{output}}<pre>
(Showing first 10 lines.)
(Showing first 10 lines.)
Sunday Monday Tuesday Wednesday Thursday Friday Saturday => ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"], which are length 2
Sunday Monday Tuesday Wednesday Thursday Friday Saturday => ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"], which are length 2
Line 4,092: Line 4,092:
===Original O(''n<sup>2</sup>'') solution===
===Original O(''n<sup>2</sup>'') solution===
where ''n'' is the number of strings in each line.
where ''n'' is the number of strings in each line.
<lang scala>// version 1.1.4-3
<syntaxhighlight lang=scala>// version 1.1.4-3


import java.io.File
import java.io.File
Line 4,120: Line 4,120:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 4,226: Line 4,226:


===Alternative O(''n log n'') solution based on sorting===
===Alternative O(''n log n'') solution based on sorting===
<lang scala>import java.io.File
<syntaxhighlight lang=scala>import java.io.File
import kotlin.math.max
import kotlin.math.max


Line 4,259: Line 4,259:
.forEach(::println)
.forEach(::println)
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 4,364: Line 4,364:


=={{header|Ksh}}==
=={{header|Ksh}}==
<lang ksh>
<syntaxhighlight lang=ksh>
#!/bin/ksh
#!/bin/ksh


Line 4,444: Line 4,444:
(( $((RANDOM%100+1))<=5 )) && { printf "%d. " $((i+1)) ; langs[i].print_wk ;}
(( $((RANDOM%100+1))<=5 )) && { printf "%d. " $((i+1)) ; langs[i].print_wk ;}
done</lang>
done</syntaxhighlight>
{{out}}<pre>
{{out}}<pre>
15. (4) Dium Dill Dima Dime Dijo Dive Diss
15. (4) Dium Dill Dima Dime Dijo Dive Diss
Line 4,455: Line 4,455:


=={{header|Lua}}==
=={{header|Lua}}==
<lang lua>function split(line)
<syntaxhighlight lang=lua>function split(line)
local wa = {}
local wa = {}
for i in string.gmatch(line, "%S+") do
for i in string.gmatch(line, "%S+") do
Line 4,505: Line 4,505:
end
end
end
end
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:45ex"> 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
<pre style="height:45ex"> 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 4,607: Line 4,607:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>
<syntaxhighlight lang=Mathematica>
(*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.*)
(*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];
Abbreviations[maxLength_Integer][str_String]:=Array[StringTake[StringPadRight[str,maxLength,"_"],#]&,maxLength];
Line 4,621: Line 4,621:
RequiredAbbreviationLength[""]="";
RequiredAbbreviationLength[""]="";
RequiredAbbreviationLength[input_String]:=Max[StringLength/@ShortestUniqueAbbreviations[StringSplit[input]]];
RequiredAbbreviationLength[input_String]:=Max[StringLength/@ShortestUniqueAbbreviations[StringSplit[input]]];
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 4,742: 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.
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===
===Using a set===
<lang Nim>
<syntaxhighlight lang=Nim>
import sets
import sets
import unicode
import unicode
Line 4,774: Line 4,774:
else:
else:
echo line
echo line
</syntaxhighlight>
</lang>
===Using a sorted list===
===Using a sorted list===
<lang Nim>
<syntaxhighlight lang=Nim>
import algorithm
import algorithm
import unicode
import unicode
Line 4,808: Line 4,808:
else:
else:
echo line
echo line
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre style="height:45ex">
<pre style="height:45ex">
Line 4,914: Line 4,914:
=={{header|Objeck}}==
=={{header|Objeck}}==
{{trans|Java}}
{{trans|Java}}
<lang objeck>use System.IO.File;
<syntaxhighlight lang=objeck>use System.IO.File;
use Collection;
use Collection;


Line 4,979: Line 4,979:
};
};
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 5,086: Line 5,086:
Output is the same as for Raku.
Output is the same as for Raku.
{{trans|Raku}}
{{trans|Raku}}
<lang perl>use strict;
<syntaxhighlight lang=perl>use strict;
use utf8;
use utf8;
binmode STDOUT, ":utf8";
binmode STDOUT, ":utf8";
Line 5,106: Line 5,106:
while ($_ = <$fh>) {
while ($_ = <$fh>) {
print "$.) " . auto_abbreviate($_) . ' ' . $_;
print "$.) " . auto_abbreviate($_) . ' ' . $_;
}</lang>
}</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
The file abbrev_auto.txt was manually created from the task description. The following code
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+).
assumes a more recent version of get_text() that strips any leading utf8 bom by default (0.8.0+).
<!--<lang Phix>-->
<!--<syntaxhighlight lang=Phix>-->
<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>
<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: 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;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 5,144: Line 5,144:
=={{header|Picat}}==
=={{header|Picat}}==
{{works with|Picat}}
{{works with|Picat}}
<lang Picat>
<syntaxhighlight lang=Picat>
import util.
import util.


Line 5,178: Line 5,178:
end
end
end.
end.
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre style="height:45ex">
<pre style="height:45ex">
Line 5,284: Line 5,284:
=={{header|Prolog}}==
=={{header|Prolog}}==
{{works with|SWI Prolog}}
{{works with|SWI Prolog}}
<lang prolog>minimum_abbreviation_length(Day_names, Min_length):-
<syntaxhighlight lang=prolog>minimum_abbreviation_length(Day_names, Min_length):-
sort(Day_names, Sorted_names),
sort(Day_names, Sorted_names),
minimum_abbreviation_length(Sorted_names, Min_length, 1).
minimum_abbreviation_length(Sorted_names, Min_length, 1).
Line 5,336: Line 5,336:


main:-
main:-
process_file("days_of_week.txt").</lang>
process_file("days_of_week.txt").</syntaxhighlight>


{{out}}
{{out}}
Line 5,442: Line 5,442:


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>EnableExplicit
<syntaxhighlight lang=PureBasic>EnableExplicit
#TZ="|"
#TZ="|"
#FZ="@"
#FZ="@"
Line 5,487: Line 5,487:
While CompareLetters(txt,letters) : letters+1 : Wend
While CompareLetters(txt,letters) : letters+1 : Wend
ProcedureReturn letters
ProcedureReturn letters
EndProcedure</lang>
EndProcedure</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:45ex"> 2: Sunday Monday Tuesday Wednesday Thursday Friday Saturday
<pre style="height:45ex"> 2: Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 5,691: Line 5,691:
{{works with|Python|3.6}}
{{works with|Python|3.6}}
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang python>def shortest_abbreviation_length(line, list_size):
<syntaxhighlight lang=python>def shortest_abbreviation_length(line, list_size):
words = line.split()
words = line.split()
word_count = len(words)
word_count = len(words)
Line 5,718: Line 5,718:
print()
print()


automatic_abbreviations('daysOfWeek.txt', 7)</lang>
automatic_abbreviations('daysOfWeek.txt', 7)</syntaxhighlight>


{{out}}
{{out}}
Line 5,825: Line 5,825:
===Functional===
===Functional===
In terms of generators:
In terms of generators:
<lang python>import operator
<syntaxhighlight lang=python>import operator
from itertools import (accumulate,
from itertools import (accumulate,
repeat)
repeat)
Line 5,878: Line 5,878:


if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:45ex">
<pre style="height:45ex">
Line 5,988: Line 5,988:
The generic primitives are curried, allowing for more flexible composition:
The generic primitives are curried, allowing for more flexible composition:
{{Works with|Python|3}}
{{Works with|Python|3}}
<lang python>'''Automatic abbreviations'''
<syntaxhighlight lang=python>'''Automatic abbreviations'''


from itertools import (accumulate, chain)
from itertools import (accumulate, chain)
Line 6,111: Line 6,111:
# MAIN ---
# MAIN ---
if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>
{{Out}}
{{Out}}
<pre>2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
<pre>2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 6,129: Line 6,129:
=={{header|Racket}}==
=={{header|Racket}}==


<lang racket>#lang racket
<syntaxhighlight lang=racket>#lang racket


(require racket/set)
(require racket/set)
Line 6,146: Line 6,146:
["" ""]
["" ""]
[(and s (app string-split ss)) (format "~a ~a" (abbr-length ss) s)]))
[(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)))</lang>
(for-each (compose displayln report-line) (take (file->lines "data.txt") 5)))</syntaxhighlight>


{{out}}
{{out}}
Line 6,162: 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.
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.


<lang perl6>sub auto-abbreviate ( Str $string ) {
<syntaxhighlight lang=perl6>sub auto-abbreviate ( Str $string ) {
return Nil unless my @words = $string.words;
return Nil unless my @words = $string.words;
return $_ if @words».substr(0, $_).Set == @words for 1 .. @words».chars.max;
return $_ if @words».substr(0, $_).Set == @words for 1 .. @words».chars.max;
Line 6,169: Line 6,169:


# Testing
# Testing
say ++$, ') ', .&auto-abbreviate, ' ', $_ for './DoWAKA.txt'.IO.lines;</lang>
say ++$, ') ', .&auto-abbreviate, ' ', $_ for './DoWAKA.txt'.IO.lines;</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:40ex;overflow:scroll;">1) 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
<pre style="height:40ex;overflow:scroll;">1) 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 6,273: Line 6,273:


=={{header|Red}}==
=={{header|Red}}==
<lang Red>
<syntaxhighlight lang=Red>
Red []
Red []
;; read and convert data to a string - to char conversion is neccessary to avoid
;; read and convert data to a string - to char conversion is neccessary to avoid
Line 6,304: Line 6,304:
print [min line] ;; print automatically reduces all words in block
print [min line] ;; print automatically reduces all words in block
]
]
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>
<pre>
Line 6,326: Line 6,326:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program finds the minimum length abbreviation for a lists of words (from a file).*/
<syntaxhighlight lang=rexx>/*REXX program finds the minimum length abbreviation for a lists of words (from a file).*/
parse arg uw /*obtain optional arguments from the CL*/
parse arg uw /*obtain optional arguments from the CL*/
iFID= 'ABBREV_A.TAB' /*name of the file that has the table. */
iFID= 'ABBREV_A.TAB' /*name of the file that has the table. */
Line 6,354: Line 6,354:
leave m /*a good abbreviation length was found.*/
leave m /*a good abbreviation length was found.*/
end /*m*/
end /*m*/
return m</lang>
return m</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input file containing the complete list of words:}}
{{out|output|text=&nbsp; when using the default input file containing the complete list of words:}}
<pre style="height:45ex">
<pre style="height:45ex">
Line 6,463: Line 6,463:
=={{header|Ruby}}==
=={{header|Ruby}}==


<lang ruby>require "abbrev"
<syntaxhighlight lang=ruby>require "abbrev"
File.read("daynames.txt").each_line do |line|
File.read("daynames.txt").each_line do |line|
Line 6,470: Line 6,470:
puts "Minimum size: #{abbr.values.max_by(&:size).size}", abbr.inspect, "\n"
puts "Minimum size: #{abbr.values.max_by(&:size).size}", abbr.inspect, "\n"
end
end
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>Minimum size: 2
<pre>Minimum size: 2
Line 6,505: Line 6,505:


=={{header|Rust}}==
=={{header|Rust}}==
<lang c>/**
<syntaxhighlight lang=c>/**
* Abbreviations from tintenalarm.de
* Abbreviations from tintenalarm.de
*/
*/
Line 6,557: Line 6,557:
}
}
num
num
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:45ex">
<pre style="height:45ex">
Line 6,663: Line 6,663:
===Functional programmed===
===Functional programmed===
====build.sbt====
====build.sbt====
<lang Scala>name := "Abbreviations-automatic"
<syntaxhighlight lang=Scala>name := "Abbreviations-automatic"
scalaVersion := "2.13.0"
scalaVersion := "2.13.0"
version := "0.1"
version := "0.1"
Line 6,669: Line 6,669:
homepage := Some(url("http://rosettacode.org/wiki/Abbreviations,_automatic#Scala"))
homepage := Some(url("http://rosettacode.org/wiki/Abbreviations,_automatic#Scala"))


libraryDependencies += "com.lihaoyi" %% "os-lib" % "0.3.0"</lang>
libraryDependencies += "com.lihaoyi" %% "os-lib" % "0.3.0"</syntaxhighlight>
====AbbreviationsAuto.scala====
====AbbreviationsAuto.scala====
<lang Scala>object AbbreviationsAuto extends App {
<syntaxhighlight lang=Scala>object AbbreviationsAuto extends App {
private val wd = os.pwd
private val wd = os.pwd


Line 6,693: Line 6,693:
.foreach(line => println(processLine(line)))
.foreach(line => println(processLine(line)))


}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:45ex"> ""
<pre style="height:45ex"> ""
Line 6,794: Line 6,794:


=={{header|SenseTalk}}==
=={{header|SenseTalk}}==
<lang sensetalk>function AbbreviationsAutomatic days
<syntaxhighlight lang=sensetalk>function AbbreviationsAutomatic days
put 1 into abbreviationLength
put 1 into abbreviationLength
put the number of items in days into len
put the number of items in days into len
Line 6,811: Line 6,811:
add 1 to abbreviationLength
add 1 to abbreviationLength
end repeat
end repeat
end AbbreviationsAutomatic</lang>
end AbbreviationsAutomatic</syntaxhighlight>
<lang sensetalk>put "Weekdays.txt" into myFile
<syntaxhighlight lang=sensetalk>put "Weekdays.txt" into myFile


put the defaultStringEncoding into savedEncoding
put the defaultStringEncoding into savedEncoding
Line 6,833: Line 6,833:
close file myFile
close file myFile


put savedEncoding into the defaultStringEncoding</lang>
put savedEncoding into the defaultStringEncoding</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 6,851: Line 6,851:


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl>
<syntaxhighlight lang=tcl>
set f [open abbreviations_automatic_weekdays.txt]
set f [open abbreviations_automatic_weekdays.txt]
set lines [split [read -nonewline $f] \n]
set lines [split [read -nonewline $f] \n]
Line 6,869: Line 6,869:
incr i
incr i
puts "$i $days"
puts "$i $days"
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
<pre>2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 6,886: Line 6,886:


=={{header|Transd}}==
=={{header|Transd}}==
<lang scheme>#lang transd
<syntaxhighlight lang=scheme>#lang transd


MainModule : {
MainModule : {
Line 6,898: Line 6,898:
(lout (+ len 1) " " days))
(lout (+ len 1) " " days))
) ) )
) ) )
}</lang>{{out}}
}</syntaxhighlight>{{out}}
<pre style="height:45ex">
<pre style="height:45ex">
2 [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
2 [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]
Line 7,002: Line 7,002:


=={{header|TSE SAL}}==
=={{header|TSE SAL}}==
<lang TSESAL>
<syntaxhighlight lang=TSESAL>
STRING PROC FNStringGetExpressionRegularCharacterMetaEscapeS( STRING inS )
STRING PROC FNStringGetExpressionRegularCharacterMetaEscapeS( STRING inS )
STRING s[255] = inS
STRING s[255] = inS
Line 7,099: Line 7,099:
GotoBufferId( bufferI )
GotoBufferId( bufferI )
END
END
</syntaxhighlight>
</lang>
{{out}} <pre>
{{out}} <pre>
2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 7,203: Line 7,203:
=={{header|VBA}}==
=={{header|VBA}}==
Function :
Function :
<lang vb>Function MinimalLenght(strLine As String) As Integer
<syntaxhighlight lang=vb>Function MinimalLenght(strLine As String) As Integer
Dim myVar As Variant, I As Integer, Flag As Boolean, myColl As Collection, Count As Integer
Dim myVar As Variant, I As Integer, Flag As Boolean, myColl As Collection, Count As Integer
myVar = Split(strLine, " ")
myVar = Split(strLine, " ")
Line 7,221: Line 7,221:
Loop While Not Flag
Loop While Not Flag
MinimalLenght = Count
MinimalLenght = Count
End Function</lang>
End Function</syntaxhighlight>
To call it :
To call it :
<lang vb>Sub Main_Abbr_Auto()
<syntaxhighlight lang=vb>Sub Main_Abbr_Auto()
Dim Nb As Integer, s As String, Result() As String, c As Integer
Dim Nb As Integer, s As String, Result() As String, c As Integer
Nb = FreeFile
Nb = FreeFile
Line 7,238: Line 7,238:
Wend
Wend
Close #Nb
Close #Nb
End Sub</lang>
End Sub</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:45ex">2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
<pre style="height:45ex">2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 7,340: Line 7,340:


=={{header|VBScript}}==
=={{header|VBScript}}==
<syntaxhighlight lang=vb>
<lang vb>
sub print(s) wscript.stdout.writeline s :end sub
sub print(s) wscript.stdout.writeline s :end sub


Line 7,378: Line 7,378:
f.close
f.close


</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 7,411: Line 7,411:
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
{{trans|C#}}
{{trans|C#}}
<lang vbnet>Module Module1
<syntaxhighlight lang=vbnet>Module Module1


Sub Main()
Sub Main()
Line 7,459: Line 7,459:
End Sub
End Sub


End Module</lang>
End Module</syntaxhighlight>
{{out}}
{{out}}
<pre> 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
<pre> 2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 7,562: Line 7,562:
=={{header|Vlang}}==
=={{header|Vlang}}==
{{trans|Go}}
{{trans|Go}}
<lang vlang>import os
<syntaxhighlight lang=vlang>import os
fn distinct_strings(strs []string) []string {
fn distinct_strings(strs []string) []string {
Line 7,620: Line 7,620:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 7,632: Line 7,632:
{{libheader|Wren-seq}}
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "io" for File
<syntaxhighlight lang=ecmascript>import "io" for File
import "/pattern" for Pattern
import "/pattern" for Pattern
import "/seq" for Lst
import "/seq" for Lst
Line 7,660: Line 7,660:
}
}
i = i + 1
i = i + 1
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 7,766: Line 7,766:


=={{header|Yabasic}}==
=={{header|Yabasic}}==
<lang Yabasic>
<syntaxhighlight lang=Yabasic>
a = open("days_of_week.txt", "r")
a = open("days_of_week.txt", "r")


Line 7,794: Line 7,794:
until(s)
until(s)
return d
return d
end sub</lang>
end sub</syntaxhighlight>


=={{header|zkl}}==
=={{header|zkl}}==
zkl doesn't grok UTF-8 so I'm using a byte by byte check. It works surprisingly well.
zkl doesn't grok UTF-8 so I'm using a byte by byte check. It works surprisingly well.
<lang zkl>nds:=File("daysOfWeek.txt").read().howza(11) // stripped lines
<syntaxhighlight lang=zkl>nds:=File("daysOfWeek.txt").read().howza(11) // stripped lines
.pump(List,Void.Filter,fcn(day){
.pump(List,Void.Filter,fcn(day){
d,N,m := day.split(),d.len(),(0).max(d.apply("len")); // N==7
d,N,m := day.split(),d.len(),(0).max(d.apply("len")); // N==7
Line 7,808: Line 7,808:
return(m,day); // no match nowhere
return(m,day); // no match nowhere
});
});
foreach n,s in (nds){ println("%3d %s".fmt(n,s)); }</lang>
foreach n,s in (nds){ println("%3d %s".fmt(n,s)); }</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:45ex">
<pre style="height:45ex">