Oto kod w Mathematica Zacząłem rozwijać najnowszy kodowany golf.
Jest to algorytm rekursywny o minimalnym dopasowaniu, nie chciwy. Oznacza to, że zdanie „pióro jest mighter niż miecz” (bez spacji) zwraca { "pióro jest potęga er niż miecz} :)
findAll[s_] :=
Module[{a = s, b = "", c, sy = "="},
While[
StringLength[a] != 0,
j = "";
While[(c = findFirst[a]) == {} && StringLength[a] != 0,
j = j <> StringTake[a, 1];
sy = "~";
a = StringDrop[a, 1];
];
b = b <> " " <> j ;
If[c != {},
b = b <> " " <> c[[1]];
a = StringDrop[a, StringLength[c[[1]]]];
];
];
Return[{StringTrim[StringReplace[b, " " -> " "]], sy}];
]
findFirst[s_] :=
If[s != "" && (c = DictionaryLookup[s]) == {},
findFirst[StringDrop[s, -1]], Return[c]];
Wejście Próbka
ss = {"twodreamstop",
"onebackstop",
"butterfingers",
"dependentrelationship",
"payperiodmatchcode",
"labordistributioncodedesc",
"benefitcalcrulecodedesc",
"psaddresstype",
"ageconrolnoticeperiod",
"month05",
"as_benefits",
"fname"}
Wyjście
twodreamstop = two dreams top
onebackstop = one backstop
butterfingers = butterfingers
dependentrelationship = dependent relationship
payperiodmatchcode = pay period match code
labordistributioncodedesc ~ labor distribution coded es c
benefitcalcrulecodedesc ~ benefit c a lc rule coded es c
psaddresstype ~ p sad dress type
ageconrolnoticeperiod ~ age con rol notice period
month05 ~ month 05
as_benefits ~ as _ benefits
fname ~ f name
HTH
możliwy duplikat [Justadistraction: tokenizing English without whitespaces. Murakami SheepMan] (http://stackoverflow.com/questions/3851723/justadistraction-tokenizing-angielski- bez-białychprzestrzeni-murakami-sheepman) –
Bez jakiejś semantycznej wiedzy zawsze będą możliwe duplikaty. Rozważmy "ichon" = "żelazko" = "ich na" –