Abbreviations, automatic: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 141:
{{trans|Python}}
 
<syntaxhighlight lang="11l">F shortest_abbreviation_length(line, list_size)
V words = line.split(‘ ’)
V word_count = words.len
Line 269:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang=AArch64"aarch64 Assemblyassembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program abbrAuto64.s */
Line 848:
 
=={{header|Ada}}==
<syntaxhighlight lang=Ada"ada">with Ada.Containers.Indefinite_Vectors;
with Ada.Strings.Fixed;
with Ada.Strings.Maps;
Line 1,024:
 
=={{header|Amazing Hopper}}==
<syntaxhighlight lang=Amazing"amazing Hopperhopper">
#include <jambo.h>
 
Line 1,066:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang=ARM"arm Assemblyassembly">
/* ARM assembly Raspberry PI */
/* program abbrAuto.s */
Line 1,592:
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang=AutoHotkey"autohotkey">AutoAbbreviations(line){
len := prev := 0
Days := StrSplit(line, " ")
Line 1,614:
return len
}</syntaxhighlight>
Examples:<syntaxhighlight lang=AutoHotkey"autohotkey">data := "
(
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Line 1,644:
 
=={{header|AWK}}==
<syntaxhighlight lang=AWK"awk">
# syntax: GAWK -f ABBREVIATIONS_AUTOMATIC.AWK ABBREVIATIONS_AUTOMATIC.TXT
{ dow_arr[NR] = $0 }
Line 1,801:
=={{header|C}}==
{{trans|C#}}
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 1,998:
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
 
Line 2,152:
=={{header|C++}}==
{{trans|C#}}
<syntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
#include <fstream>
Line 2,320:
=={{header|COBOL}}==
{{works with|GnuCOBOL|3.1.2.0}}
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. AUTO-ABBREVIATIONS.
 
Line 2,587:
=={{header|Common Lisp}}==
It uses the standard library split-sequence to split the string into words.
<syntaxhighlight lang="lisp">
(defun max-mismatch (list)
(if (cdr list)
Line 2,703:
=={{header|D}}==
{{trans|Kotlin}}
<syntaxhighlight lang=D"d">import std.conv;
import std.exception;
import std.range;
Line 2,846:
{{libheader| System.IOUtils}}
{{Trans|Kotlin}}
<syntaxhighlight lang=Delphi"delphi">
program Abbreviations_Automatic;
 
Line 2,941:
The output is of the set of abbreviations. These are not sorted correctly and some encoding errors remain.
 
<syntaxhighlight lang="erlang">
-module(abbreviateweekdays).
-export([ main/0 ]).
Line 3,011:
=={{header|F_Sharp|F#}}==
===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)
fN 0
</syntaxhighlight>
===The Task - Demonstrate the function===
<syntaxhighlight lang="fsharp">
fN ["Sunday"; "Monday"; "Tuesday"; "Wednesday"; "Thursday"; "Friday"; "Saturday"] // -> 2
fN ["Sondag"; "Maandag"; "Dinsdag"; "Woensdag"; "Donderdag"; "Vrydag"; "Saterdag"] // -> 2
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.
 
<syntaxhighlight lang="factor">USING: formatting io io.encodings.utf8 io.files kernel math
sequences sets splitting ;
IN: rosetta-code.abbreviations-automatic
Line 3,166:
=={{header|Go}}==
{{trans|Kotlin}}
<syntaxhighlight lang="go">package main
 
import(
Line 3,249:
=={{header|Groovy}}==
{{trans|Java}}
<syntaxhighlight lang="groovy">class Abbreviations {
static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("days_of_week.txt"), "utf-8"))
Line 3,395:
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">import Data.List (inits, intercalate, transpose)
import qualified Data.Set as S
 
Line 3,436:
 
=={{header|J}}==
<syntaxhighlight lang=J"j">NB. y is words in boxes
abbreviation_length =: monad define
N =. # y
Line 3,477:
=={{header|Java}}==
{{trans|D}}
<syntaxhighlight lang=Java"java">import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
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.
<syntaxhighlight lang="javascript">
Array.prototype.hasDoubles = function() {
let arr = this.slice();
Line 3,774:
 
{{Works with|MacOS JXA}}
<syntaxhighlight lang="javascript">(() => {
"use strict";
 
Line 3,948:
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">const text = """
Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Sondag Maandag Dinsdag Woensdag Donderdag Vrydag Saterdag
Line 4,092:
===Original O(''n<sup>2</sup>'') solution===
where ''n'' is the number of strings in each line.
<syntaxhighlight lang="scala">// version 1.1.4-3
 
import java.io.File
Line 4,226:
 
===Alternative O(''n log n'') solution based on sorting===
<syntaxhighlight lang="scala">import java.io.File
import kotlin.math.max
 
Line 4,364:
 
=={{header|Ksh}}==
<syntaxhighlight lang="ksh">
#!/bin/ksh
 
Line 4,455:
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">function split(line)
local wa = {}
for i in string.gmatch(line, "%S+") do
Line 4,607:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"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.*)
Abbreviations[maxLength_Integer][str_String]:=Array[StringTake[StringPadRight[str,maxLength,"_"],#]&,maxLength];
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===
<syntaxhighlight lang=Nim"nim">
import sets
import unicode
Line 4,776:
</syntaxhighlight>
===Using a sorted list===
<syntaxhighlight lang=Nim"nim">
import algorithm
import unicode
Line 4,914:
=={{header|Objeck}}==
{{trans|Java}}
<syntaxhighlight lang="objeck">use System.IO.File;
use Collection;
 
Line 5,086:
Output is the same as for Raku.
{{trans|Raku}}
<syntaxhighlight lang="perl">use strict;
use utf8;
binmode STDOUT, ":utf8";
Line 5,111:
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+).
<!--<syntaxhighlight lang=Phix"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>
Line 5,144:
=={{header|Picat}}==
{{works with|Picat}}
<syntaxhighlight lang=Picat"picat">
import util.
 
Line 5,284:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<syntaxhighlight lang="prolog">minimum_abbreviation_length(Day_names, Min_length):-
sort(Day_names, Sorted_names),
minimum_abbreviation_length(Sorted_names, Min_length, 1).
Line 5,442:
 
=={{header|PureBasic}}==
<syntaxhighlight lang=PureBasic"purebasic">EnableExplicit
#TZ="|"
#FZ="@"
Line 5,691:
{{works with|Python|3.6}}
{{trans|Kotlin}}
<syntaxhighlight lang="python">def shortest_abbreviation_length(line, list_size):
words = line.split()
word_count = len(words)
Line 5,825:
===Functional===
In terms of generators:
<syntaxhighlight lang="python">import operator
from itertools import (accumulate,
repeat)
Line 5,988:
The generic primitives are curried, allowing for more flexible composition:
{{Works with|Python|3}}
<syntaxhighlight lang="python">'''Automatic abbreviations'''
 
from itertools import (accumulate, chain)
Line 6,129:
=={{header|Racket}}==
 
<syntaxhighlight lang="racket">#lang racket
 
(require racket/set)
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.
 
<syntaxhighlight lang=perl6"raku" line>sub auto-abbreviate ( Str $string ) {
return Nil unless my @words = $string.words;
return $_ if @words».substr(0, $_).Set == @words for 1 .. @words».chars.max;
Line 6,273:
 
=={{header|Red}}==
<syntaxhighlight lang=Red"red">
Red []
;; read and convert data to a string - to char conversion is neccessary to avoid
Line 6,326:
 
=={{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*/
iFID= 'ABBREV_A.TAB' /*name of the file that has the table. */
Line 6,463:
=={{header|Ruby}}==
 
<syntaxhighlight lang="ruby">require "abbrev"
File.read("daynames.txt").each_line do |line|
Line 6,505:
 
=={{header|Rust}}==
<syntaxhighlight lang="c">/**
* Abbreviations from tintenalarm.de
*/
Line 6,663:
===Functional programmed===
====build.sbt====
<syntaxhighlight lang=Scala"scala">name := "Abbreviations-automatic"
scalaVersion := "2.13.0"
version := "0.1"
Line 6,671:
libraryDependencies += "com.lihaoyi" %% "os-lib" % "0.3.0"</syntaxhighlight>
====AbbreviationsAuto.scala====
<syntaxhighlight lang=Scala"scala">object AbbreviationsAuto extends App {
private val wd = os.pwd
 
Line 6,794:
 
=={{header|SenseTalk}}==
<syntaxhighlight lang="sensetalk">function AbbreviationsAutomatic days
put 1 into abbreviationLength
put the number of items in days into len
Line 6,812:
end repeat
end AbbreviationsAutomatic</syntaxhighlight>
<syntaxhighlight lang="sensetalk">put "Weekdays.txt" into myFile
 
put the defaultStringEncoding into savedEncoding
Line 6,851:
 
=={{header|Tcl}}==
<syntaxhighlight lang="tcl">
set f [open abbreviations_automatic_weekdays.txt]
set lines [split [read -nonewline $f] \n]
Line 6,886:
 
=={{header|Transd}}==
<syntaxhighlight lang="scheme">#lang transd
 
MainModule : {
Line 7,002:
 
=={{header|TSE SAL}}==
<syntaxhighlight lang=TSESAL"tsesal">
STRING PROC FNStringGetExpressionRegularCharacterMetaEscapeS( STRING inS )
STRING s[255] = inS
Line 7,203:
=={{header|VBA}}==
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
myVar = Split(strLine, " ")
Line 7,223:
End Function</syntaxhighlight>
To call it :
<syntaxhighlight lang="vb">Sub Main_Abbr_Auto()
Dim Nb As Integer, s As String, Result() As String, c As Integer
Nb = FreeFile
Line 7,340:
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
sub print(s) wscript.stdout.writeline s :end sub
 
Line 7,411:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<syntaxhighlight lang="vbnet">Module Module1
 
Sub Main()
Line 7,562:
=={{header|Vlang}}==
{{trans|Go}}
<syntaxhighlight lang="vlang">import os
fn distinct_strings(strs []string) []string {
Line 7,632:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascript">import "io" for File
import "/pattern" for Pattern
import "/seq" for Lst
Line 7,766:
 
=={{header|Yabasic}}==
<syntaxhighlight lang=Yabasic"yabasic">
a = open("days_of_week.txt", "r")
 
Line 7,798:
=={{header|zkl}}==
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){
d,N,m := day.split(),d.len(),(0).max(d.apply("len")); // N==7
10,333

edits