我目前正在尝试进行拼写检查,但是在处理某些案件时遇到了一些麻烦。
例如,给定字符串:{else“-but},我的拼写检查会自动将其读取为无效单词。但是,由于else和but都正确拼写,因此我不想将其标记为不正确
有什么办法可以用正则表达式做到这一点? 我遇到的一个更常见的情况是诸如“背心口袋”之类的东西。
答案 0 :(得分:1)
您应该使用unicode分词,而不是正则表达式。使用uuseg和uucp库,您可以提取单词并使用
过滤单词边界let is_alphaword =
let alphachar = function
| `Malformed _ -> false
| `Uchar x ->
match Uucp.Break.word x with
| `LE | `Extend -> true
| _ -> false
in
Uutf.String.fold_utf_8 (fun acc _ x -> acc && alphachar x) true
(* Note that we are supposing strings to be utf-8 encoded *)
let words s =
let cons l x = if is_alphaword x then x :: l else l in
List.rev (Uuseg_string.fold_utf_8 `Word cons [] s)
此功能将字符串逐字分割:
words "else\"--but";;
- : string list = ["else"; "but"]
words "waistcoat-pocket";;
- : string list = ["waistcoat"; "pocket"]
并且可以在更一般的上下文中正常工作
words "आ तवेता नि षीदतेन्द्रमभि पर गायत";;
- : string list =
["आ"; "तवेता"; "नि"; "षीदतेन्द्रमभि";
"पर"; "गायत"]
或
words "Étoile(de Barnard)";;
- : string list = ["Étoile"; "de"; "Barnard"]