word: form synonyms from alt column
[sheet.git] / tools / word.pg.sql
1 CREATE TABLE word (
2         form       text        NOT NULL,
3         alt        text[],
4         lang       text        NOT NULL DEFAULT 'eng',
5         cat        integer              REFERENCES word (id),
6         ref        integer              REFERENCES word (id),
7         prio       smallint    NOT NULL DEFAULT '1',
8         source     text,
9         thumb      text[],
10         wptitle    text,
11         created    timestamptz          DEFAULT now(),
12         id         serial      NOT NULL PRIMARY KEY
13 );
14
15 COMMENT ON COLUMN word.form       IS 'preferred textual representation';
16 COMMENT ON COLUMN word.alt        IS 'alternate forms with equivalent meaning';
17 COMMENT ON COLUMN word.lang       IS 'ISO 639-3 language code';
18 COMMENT ON COLUMN word.cat        IS 'hierarchical classification';
19 COMMENT ON COLUMN word.ref        IS 'reference to equivalent eng translation';
20 COMMENT ON COLUMN word.prio       IS 'difficulty level or importance; lower values have precedence';
21 COMMENT ON COLUMN word.source     IS 'URI of downloaded image';
22 COMMENT ON COLUMN word.thumb      IS 'ImageMagick convert options to create thumbnail from source image';
23 COMMENT ON COLUMN word.wptitle    IS 'reference Wikipedia article';
24
25 CREATE OR REPLACE FUNCTION exportform(word) RETURNS text AS $$
26         SELECT array_to_string($1.form || $1.alt, '/');
27 $$ LANGUAGE SQL IMMUTABLE;
28
29 CREATE OR REPLACE VIEW _cat_words AS
30         SELECT exportform(word.*) form, sub.*, word.lang, word.ref
31         FROM word RIGHT JOIN (
32                 SELECT cat id, array_agg(exportform(word.*) ORDER BY form) forms
33                         FROM word WHERE ref IS NULL GROUP BY cat
34         ) sub USING (id);