1 /*************************************************************************/
2 /* Copyright (c) 2004                                                    */
3 /* Daniel Sleator, David Temperley, and John Lafferty                    */
4 /* Copyright (c) 2013 Linas Vepstas                                      */
5 /* All rights reserved                                                   */
6 /*                                                                       */
7 /* Use of the link grammar parsing system is subject to the terms of the */
8 /* license set forth in the LICENSE file included with this software.    */
9 /* This license allows free redistribution and use in source and binary  */
10 /* forms, with or without modification, subject to certain conditions.   */
11 /*                                                                       */
12 /*************************************************************************/
13 
14 #ifndef _PP_STRUCTURES_H_
15 #define _PP_STRUCTURES_H_
16 
17 #include <stdbool.h>
18 #include "api-types.h"
19 #include "post-process.h"
20 
21 typedef struct Domain_s Domain;
22 typedef struct DTreeLeaf_s DTreeLeaf;
23 typedef struct List_o_links_struct List_o_links;
24 
25 struct Domain_s
26 {
27 	const char *   string;
28 	List_o_links * lol;
29 	DTreeLeaf *    child;
30 	Domain *       parent;
31 	size_t         size;
32 	size_t         start_link;  /* the link that started this domain */
33 	char           type;        /* one letter name */
34 };
35 
36 
37 struct DTreeLeaf_s
38 {
39 	Domain *    parent;
40 	DTreeLeaf * next;
41 	int         link;
42 };
43 
44 struct PP_data_s
45 {
46 	List_o_links ** word_links;
47 	size_t wowlen;
48 	size_t N_domains;
49 	Domain * domain_array;          /* The domains, sorted by size */
50 	size_t domlen;                  /* Allocated size of domain_array */
51 	size_t num_words;               /* Number of words in linkage */
52 	List_o_links * links_to_ignore;
53 
54 	bool *visited;                  /* For the depth-first search */
55 	size_t vlength;                 /* Length of visited array */
56 };
57 
58 /* A new Postprocessor struct is alloc'ed for each sentence. It contains
59  * sentence-specific post-processing information.
60  */
61 struct Postprocessor_s
62 {
63 	pp_knowledge  * knowledge;           /* Internal rep'n of the actual rules */
64 	int n_global_rules_firing;           /* this & the next are diagnostic     */
65 	int n_local_rules_firing;
66 	pp_linkset *set_of_links_of_sentence;     /* seen in *any* linkage of sent */
67 	pp_linkset *set_of_links_in_an_active_rule;/*used in *some* linkage of sent*/
68 	int *relevant_contains_one_rules;        /* -1-terminated list of indices  */
69 	int *relevant_contains_none_rules;
70 	bool q_pruned_rules;       /* don't prune rules more than once in p.p. */
71 	String_set *string_set;      /* Link names seen for sentence */
72 
73 	/* Per-linkage state; this data must be reset prior to processing
74 	 * each new linkage. */
75 	const char *violation;
76 	PP_data pp_data;
77 };
78 
79 struct PP_domains_s
80 {
81 	size_t          num_domains;
82 	const char **   domain_name;
83 };
84 
85 /* -------------------------------------------------------------- */
86 /* Other. "private", internal-use-only post-processing structures */
87 
88 /* Davy added these */
89 struct List_o_links_struct
90 {
91 	size_t link;     /* the link number */
92 	size_t word;     /* the word at the other end of this link */
93 	List_o_links * next;
94 };
95 
96 /* from pp_linkset.c */
97 typedef struct pp_linkset_node_s
98 {
99 	const char *str;
100 	struct pp_linkset_node_s *next;
101 } pp_linkset_node;
102 
103 struct pp_linkset_s
104 {
105 	unsigned int hash_table_size;
106 	unsigned int population;
107 	pp_linkset_node **hash_table;    /* data actually lives here */
108 };
109 
110 typedef struct StartingLinkAndDomain_s StartingLinkAndDomain;
111 struct StartingLinkAndDomain_s
112 {
113 	const char *starting_link;
114 	int   domain;       /* domain which the link belongs to (-1: terminator)*/
115 };
116 
117 typedef struct pp_rule_s
118 {
119 	/* Holds a single post-processing rule. Since rules come in many
120 	   flavors, not all fields of the following are always relevant  */
121 	const char *selector; /* name of link to which rule applies      */
122 	bool selector_has_wildcard;             /* selector contains '*' */
123 	pp_linkset *link_set; /* handle to set of links relevant to rule */
124 	int   link_set_size;  /* size of this set                        */
125 	int   domain;         /* type of domain to which rule applies    */
126 	const char  **link_array; /* array holding the spelled-out names */
127 	const char  *msg;     /* explanation (NULL=end sentinel in array)*/
128 	int use_count;        /* Number of times rule has been applied   */
129 } pp_rule;
130 
131 typedef struct PPLexTable_s PPLexTable;
132 struct pp_knowledge_s
133 {
134 	PPLexTable *lt; /* Internal rep'n of sets of strings from knowledge file */
135 	const char *path; /* Name of file we loaded from */
136 
137 	/* handles to sets of links specified in knowledge file. These constitute
138 	   auxiliary data, necessary to implement the rules, below. See comments
139 	   in post-process.c for a description of these. */
140 	pp_linkset *domain_starter_links;
141 	pp_linkset *urfl_domain_starter_links;
142 	pp_linkset *urfl_only_domain_starter_links;
143 	pp_linkset *domain_contains_links;
144 	pp_linkset *must_form_a_cycle_links;
145 	pp_linkset *restricted_links;
146 	pp_linkset *ignore_these_links;
147 	pp_linkset *left_domain_starter_links;
148 
149 	/* arrays of rules specified in knowledge file */
150 	pp_rule *form_a_cycle_rules;
151 	pp_rule *contains_one_rules;
152 	pp_rule *contains_none_rules;
153 	pp_rule *bounded_rules;
154 
155 	size_t n_form_a_cycle_rules;
156 	size_t n_contains_one_rules;
157 	size_t n_contains_none_rules;
158 	size_t n_bounded_rules;
159 
160 	size_t nStartingLinks;
161 	pp_linkset *set_of_links_starting_bounded_domain;
162 	StartingLinkAndDomain *starting_link_lookup_table;
163 	String_set *string_set;
164 };
165 
166 #endif
167