Đây là một mã trong Mathematica Tôi bắt đầu phát triển cho một sân golf mã gần đây.
Đó là thuật toán tối thiểu, không tham lam, đệ quy. Điều đó có nghĩa rằng câu "bút là mighter hơn thanh kiếm" (không có khoảng trắng) trả về { "bút là sức er hơn sword} :)
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]];
Sample Input
ss = {"twodreamstop",
"onebackstop",
"butterfingers",
"dependentrelationship",
"payperiodmatchcode",
"labordistributioncodedesc",
"benefitcalcrulecodedesc",
"psaddresstype",
"ageconrolnoticeperiod",
"month05",
"as_benefits",
"fname"}
Output
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
Nguồn
2010-10-11 02:56:43
bản sao có thể có của [Justadistraction: tokenizing English without whitespaces. Murakami SheepMan] (http://stackoverflow.com/questions/3851723/justadistraction-tokenizing-english-without-whitespaces-murakami-sheepman) –
Nếu không có một số kiến thức ngữ nghĩa sẽ luôn luôn có thể trùng lặp. Hãy xem xét "theiron" = "sắt" = "của họ trên" –