java为SUTime添加用于解析季度的自定义规则
我遵循the official instructions为财政年度季度(像第一季度、第二季度、第三季度和第四季度)添加自定义SUTime规则
我使用默认的defs.sutime.txt
和english.sutime.txt
作为我自己规则文件的模板
将以下代码附加到mydefs.sutime.txt
// Financial Quarters
FYQ1 = {
type: QUARTER_OF_YEAR,
label: "FYQ1",
value: TimeWithRange(TimeRange(IsoDate(ANY,10,1), IsoDate(ANY,12,31), QUARTER))
}
FYQ2 = {
type: QUARTER_OF_YEAR,
label: "FYQ2",
value: TimeWithRange(TimeRange(IsoDate(ANY,1,1), IsoDate(ANY,3,31), QUARTER))
}
FYQ3 = {
type: QUARTER_OF_YEAR,
label: "FYQ3",
value: TimeWithRange(TimeRange(IsoDate(ANY,4,1), IsoDate(ANY,6,30), QUARTER))
}
FYQ4 = {
type: QUARTER_OF_YEAR,
label: "FYQ4",
value: TimeWithRange(TimeRange(IsoDate(ANY,7,1), IsoDate(ANY,9,30), QUARTER))
}
并将以下代码附加到我的english.sutime.txt
# Financial Quarters
FISCAL_YEAR_QUARTER_MAP = {
"Q1": FYQ1,
"Q2": FYQ2,
"Q3": FYQ3,
"Q4": FYQ4
}
FISCAL_YEAR_QUARTER_YEAR_OFFSETS_MAP = {
"Q1": 1,
"Q2": 0,
"Q3": 0,
"Q4": 0
}
$FiscalYearQuarterTerm = CreateRegex(Keys(FISCAL_YEAR_QUARTER_MAP))
{
matchWithResults: TRUE,
pattern: ((/$FiscalYearQuarterTerm/) (FY)? (/(FY)?([0-9]{4})/)),
result: TemporalCompose(INTERSECT, IsoDate(Subtract({type: "NUMBER", value: $$3.matchResults[0].word.group(2)}, FISCAL_YEAR_QUARTER_YEAR_OFFSETS_MAP[$1[0].word]), ANY, ANY), FISCAL_YEAR_QUARTER_MAP[$1[0].word])
}
{
pattern: ((/$FiscalYearQuarterTerm/)),
result: FISCAL_YEAR_QUARTER_MAP[$1[0].word]
}
我仍然无法正确解析像“Q12020”这样的东西
如何正确添加分析会计年度季度(例如“Q1”)的规则
以下是我的完整代码:
import java.util.List;
import java.util.Properties;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.time.*;
import edu.stanford.nlp.util.CoreMap;
public class SUTimeSoExample {
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty("sutime.includeRange", "true");
props.setProperty("sutime.markTimeRanges", "true");
props.setProperty("sutime.rules", "./defs.sutime.txt,./english.sutime.txt");
AnnotationPipeline pipeline = new AnnotationPipeline();
pipeline.addAnnotator(new TokenizerAnnotator(false));
pipeline.addAnnotator(new WordsToSentencesAnnotator(false));
pipeline.addAnnotator(new POSTaggerAnnotator(false));
pipeline.addAnnotator(new TimeAnnotator("sutime", props));
String input = "Stuff for Q1 2020";
Annotation annotation = new Annotation(input);
annotation.set(CoreAnnotations.DocDateAnnotation.class, "2020-06-01");
pipeline.annotate(annotation);
System.out.println(annotation.get(CoreAnnotations.TextAnnotation.class));
List<CoreMap> timexAnnsAll = annotation.get(TimeAnnotations.TimexAnnotations.class);
for (CoreMap cm : timexAnnsAll) {
System.out.println(cm // match
+ " --> " + cm.get(TimeExpression.Annotation.class).getTemporal() // parsed value
);
}
}
}
注意,为了避免this issue,我从stanford corenlp models JAR中删除了deafoltdefs.sutime.txt
和english.sutime.txt
文件
# 1 楼答案
这里有一个Java代码示例:
https://stanfordnlp.github.io/CoreNLP/sutime.html
如果您遵循该示例,主要以以下方式构建管道,它应该会起作用:
并确保使用4.0.0版
如果只想运行SUTime而不运行统计模型,可以将
ner.rulesOnly
设置为true可以使用
ner.docDate
的几个属性之一,或者在运行之前在注释中设置文档日期