jaclearn.nlp.sng_parser package

jaclearn.nlp.sng_parser.tprint(graph, file=None, show_entities=True, show_relations=True)[source]

Print a scene graph as a table. The printed strings contains only essential information about the parsed scene graph.

class jaclearn.nlp.sng_parser.Parser(backend=None, **kwargs)[source]

Bases: object

The scene graph parser. To instantiate a scene graph parser, you need to specify a backend (default: spacy) and the initialization keyword arguments for the backend (optional).

Once the backend has been specified, you can call parser.parse(sentence) to parse a sentence in natural language into a scene graph. Note that, the function parse also supports extra keyword arguments which will be passed to the backend for the parsing. To use this feature, you may refer to the implementation of your parser backend.

Example:: >>> parser = Parser(backend, **init_kwargs) >>> graph = parser.parse(‘A woman is playing the piano,’)

init_kwargs

Get the keyward arguments used for initializing the backend.

parse(sentence, **kwargs)[source]

Parse a sentence into a scene graph.

Parameters:sentence (str) – the input sentence.
Returns:the parsed scene graph. Please refer to the README file for the specification of the return value.
Return type:graph (dict)
classmethod register_backend(backend)[source]

Register a class as the backend. The backend should implement a method named parse having the following signature: parse(sentence, <other_keyward_arguments>).

To register your customized backend as the parser, use this class method as a decorator on your class.

Example:: >>> @Parser.register_backend >>> class CustomizedBackend(Backend): >>> # Your implementation follows… >>> pass

unwrapped

Get the backend.

jaclearn.nlp.sng_parser.get_default_parser()[source]

Get the default parser.

The default parser is a global one (singleton).

jaclearn.nlp.sng_parser.parse(sentence, **kwargs)[source]

Parse the sentence using the default parser. This ia an easy-to-use feature for those who do not want to configure their own parsers and want to use the parser at different places in their codes.

Please note that the default parser is a singleton. Thus, if you are using a stateful parser, you need to be careful about sharing this parser everywhere.