@sharpee/parser-en-us 0.9.60-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +123 -0
- package/dist/direction-mappings.d.ts +24 -0
- package/dist/direction-mappings.d.ts.map +1 -0
- package/dist/direction-mappings.js +82 -0
- package/dist/direction-mappings.js.map +1 -0
- package/dist/english-grammar-engine.d.ts +85 -0
- package/dist/english-grammar-engine.d.ts.map +1 -0
- package/dist/english-grammar-engine.js +562 -0
- package/dist/english-grammar-engine.js.map +1 -0
- package/dist/english-parser.d.ts +184 -0
- package/dist/english-parser.d.ts.map +1 -0
- package/dist/english-parser.js +1268 -0
- package/dist/english-parser.js.map +1 -0
- package/dist/english-pattern-compiler.d.ts +29 -0
- package/dist/english-pattern-compiler.d.ts.map +1 -0
- package/dist/english-pattern-compiler.js +211 -0
- package/dist/english-pattern-compiler.js.map +1 -0
- package/dist/grammar.d.ts +19 -0
- package/dist/grammar.d.ts.map +1 -0
- package/dist/grammar.js +620 -0
- package/dist/grammar.js.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +44 -0
- package/dist/index.js.map +1 -0
- package/dist/parse-failure.d.ts +59 -0
- package/dist/parse-failure.d.ts.map +1 -0
- package/dist/parse-failure.js +132 -0
- package/dist/parse-failure.js.map +1 -0
- package/dist/parser-types.d.ts +185 -0
- package/dist/parser-types.d.ts.map +1 -0
- package/dist/parser-types.js +134 -0
- package/dist/parser-types.js.map +1 -0
- package/dist/pronoun-context.d.ts +119 -0
- package/dist/pronoun-context.d.ts.map +1 -0
- package/dist/pronoun-context.js +249 -0
- package/dist/pronoun-context.js.map +1 -0
- package/dist/scope-evaluator.d.ts +58 -0
- package/dist/scope-evaluator.d.ts.map +1 -0
- package/dist/scope-evaluator.js +205 -0
- package/dist/scope-evaluator.js.map +1 -0
- package/dist/slot-consumers/entity-slot-consumer.d.ts +36 -0
- package/dist/slot-consumers/entity-slot-consumer.d.ts.map +1 -0
- package/dist/slot-consumers/entity-slot-consumer.js +413 -0
- package/dist/slot-consumers/entity-slot-consumer.js.map +1 -0
- package/dist/slot-consumers/index.d.ts +43 -0
- package/dist/slot-consumers/index.d.ts.map +1 -0
- package/dist/slot-consumers/index.js +78 -0
- package/dist/slot-consumers/index.js.map +1 -0
- package/dist/slot-consumers/slot-consumer.d.ts +61 -0
- package/dist/slot-consumers/slot-consumer.d.ts.map +1 -0
- package/dist/slot-consumers/slot-consumer.js +31 -0
- package/dist/slot-consumers/slot-consumer.js.map +1 -0
- package/dist/slot-consumers/text-slot-consumer.d.ts +33 -0
- package/dist/slot-consumers/text-slot-consumer.d.ts.map +1 -0
- package/dist/slot-consumers/text-slot-consumer.js +157 -0
- package/dist/slot-consumers/text-slot-consumer.js.map +1 -0
- package/dist/slot-consumers/typed-slot-consumer.d.ts +35 -0
- package/dist/slot-consumers/typed-slot-consumer.d.ts.map +1 -0
- package/dist/slot-consumers/typed-slot-consumer.js +151 -0
- package/dist/slot-consumers/typed-slot-consumer.js.map +1 -0
- package/dist/slot-consumers/vocabulary-slot-consumer.d.ts +42 -0
- package/dist/slot-consumers/vocabulary-slot-consumer.d.ts.map +1 -0
- package/dist/slot-consumers/vocabulary-slot-consumer.js +186 -0
- package/dist/slot-consumers/vocabulary-slot-consumer.js.map +1 -0
- package/package.json +65 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* English Parser Implementation
|
|
3
|
+
*
|
|
4
|
+
* This parser handles English-specific grammar patterns and preserves all information
|
|
5
|
+
* in rich structured commands.
|
|
6
|
+
*/
|
|
7
|
+
import { Parser, ParserOptions, Token as InternalToken, InternalParseResult, ParserLanguageProvider, VerbVocabulary, VocabularyEntry, Constraint } from '@sharpee/if-domain';
|
|
8
|
+
import type { IParsedCommand, IValidatedCommand, IParseError as CoreParseError } from '@sharpee/world-model';
|
|
9
|
+
import type { ISystemEvent, Result } from '@sharpee/core';
|
|
10
|
+
import { GrammarBuilder } from '@sharpee/if-domain';
|
|
11
|
+
import { PronounContextManager } from './pronoun-context';
|
|
12
|
+
type CommandResult<T, E> = Result<T, E>;
|
|
13
|
+
/**
|
|
14
|
+
* English parser with rich information preservation
|
|
15
|
+
*/
|
|
16
|
+
export declare class EnglishParser implements Parser {
|
|
17
|
+
private options;
|
|
18
|
+
private language;
|
|
19
|
+
private onDebugEvent?;
|
|
20
|
+
private platformEventEmitter?;
|
|
21
|
+
private grammarEngine;
|
|
22
|
+
private worldContext;
|
|
23
|
+
/** Pronoun context manager for "it", "them", "him", "her" resolution (ADR-089) */
|
|
24
|
+
private pronounContext;
|
|
25
|
+
constructor(language: ParserLanguageProvider, options?: ParserOptions);
|
|
26
|
+
/**
|
|
27
|
+
* Initialize vocabulary from language provider
|
|
28
|
+
*/
|
|
29
|
+
private initializeVocabulary;
|
|
30
|
+
/**
|
|
31
|
+
* Set debug event callback
|
|
32
|
+
*/
|
|
33
|
+
setDebugCallback(callback: ((event: ISystemEvent) => void) | undefined): void;
|
|
34
|
+
/**
|
|
35
|
+
* Set platform event emitter for parser debugging
|
|
36
|
+
*/
|
|
37
|
+
setPlatformEventEmitter(emitter: ((event: any) => void) | undefined): void;
|
|
38
|
+
/**
|
|
39
|
+
* Set the world context for scope constraint evaluation
|
|
40
|
+
*/
|
|
41
|
+
setWorldContext(world: any, actorId: string, currentLocation: string): void;
|
|
42
|
+
/**
|
|
43
|
+
* Emit a platform debug event
|
|
44
|
+
*/
|
|
45
|
+
private emitPlatformEvent;
|
|
46
|
+
/**
|
|
47
|
+
* Register additional verbs after parser creation
|
|
48
|
+
* Used for story-specific vocabulary
|
|
49
|
+
*/
|
|
50
|
+
registerVerbs(verbs: VerbVocabulary[]): void;
|
|
51
|
+
/**
|
|
52
|
+
* Register additional vocabulary entries
|
|
53
|
+
* More generic than registerVerbs - can handle any part of speech
|
|
54
|
+
*/
|
|
55
|
+
registerVocabulary(entries: VocabularyEntry[]): void;
|
|
56
|
+
/**
|
|
57
|
+
* Parse input text into structured command with rich information
|
|
58
|
+
*/
|
|
59
|
+
parse(input: string): CommandResult<IParsedCommand, CoreParseError>;
|
|
60
|
+
/**
|
|
61
|
+
* Parse input that may contain multiple commands separated by periods or commas.
|
|
62
|
+
* Returns an array of parsed commands (or errors).
|
|
63
|
+
*
|
|
64
|
+
* Period chaining:
|
|
65
|
+
* - "take sword. go north." → [take sword, go north]
|
|
66
|
+
*
|
|
67
|
+
* Comma chaining (only when verb detected after comma):
|
|
68
|
+
* - "take sword, drop it" → [take sword, drop it] (verb after comma)
|
|
69
|
+
* - "take knife, lamp" → single command with list (no verb after comma)
|
|
70
|
+
*
|
|
71
|
+
* Examples:
|
|
72
|
+
* - "take sword. go north." → [take sword, go north]
|
|
73
|
+
* - "take sword" → [take sword]
|
|
74
|
+
* - "take sword. invalid. go north" → [take sword, error, go north]
|
|
75
|
+
*/
|
|
76
|
+
parseChain(input: string): CommandResult<IParsedCommand, CoreParseError>[];
|
|
77
|
+
/**
|
|
78
|
+
* Split a segment on commas only if a verb is detected after the comma.
|
|
79
|
+
* "take knife, drop lamp" → ["take knife", "drop lamp"] (verb after comma)
|
|
80
|
+
* "take knife, lamp" → ["take knife, lamp"] (no verb, treat as list)
|
|
81
|
+
*/
|
|
82
|
+
private splitOnCommasIfChain;
|
|
83
|
+
/**
|
|
84
|
+
* Split input on periods, preserving quoted strings.
|
|
85
|
+
* Handles edge cases like trailing periods and empty segments.
|
|
86
|
+
*/
|
|
87
|
+
private splitOnPeriods;
|
|
88
|
+
/**
|
|
89
|
+
* Tokenize input with rich information preservation
|
|
90
|
+
*/
|
|
91
|
+
private tokenizeRich;
|
|
92
|
+
/**
|
|
93
|
+
* Find possible command structures in the tokens
|
|
94
|
+
*/
|
|
95
|
+
private findCommandStructures;
|
|
96
|
+
/**
|
|
97
|
+
* Convert a grammar match to a RichCandidate
|
|
98
|
+
*/
|
|
99
|
+
private convertGrammarMatch;
|
|
100
|
+
/**
|
|
101
|
+
* Register story-specific grammar rules
|
|
102
|
+
* @deprecated Use getStoryGrammar() for full API
|
|
103
|
+
*/
|
|
104
|
+
registerGrammar(pattern: string, action: string, constraints?: Record<string, Constraint>): void;
|
|
105
|
+
/**
|
|
106
|
+
* Get the grammar builder for story-specific rules.
|
|
107
|
+
* Stories use this to define custom grammar patterns.
|
|
108
|
+
*
|
|
109
|
+
* ADR-084: Returns the grammar builder directly instead of a wrapper,
|
|
110
|
+
* giving stories full access to all PatternBuilder methods.
|
|
111
|
+
*/
|
|
112
|
+
getStoryGrammar(): GrammarBuilder;
|
|
113
|
+
/**
|
|
114
|
+
* Get candidate interpretations for a token
|
|
115
|
+
*/
|
|
116
|
+
private getTokenCandidates;
|
|
117
|
+
/**
|
|
118
|
+
* Old tokenize method for compatibility
|
|
119
|
+
*/
|
|
120
|
+
tokenize(input: string): InternalToken[];
|
|
121
|
+
/**
|
|
122
|
+
* Parse with errors for testing compatibility
|
|
123
|
+
*/
|
|
124
|
+
parseWithErrors(input: string, options?: ParserOptions): InternalParseResult;
|
|
125
|
+
/**
|
|
126
|
+
* Add a custom verb to the parser vocabulary
|
|
127
|
+
* @param actionId The action ID this verb maps to
|
|
128
|
+
* @param verbs Array of verb forms to recognize
|
|
129
|
+
* @param pattern Optional grammar pattern for the verb (e.g., 'VERB_OBJ')
|
|
130
|
+
* @param prepositions Optional prepositions this verb can use
|
|
131
|
+
*/
|
|
132
|
+
addVerb(actionId: string, verbs: string[], pattern?: string, prepositions?: string[]): void;
|
|
133
|
+
/**
|
|
134
|
+
* Add a custom noun/synonym to the parser vocabulary
|
|
135
|
+
* @param word The word to add
|
|
136
|
+
* @param canonical The canonical form it maps to
|
|
137
|
+
*/
|
|
138
|
+
addNoun(word: string, canonical: string): void;
|
|
139
|
+
/**
|
|
140
|
+
* Add a custom adjective to the parser vocabulary
|
|
141
|
+
* @param word The adjective to add
|
|
142
|
+
*/
|
|
143
|
+
addAdjective(word: string): void;
|
|
144
|
+
/**
|
|
145
|
+
* Add a custom preposition to the parser vocabulary
|
|
146
|
+
* @param word The preposition to add
|
|
147
|
+
*/
|
|
148
|
+
addPreposition(word: string): void;
|
|
149
|
+
/**
|
|
150
|
+
* Build a detailed parse error from failure analysis
|
|
151
|
+
*/
|
|
152
|
+
private buildParseError;
|
|
153
|
+
/**
|
|
154
|
+
* Update pronoun context after a successful command execution
|
|
155
|
+
* Called by the engine after command execution succeeds
|
|
156
|
+
* @param command The validated command with resolved entity IDs
|
|
157
|
+
* @param turnNumber Current turn number
|
|
158
|
+
*/
|
|
159
|
+
updatePronounContext(command: IValidatedCommand, turnNumber: number): void;
|
|
160
|
+
/**
|
|
161
|
+
* Register an entity that was mentioned in context
|
|
162
|
+
* Used when entities are referenced outside of standard parsing
|
|
163
|
+
* @param entityId The entity's ID
|
|
164
|
+
* @param text How the player referred to it
|
|
165
|
+
* @param turnNumber Current turn number
|
|
166
|
+
*/
|
|
167
|
+
registerPronounEntity(entityId: string, text: string, turnNumber: number): void;
|
|
168
|
+
/**
|
|
169
|
+
* Reset the pronoun context
|
|
170
|
+
* Called on game restart or when context should be cleared
|
|
171
|
+
*/
|
|
172
|
+
resetPronounContext(): void;
|
|
173
|
+
/**
|
|
174
|
+
* Get the last successfully parsed command
|
|
175
|
+
* Used for "again" / "g" command support
|
|
176
|
+
*/
|
|
177
|
+
getLastCommand(): IParsedCommand | null;
|
|
178
|
+
/**
|
|
179
|
+
* Get the pronoun context manager (for testing/debugging)
|
|
180
|
+
*/
|
|
181
|
+
getPronounContextManager(): PronounContextManager;
|
|
182
|
+
}
|
|
183
|
+
export {};
|
|
184
|
+
//# sourceMappingURL=english-parser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"english-parser.d.ts","sourceRoot":"","sources":["../src/english-parser.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EACL,MAAM,EACN,aAAa,EACb,KAAK,IAAI,aAAa,EAGtB,mBAAmB,EAMnB,sBA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