Bahasa apa ini?

16

Anda harus menulis program atau fungsi yang menentukan bahasa kata yang diberikan.

Tugasnya adalah mengenali beberapa dari 5000 kata yang paling umum dalam 4 bahasa:

  • Inggris
  • Jerman
  • Italia
  • Hongaria

Daftar kata dapat ditemukan di repositori GitHub ini.

Kamu adalah diizinkan untuk membuat kesalahan dalam 40% dari kasus uji yang disediakan . Yaitu Anda dapat salah mengkategorikan 8000 dari 20.000 input.

Detail

  • Daftar hanya berisi kata-kata dengan huruf kecil a-zjadi misalnya won'tdanmöchte tidak termasuk.
  • Beberapa kata muncul dalam berbagai bahasa yang berarti kode Anda tidak selalu dapat menebak hasil yang diharapkan dengan benar.
  • Untuk kenyamanan Anda dapat mengunduh semua kotak uji sebagai satu daftar . Di setiap baris angka menunjukkan bahasa kata. ( 1untuk bahasa Inggris, 2untuk Jerman, 3untuk Italia dan 4untuk Hongaria.)
  • Celah standar tidak diijinkan.
  • Menggunakan kata mencantumkan data serupa kami yang disediakan oleh bahasa pemrograman Anda dilarang.

Memasukkan

  • String yang hanya berisi huruf Inggris (az) kecil.
  • Mengejar baris baru adalah opsional.

Keluaran

  • Anda dapat mengategorikan kata-kata dengan memberikan output yang berbeda dan konsisten (selalu sama) untuk setiap bahasa. (Misalnya 1untuk Bahasa Inggris, 2untuk Bahasa Jerman, 3untuk Bahasa Italia dan 4untuk Bahasa Hongaria.)

Ini adalah kode golf sehingga program atau fungsi terpendek menang.

Pertanyaan terkait kode golf: Apakah ini sebuah kata?

Daftar kata diambil dari wiktionary.org dan 101languages.net.

randomra
sumber
apakah Anda yakin daftarnya benar? Saya cukup yakin saya tidak pernah mendengar bahasa Jerman. Apakah mengeluarkan array dengan semua bahasa yang mungkin diperhitungkan? misalnya rupanya dalam semua bahasa sehingga akan menempatkan {1,2,3,4}
Eumel
@Eumel Pasangan pertama kata-kata bahasa Inggris mungkin ada di suatu tempat di daftar lain karena mungkin ada frasa bahasa Inggris dalam teks-teks bahasa yang digunakan untuk menghasilkan daftar kata. Anda dapat mengategorikan input menjadi hanya satu bahasa. (Whihch berarti seperti yang disebutkan dalam pertanyaan bahwa "kode Anda tidak selalu dapat menebak output yang diharapkan dengan benar".)
randomra
Daftar hanya berisi kata-kata dengan huruf kecil ... Itu tidak sepenuhnya benar. The all_languagesfile termasuk puluhan kata dikapitalisasi ( Mr, Gutenberg, dll) dan non-kata "" (string kosong) dan "]] | -". Saya menganggap tidak apa-apa untuk mengurangi yang pertama dan menghapus yang terakhir?
ossifrage pelit
@squeamishossifrage Terima kasih atas tangkapannya. Diperbarui daftar bahasa Inggris. (Ada ~ 60 huruf besar dan 2 non-kata.)
randomra
Mengapa menghapus diakritik? Jika tujuannya adalah untuk membedakan bahasa yang tidak memiliki diakritik, lalu mengapa tidak menggunakan bahasa yang tidak memiliki diakritik?
tepuk

Jawaban:

9

Retina , 51 byte

.*[aeio]$
1
A`en$|ch|ei|au
^$
2
A`[jkz]|gy|m$
\D+
4

Saya datang dengan regex dan @ MartinBüttner melakukan konversi ke / golf di Retina jadi ... hore untuk upaya tim?

Pemetaannya adalah 1 -> Italian, 2 -> German, (empty) -> Hungarian, 4 -> English, dengan jumlah yang diklasifikasikan dalam setiap kategori 4506 + 1852 + 2092 + 3560 = 12010.

Cobalah online! | Versi multiline yang dimodifikasi

Penjelasan

Pertama, setara dengan Python adalah seperti ini:

