Skip to content

Instantly share code, notes, and snippets.

@EmacsUser
Last active December 14, 2015 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EmacsUser/5148484 to your computer and use it in GitHub Desktop.
Save EmacsUser/5148484 to your computer and use it in GitHub Desktop.
日本語とInform
Chapter "Japanese"
Section "Includes"
[Development snapshots are at https://github.com/EmacsUser/i7grip.]
Include Context-Free Parsing Engine by Brady Garvin.
Include Punctuated Word Parsing Engine by Brady Garvin.
Include Disambiguation Framework by Brady Garvin.
Section "Parsing Language"
A parsing language is a kind of value. The parsing languages are English and Japanese.
The current parsing language is a parsing language that varies. The current parsing language is English.
Section "Japanese Parser"
The Japanese parser is a context-free parser that varies.
Kaori, hon, wo, yomu, kudasai/--, and comma are parsemes that vary.
Nominal, verbal, transitive command, and command are parsemes that vary.
Translation relates various parsemes to various texts. The verb to translate to (it translates to, they translate to, it translated to it is translated to, it is translating to) implies the translation relation.
When play begins:
now the Japanese parser is a new context-free parser;
now kaori is a new nonterminal in the Japanese parser named "kaori";
now kaori translates to "kaori";
now hon is a new nonterminal in the Japanese parser named "hon";
now hon translates to "book";
now wo is a new nonterminal in the Japanese parser named "direct object particle";
now yomu is a new nonterminal in the Japanese parser named "yomu";
now yomu translates to "read";
now kudasai/-- is a new nonterminal in the Japanese parser named "optional occurrence of kudasai";
now comma is a new nonterminal in the Japanese parser named "comma";
now comma translates to ",";
now nominal is a new nonterminal in the Japanese parser named "nominal";
now verbal is a new nonterminal in the Japanese parser named "verbal";
now a transitive command is a new nonterminal in the Japanese parser named "transitive command";
now a command is a new nonterminal in the Japanese parser named "command";
understand "kaori" and "kaorisan" and "kaori-san" as kaori regardless of case;
understand "," as a comma;
understand "hon" as hon regardless of case;
understand "wo" or "o" as wo regardless of case;
understand "yomu" or "yonde [kudasai/--]" as yomu regardless of case;
understand "kudasai" or "" as kudasai/-- regardless of case;
understand "[kaori]" as a nominal;
understand "[hon]" as a nominal;
understand "[yomu]" as a verbal;
understand "[nominal] [wo] [verbal]" as a transitive command;
understand "[a transitive command]" as a command;
understand "[nominal] [comma] [a command]" as a command;
put the Japanese parser into normal form.
Section "Japanese-to-English Rewrites"
The Japanese canonicalization rulebook is a rulebook.
To reverse the children of (V - a parse tree vertex):
let the previous child be the first child of V;
let the child be the right sibling of the previous child;
write the last child the previous child to V;
write the left sibling the child to the previous child;
write the right sibling a null parse tree vertex to the previous child;
while the child is not null:
let the next child be the right sibling of the child;
write the left sibling the next child to the child;
write the right sibling the previous child to the child;
now the previous child is the child;
now the child is the next child;
write the first child the previous child to V;
write the left sibling a null parse tree vertex to the previous child.
A Japanese canonicalization rule (this is the reverse transitive commands rule):
if the parseme of the parse tree vertex to canonicalize is a transitive command:
reverse the children of the parse tree vertex to canonicalize.
Section "The Main Japanese-to-English Rewrite Rule"
[We don't use these parts of the parsing framework; declare them, but leave them empty.]
The Japanese scoring rulebook is a rulebook.
The Japanese primary filtration rulebook is a rulebook.
The Japanese secondary filtration rulebook is a rulebook.
The rewritten command is some indexed text that varies.
To decide whether addressing Kaori:
decide on whether or not the player's command exactly matches the regular expression "kaori(-?san)? *, +.*".
After reading a command when the current parsing language is Japanese or addressing Kaori:
let the synthetic command be a new synthetic text copied from "[the player's command]";
write the punctuated words of the synthetic command to the Japanese parser;
let the root be the root of the match for a command canonicalized by the Japanese canonicalization rulebook and disambiguated by scores from the Japanese scoring rulebook and primary filtration from the Japanese primary filtration and secondary filtration from the Japanese secondary filtration rulebook and disambiguating choices from the default value of phrase (context-free parser, linked list, truth state) -> disambiguation feature;
[We don't provide disambiguation, so if an input is ambiguous, we won't rewrite it. The grammar should therefore be written to prevent (grammatical) ambiguities.]
if the root is null:
if addressing Kaori:
say "'Sumimasen. Amari wakarimasendesita.'";
otherwise:
say "Watasiha sono meireiga wakarimasendesita.";
reject the player's command;
otherwise:
now the rewritten command is "";
let the vertex be the root;
while the vertex is not null:
if the parseme of the vertex relates to a text by the translation relation:
now the rewritten command is "[the rewritten command][if the rewritten command is not empty] [end if][the text to which the parseme of the vertex relates by the translation relation]";
now the vertex is the parse tree vertex to visit after the vertex;
say "(For debugging, the English interpretation is '[the rewritten command]'.)[command clarification break]";
replace the player's command with "[the rewritten command]";
delete the punctuated words from the Japanese parser;
delete the synthetic text the synthetic command.
Chapter "Scenario"
Daigaku is a room. A book is here. A woman called Kaori is here.
Persuasion: rule succeeds.
Instead of examining the book:
if the current parsing language is:
-- English:
now the current parsing language is Japanese;
-- Japanese:
now the current parsing language is English;
say "Switched to [the current parsing language]."
Test me with "read book / read book / hon wo yonde / kaori, read book / kaori, hon wo yonde".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment