如何在可区分联合中使用符号/标点字符
我正在尝试为自然语言解析器返回的词性标签和其他标签创建一个可区分的联合。
在 C#/Java 中,通常使用字符串或枚举来表示这些值,但在 F# 中,可区分联合似乎更合适,因为它们是不同的只读值。
在语言参考中,我发现这个符号 <代码> ``...`` 可用于分隔关键字/保留字。这适用于
type ArgumentType =
| A0 // 主题
| A1 // 间接对象
| A2 // 直接对象
| A3 //
| A4 //
| A5 //
| AA //
| ``AM-ADV``
但是,标签包含 $ 等符号,例如
type PosTag =
| CC // 并列连词
| CD // 基数
| DT // 限定符
| EX // 那里存在
| FW // 外来词
| IN // 介词或从属连词
| JJ // 形容词
| JJR // 形容词,比较级
| JJS // 形容词,最高级
| LS // 列表项目标记
| MD // 模态
| NN // 名词,单数或众数
| NNP // 专有名词,单数
| NNPS // 专有名词,复数
| NNS // 名词,复数
| PDT // 预定器
| POS // 所有格词尾
| PRP // 人称代词
| PRP$ //$ 所有格代词
| RB // 副词
| RBR // 副词,比较级
| RBS // 副词,最高级
| RP // 粒子
| SYM // 符号
|至 // 至
|呃 // 感叹词
| VB // 动词,基本形式
| VBD // 动词,过去式
| VBG // 动词、动名词或现在分词
| VBN // 动词,过去分词
| VBP // 动词,非第三人称单数现在时
| VBZ // 动词,第三人称单数现在时
| WDT // Wh 决定器
| WP // Wh 代词
| WP$ //$ 所有格 wh 代词
| WRB // Wh 副词
| ``#``
| ``$``
| ``''``
| ``(``
| ``)``
| ``,``
| ``。``
| ``:``
| `` //不知道如何转义/分隔这个
> <代码> ``...`` 不适用于 WP$ 或类似 (
另外,我有一个有趣的问题,解析器返回 `` 作为有意义的符号,所以我也需要转义它。
还有其他方法可以做到这一点,或者这对于受歧视的联合来说是不可能的吗?
现在我收到诸如
- 无效的命名空间、模块、类型或联合案例名称
- 受歧视的联合案例和异常标签必须是大写标识符之类的
错误我想我可以以某种方式覆盖这些愚蠢的 toString情况并用一些等效的字母数字替换符号?
I'm trying to create a discriminated union for part of speech tags and other labels returned by a natural language parser.
It's common to use either strings or enums for these in C#/Java, but discriminated unions seem more appropriate in F# because these are distinct, read-only values.
In the language reference, I found that this symbol
can be used to delimit keywords/reserved words. This works for
``...``
type ArgumentType =
| A0 // subject
| A1 // indirect object
| A2 // direct object
| A3 //
| A4 //
| A5 //
| AA //
| ``AM-ADV``
However, the tags contain symbols like $, e.g.
type PosTag =
| CC // Coordinating conjunction
| CD // Cardinal Number
| DT // Determiner
| EX // Existential there
| FW // Foreign Word
| IN // Preposision or subordinating conjunction
| JJ // Adjective
| JJR // Adjective, comparative
| JJS // Adjective, superlative
| LS // List Item Marker
| MD // Modal
| NN // Noun, singular or mass
| NNP // Proper Noun, singular
| NNPS // Proper Noun, plural
| NNS // Noun, plural
| PDT // Predeterminer
| POS // Possessive Ending
| PRP // Personal Pronoun
| PRP$ //$ Possessive Pronoun
| RB // Adverb
| RBR // Adverb, comparative
| RBS // Adverb, superlative
| RP // Particle
| SYM // Symbol
| TO // to
| UH // Interjection
| VB // Verb, base form
| VBD // Verb, past tense
| VBG // Verb, gerund or persent participle
| VBN // Verb, past participle
| VBP // Verb, non-3rd person singular present
| VBZ // Verb, 3rd person singular present
| WDT // Wh-determiner
| WP // Wh-pronoun
| WP$ //$ Possessive wh-pronoun
| WRB // Wh-adverb
| ``#``
| ``$``
| ``''``
| ``(``
| ``)``
| ``,``
| ``.``
| ``:``
| `` //not sure how to escape/delimit this
isn't working for WP$ or symbols like (
``...``
Also, I have the interesting problem that the parser returns `` as a meaningful symbol, so I need to escape it as well.
Is there some other way to do this, or is this just not possible with a discriminated union?
Right now I'm getting errors like
- Invalid namespace, module, type or union case name
- Discriminated union cases and exception labels must be uppercase identifiers
I suppose I could somehow override toString for these goofy cases and replace the symbols with some alphanumeric equivalent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该规范似乎不清楚在什么情况下允许在双反引号中转义哪些字符。
我认为您最好的选择是对 DU 案例使用标准标识符,并按照您的建议覆盖
ToString
。The spec doesn't seem clear about what characters are allowed to be escaped in double-backticks in what contexts.
I think your best bet is to use standard identifiers for the DU cases, and override
ToString
as you suggest.根据我的经验,双反引号标识符仅在 let 绑定或类型成员中得到完全支持。因此,这意味着您可以在其中放置任何字符序列(除了为 F# codegen 保留的
@
字符)。当您想在模块、类型或 DU 案例定义中使用它们作为标识符时,它的效果不太好,因为某些字符不受支持。
例如
.
、/
、*
、+
、$
、[
、]
、\
或&
生成“无效的命名空间、模块、类型或联合案例名称”错误。From my experience, double-backtick marks identifiers are/seem to be fully supported only in let Bindings or type members. So that means you can put about any sequence of characters inside (excepting the
@
character which is reserved for F# codegen).When you want to use them as identifiers in module, type or DU cases definition, it doesn't play as nice since some characters are not supported.
E.g.
.
,/
,*
,+
,$
,[
,]
,\
or&
generate an "Invalid namespace, module, type or union case name" error.