import re
def f(s):
  if re.search("[aeio]$", s):
    return 1
  if re.search("en$|ch|ei|au", s):
    return 2
  if re.search("[jkz]|gy|m$", s):
    return ""
  return 4

Mari saya hanya mengatakan bahwa o$adalah sangat baik indikator yang dari bahasa Italia.

Versi Retina serupa, dengan pasangan garis membentuk tahap penggantian. Misalnya, dua baris pertama

.*[aeio]$
1

mengganti kecocokan dari baris pertama dengan isi baris kedua.

Tiga baris berikutnya melakukan hal yang sama, tetapi menggunakan mode anti-grep Retina - anti-grep (ditentukan dengan A`) menghapus baris jika cocok dengan regex yang diberikan, dan dua baris berikut adalah penggantian dari baris kosong ke output yang diinginkan.

A`en$|ch|ei|au
^$
2

Baris berikut menggunakan anti-grep lagi, tetapi tidak menggantikan baris kosong, memberikan output tetap untuk Hongaria.

A`[jkz]|gy|m$

Akhirnya, dua baris terakhir

\D+
4

menggantikan baris non-kosong dengan 4. Semua pergantian hanya dapat terjadi jika tidak ada pergantian sebelumnya diaktifkan, mensimulasikan if/else ifrantai.

Sp3000
sumber
1

LabVIEW, 29 Primview LabVIEW Primview dan 148,950 Bytes

siklus melalui bahasa dan menempatkan iterator ke dalam array jika kata itu ada. Ini akan diperiksa oleh loop dalam, memilih garis h dan melakukan= . Dalam LabVIEW, itu hanya memberikan kebenaran jika String sama persis.

Sekarang ambil elemen pertama dari array output sehingga bahasa Inggris mengambil alih sisanya.

Output untuk saat ini adalah 0untuk bahasa Inggris, 1untuk Jerman, 2untuk Italia dan 3untuk Hongaria.

Eumel
sumber
Saya tidak akrab dengan LabView, tetapi bagaimana Anda menyimpan nilai-nilai (daftar kata) dan bagaimana mereka tercermin dalam Primitif LabView? Entri meta mengatakan: " Konstanta: string adalah 1 LabVIEW Primitive per karakter ". Bukankah itu sangat meningkatkan jumlah Primitive?
masukkan nama pengguna di sini
Saya memuat dari file yang merupakan path direktori + membangun path dengan string + load file. Penyimpanan dilakukan secara internal dan dilewatkan oleh kabel.
Eumel
5
Saya mungkin salah, tapi saya pikir inti dari tantangannya adalah tentang bagaimana menekan / menyimpan daftar kata. Jadi memuat dari file eksternal mungkin dilarang. Akan bertanya pada OP tentang itu. :)
masukkan nama pengguna di sini
2
Jika Anda menggunakan file eksternal, ukurannya harus ditambahkan ke ukuran kode Anda karena itu adalah bagian dari solusi Anda.
randomra
Saya mendapat kesan bahwa mereka seharusnya diberi tetapi tidak menambahkannya np
Eumel
1

Java, 3416 byte, 62%

ini solusi saya, saya menganalisis daftar kata-kata yang diberikan dan menemukan 60 bigrams dan trigram yang paling umum untuk setiap bahasa. Sekarang saya memeriksa n-gram saya terhadap kata, dan memilih bahasa dengan n-gram terbanyak dalam kata.

public class Classificator {

    String[][] triGr = {
            {"ing","ion","ent","tio","ted","nce","ter","res","ati","con","ess","ate","pro","ain","est","ons","men","ect","red","rea","com","ere","ers","nte","ine","her","ble","ist","tin","for","per","der","ear","str","ght","pre","ver","int","nde","the","igh","ive","sta","ure","end","enc","ned","ste","dis","ous","all","and","anc","ant","oun","ten","tra","are","sed","cti"},
            {"sch","che","ver","gen","ten","cht","ich","ein","ste","ter","hen","nde","nge","ach","ere","ung","den","sse","ers","and","eit","ier","ren","sen","ges","ang","ben","rei","est","nen","nte","men","aus","der","ent","hei","her","lle","ern","ert","uch","ine","ehe","auf","lie","tte","ige","ing","hte","mme","end","wei","len","hre","rau","ite","bes","ken","cha","ebe"},
            {"ent","are","ato","nte","ett","ere","ion","chi","con","one","men","nti","gli","pre","ess","att","tto","par","per","sta","tra","zio","and","iam","end","ter","res","est","nto","tta","acc","sci","cia","ver","ndo","amo","ant","str","tro","ssi","pro","era","eri","nta","der","ate","ort","com","man","tor","rat","ell","ale","gio","ont","col","tti","ano","ore","ist"},
            {"sze","ere","meg","ett","gye","ele","ond","egy","enn","ott","tte","ete","unk","ban","tem","agy","zer","esz","tet","ara","nek","hal","dol","mon","art","ala","ato","szt","len","men","ben","kap","ent","min","ndo","eze","sza","isz","fog","kez","ind","ten","tam","nak","fel","ene","all","asz","gon","mar","zem","szo","tek","zet","elm","het","eve","ssz","hat","ell"}

                    };
    static String[][] biGr = {
        {"in","ed","re","er","es","en","on","te","ng","st","nt","ti","ar","le","an","se","de","at","ea","co","ri","ce","or","io","al","is","it","ne","ra","ro","ou","ve","me","nd","el","li","he","ly","si","pr","ur","th","di","pe","la","ta","ss","ns","nc","ll","ec","tr","as","ai","ic","il","us","ch","un","ct"},
        {"en","er","ch","te","ge","ei","st","an","re","in","he","ie","be","sc","de","es","le","au","se","ne","el","ng","nd","un","ra","ar","nt","ve","ic","et","me","ri","li","ss","it","ht","ha","la","is","al","eh","ll","we","or","ke","fe","us","rt","ig","on","ma","ti","nn","ac","rs","at","eg","ta","ck","ol"},
        {"re","er","to","ar","en","te","ta","at","an","nt","ra","ri","co","on","ti","ia","or","io","in","st","tt","ca","es","ro","ci","di","li","no","ma","al","am","ne","me","le","sc","ve","sa","si","tr","nd","se","pa","ss","et","ic","na","pe","de","pr","ol","mo","do","so","it","la","ce","ie","is","mi","cc"},
        {"el","en","sz","te","et","er","an","me","ta","on","al","ar","ha","le","gy","eg","re","ze","em","ol","at","ek","es","tt","ke","ni","la","ra","ne","ve","nd","ak","ka","in","am","ad","ye","is","ok","ba","na","ma","ed","to","mi","do","om","be","se","ag","as","ez","ot","ko","or","cs","he","ll","nn","ny"}

                    };

    public int guess(String word) {

        if (word.length() < 3) {
            return 4; // most words below 2 characters on list are hungarians
        }
        int score[] = { 0, 0, 0, 0 };
        for (int i = 0; i < 4; i++) {
            for (String s : triGr[i]) {
                if (word.contains(s)) {
                    score[i] = score[i] + 2;
                }
            }
            for (String s : biGr[i]) {
                if (word.contains(s)) {
                    score[i] = score[i] + 1;
                }
            }
        }
        int v = -1;
        int max = 0;
        for (int i = 0; i < 4; i++) {
            if (score[i] > max) {
                max = score[i];
                v = i;
            }
        }
        v++;
        return v==0?Math.round(4)+1:v;
    }
}

dan ini adalah testcase saya

public class Test {

    Map<String, List<Integer>> words = new HashMap<String, List<Integer>>();

    boolean validate(String word, Integer lang) {
        List<Integer> langs = words.get(word);
        return langs.contains(lang);
    }

    public static void main(String[] args) throws FileNotFoundException {

        FileReader reader = new FileReader("list.txt");
        BufferedReader buf = new BufferedReader(reader);
        Classificator cl = new Classificator();
        Test test = new Test();
        buf.lines().forEach(x -> test.process(x));
        int guess = 0, words = 0;
        for (String word : test.words.keySet()) {
            int lang = cl.guess(word);
            if (lang==0){
                continue;
            }
            boolean result = test.validate(word, lang);
            words++;
            if (result) {
                guess++;
            }
        }
        System.out.println(guess+ " "+words+ "    "+(guess*100f/words));
    }

    private void process(String x) {
        String arr[] = x.split("\\s+");
        String word = arr[0].trim();
        List<Integer> langs = words.get(word);
        if (langs == null) {
            langs = new ArrayList<Integer>();
            words.put(word, langs);
        }
        langs.add(Integer.parseInt(arr[1].trim()));

    }

}
pengguna902383
sumber