[英]Parsekit autocompletion
給出一個簡單的Parsekit語法。
@start = sentence+;
sentence = 'beer' container;
container = 'bottle' | 'cup';
如果我有部分解析beer
,是否可以讓Parsekit將可能的完成返回到句子中?
這里是ParseKit的開發者。
不幸的是,ParseKit中沒有“自由”機制,它將自己返回這些數據。
但是,這正是ParseKit設計/非常適合的任務。 但是你必須自己編寫一些代碼。 像這樣的東西會起作用:
首先,稍微改變語法以包括'beer'
的命名生產。 這樣可以更輕松地實現回調:
@start = sentence+;
sentence = beer container;
beer = 'beer';
container = 'bottle' | 'cup';
然后為beer
生產實施匯編程序回調。
- (void)parser:(PKParser *)p didMatchBeer:(PKAssembly *)a {
// here you could find some clever way to look up the completions for 'beer' prefix
// or you can hardcode the completions yourself
NSArray *completions = [NSArray arrayWithObjects:@"bottle", @"cup", nil];
// store the completions as the current assembly's target
a.target = completions;
}
您的“客戶”或“驅動程序”代碼應如下所示:
PKParser *p = [PKParserFactory parserFromGrammar:g assembler:a];
PKAssembly *input = [PKAssembly assemblyWithString:@"beer"]; // get this string from the user in reality
PKAssembly *result = [p bestMatchFor:input];
NSArray *completions = result.target;
這應該讓您了解如何使用ParseKit實現這些類型的事物。 對於這樣一個小例子,這個解決方案可能看起來有點過分(可能是)。 但對於一個大型的現實世界的例子,這將是一個非常強大和優雅的自動完成解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.