Abbreviations, automatic: Difference between revisions
Content added Content deleted
Alpha bravo (talk | contribs) (Added AutoHotkey) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 141: | Line 141: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<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)</ |
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}} |
||
< |
<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}}== |
||
< |
<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;</ |
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}}== |
||
< |
<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}} |
||
< |
<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}}== |
||
< |
<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 |
||
}</ |
}</syntaxhighlight> |
||
Examples:< |
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</ |
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}}== |
||
< |
<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#}} |
||
< |
<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; |
||
}</ |
}</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#}}== |
||
< |
<syntaxhighlight lang=csharp>using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
Line 2,049: | Line 2,049: | ||
} |
} |
||
} |
} |
||
}</ |
}</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#}} |
||
< |
<syntaxhighlight lang=cpp>#include <iomanip> |
||
#include <iostream> |
#include <iostream> |
||
#include <fstream> |
#include <fstream> |
||
Line 2,217: | Line 2,217: | ||
return 0; |
return 0; |
||
}</ |
}</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}} |
||
< |
<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. |
||
< |
<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}} |
||
< |
<syntaxhighlight lang=D>import std.conv; |
||
import std.exception; |
import std.exception; |
||
import std.range; |
import std.range; |
||
Line 2,738: | Line 2,738: | ||
} |
} |
||
} |
} |
||
}</ |
}</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}} |
||
< |
<syntaxhighlight lang=Delphi> |
||
program Abbreviations_Automatic; |
program Abbreviations_Automatic; |
||
Line 2,934: | Line 2,934: | ||
end; |
end; |
||
Readln; |
Readln; |
||
end.</ |
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. |
||
< |
<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=== |
||
< |
<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=== |
||
< |
<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. |
||
< |
<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</ |
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}} |
||
< |
<syntaxhighlight lang=go>package main |
||
import( |
import( |
||
Line 3,240: | Line 3,240: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,249: | Line 3,249: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<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: | ||
} |
} |
||
} |
} |
||
}</ |
}</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}}== |
||
< |
<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</ |
<$> 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}}== |
||
< |
<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 |
||
)</ |
)</syntaxhighlight> |
||
<pre> |
<pre> |
||
Line 3,477: | Line 3,477: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<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: | ||
} |
} |
||
} |
} |
||
}</ |
}</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. |
||
< |
<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}} |
||
< |
<syntaxhighlight lang=javascript>(() => { |
||
"use strict"; |
"use strict"; |
||
Line 3,934: | Line 3,934: | ||
// MAIN --- |
// MAIN --- |
||
return main(); |
return main(); |
||
})();</ |
})();</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}}== |
||
< |
<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) |
||
</ |
</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. |
||
< |
<syntaxhighlight lang=scala>// version 1.1.4-3 |
||
import java.io.File |
import java.io.File |
||
Line 4,120: | Line 4,120: | ||
} |
} |
||
} |
} |
||
}</ |
}</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=== |
||
< |
<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) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,364: | Line 4,364: | ||
=={{header|Ksh}}== |
=={{header|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</ |
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}}== |
||
< |
<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</ |
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}}== |
||
< |
<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=== |
||
< |
<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=== |
||
< |
<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}} |
||
< |
<syntaxhighlight lang=objeck>use System.IO.File; |
||
use Collection; |
use Collection; |
||
Line 4,979: | Line 4,979: | ||
}; |
}; |
||
} |
} |
||
}</ |
}</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}} |
||
< |
<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($_) . ' ' . $_; |
||
}</ |
}</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+). |
||
<!--< |
<!--<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> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 5,144: | Line 5,144: | ||
=={{header|Picat}}== |
=={{header|Picat}}== |
||
{{works with|Picat}} |
{{works with|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}} |
||
< |
<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").</ |
process_file("days_of_week.txt").</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 5,442: | Line 5,442: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<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</ |
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}} |
||
< |
<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)</ |
automatic_abbreviations('daysOfWeek.txt', 7)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 5,825: | Line 5,825: | ||
===Functional=== |
===Functional=== |
||
In terms of generators: |
In terms of generators: |
||
< |
<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()</ |
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}} |
||
< |
<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()</ |
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}}== |
||
< |
<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)))</ |
(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. |
||
< |
<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;</ |
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}}== |
||
< |
<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}}== |
||
< |
<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</ |
return m</syntaxhighlight> |
||
{{out|output|text= when using the default input file containing the complete list of words:}} |
{{out|output|text= 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}}== |
||
< |
<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}}== |
||
< |
<syntaxhighlight lang=c>/** |
||
* Abbreviations from tintenalarm.de |
* Abbreviations from tintenalarm.de |
||
*/ |
*/ |
||
Line 6,557: | Line 6,557: | ||
} |
} |
||
num |
num |
||
}</ |
}</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==== |
||
< |
<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"</ |
libraryDependencies += "com.lihaoyi" %% "os-lib" % "0.3.0"</syntaxhighlight> |
||
====AbbreviationsAuto.scala==== |
====AbbreviationsAuto.scala==== |
||
< |
<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))) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="height:45ex"> "" |
<pre style="height:45ex"> "" |
||
Line 6,794: | Line 6,794: | ||
=={{header|SenseTalk}}== |
=={{header|SenseTalk}}== |
||
< |
<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</ |
end AbbreviationsAutomatic</syntaxhighlight> |
||
< |
<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</ |
put savedEncoding into the defaultStringEncoding</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 6,851: | Line 6,851: | ||
=={{header|Tcl}}== |
=={{header|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" |
||
}</ |
}</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}}== |
||
< |
<syntaxhighlight lang=scheme>#lang transd |
||
MainModule : { |
MainModule : { |
||
Line 6,898: | Line 6,898: | ||
(lout (+ len 1) " " days)) |
(lout (+ len 1) " " days)) |
||
) ) ) |
) ) ) |
||
}</ |
}</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}}== |
||
< |
<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 : |
||
< |
<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</ |
End Function</syntaxhighlight> |
||
To call it : |
To call it : |
||
< |
<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</ |
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#}} |
||
< |
<syntaxhighlight lang=vbnet>Module Module1 |
||
Sub Main() |
Sub Main() |
||
Line 7,459: | Line 7,459: | ||
End Sub |
End Sub |
||
End Module</ |
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}} |
||
< |
<syntaxhighlight lang=vlang>import os |
||
fn distinct_strings(strs []string) []string { |
fn distinct_strings(strs []string) []string { |
||
Line 7,620: | Line 7,620: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 7,632: | Line 7,632: | ||
{{libheader|Wren-seq}} |
{{libheader|Wren-seq}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<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 |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 7,766: | Line 7,766: | ||
=={{header|Yabasic}}== |
=={{header|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</ |
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. |
||
< |
<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)); }</ |
foreach n,s in (nds){ println("%3d %s".fmt(n,s)); }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="height:45ex"> |
<pre style="height:45ex"> |