gpt4 book ai didi

java - Java 中 MetaMap 的正则表达式

转载 作者:行者123 更新时间:2023-12-01 17:39:33 25 4
gpt4 key购买 nike

MetaMap 文件具有以下行:

mappings([map(-1000,[ev(-1000,'C0018017','Objective','Goals',[objective],[inpr],[[[1,1],[1,1],0]],yes,no)])]).

格式解释为

mappings(
[map(negated overall score for this mapping,
[ev(negated candidate score,'UMLS concept ID','UMLS concept','preferred name for concept - may or may not be different',
[matched word or words lowercased that this candidate matches in the phrase - comma separated list],
[semantic type(s) - comma separated list],
[match map list - see below],candidate involved with head of phrase - yes or no,
is this an overmatch - yes or no
)
]
)
]
).

我想在 java 中运行 RegEx 查询,它为我提供字符串“UMLS 概念 ID”、语义类型和匹配映射列表。RegEx 是正确的工具吗?或者在 Java 中实现此目的最有效的方法是什么?

最佳答案

这是我对正则表达式解决方案的尝试。我正在尝试这种replace“元正则表达式”方法;我希望它能读取出更具可读性的代码。

String line = "mappings([map(-1000,[ev(-1000,'C0018017','Objective','Goals',[objective],[inpr],[[[1,1],[1,1],0]],yes,no)])]).";
String regex =
"mappings([map(number,[ev(number,<quoted>,quoted,quoted,[csv],[<csv>],[<matchmap>],yesno,yesno)])])."
.replaceAll("([\\.\\(\\)\\[\\]])", "\\\\$1") // escape metacharacters
.replace("<", "(").replace(">", ")") // set up capture groups
.replace("number", "-?\\d+")
.replace("quoted", "'[^']*'")
.replace("yesno", "(?:yes|no)")
.replace("csv", "[^\\]]*")
.replace("matchmap", ".*?")
;
System.out.println(regex);
// prints "mappings\(\[map\(-?\d+,\[ev\(-?\d+,('[^']*'),'[^']*','[^']*',\[[^\]]*\],\[([^\]]*)\],\[(.*?)\],(?:yes|no),(?:yes|no)\)\]\)\]\)\."

Matcher m = Pattern.compile(regex).matcher(line);
if (m.find()) {
System.out.println(m.group(1)); // prints "'C0018017'"
System.out.println(m.group(2)); // prints "inpr"
System.out.println(m.group(3)); // prints "[[1,1],[1,1],0]"
}

replace元正则表达式允许您通过设置适当的replace轻松地容纳符号之间的空格(而不是将其全部散布成一团难以阅读的困惑)。

关于java - Java 中 MetaMap 的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2728910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com