【问题标题】:Convert string into list of words将字符串转换为单词列表
【发布时间】:2018-03-16 01:25:34
【问题描述】:

如何传递字符串并将其转换为 sml 中的单词列表?

例如:"one two three"["one", "two", "three"]

【问题讨论】:

标签: sml smlnj


【解决方案1】:

您可以(并且可能应该)使用String.tokens

- String.tokens Char.isSpace "one two three";
> val it = ["one", "two", "three"] : string list

还有String.fields。它们在处理连续/多余分隔符的方式上有所不同:

- String.tokens Char.isSpace "  one  two  three  ";
> val it = ["one", "two", "three"] : string list
- String.fields Char.isSpace "  one  two  three  ";
> val it = ["", "", "one", "", "two", "", "three", "", ""] : string list

如果您的字符串有多个潜在的分隔符并且您只对单词感兴趣:

fun isWordSep c = Char.isSpace c orelse
                ( Char.isPunct c andalso c <> #"-" andalso c <> #"'" )
val words = String.tokens isWordSep

这适用于单词是什么的一种定义:

- words "I'm jolly-good.  Are you?";
> val it = ["I'm", "jolly-good", "Are", "you"] : string list

并非所有自然语言都会遵守此定义,例如e.g. 是一个首字母缩写词,而不是两个词,eg。对于任何准确性,您都将进入自然语言处理领域。

【讨论】:

    猜你喜欢
    • 2011-09-05
    • 2020-04-02
    • 2012-01-18
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 2018-01-08
    • 2019-05-19
    • 1970-01-01
    相关资源
    最近更新 更多