DROP TABLE autoprominence; 
SELECT pospos.speaker, pospos.recnr, pospos.texttype, pospos.style,
    pospos.id, 0 AS value, 
pospos.previd, 0 AS prevvalue,
pospos.nextid, 0 AS nextvalue, pospos.chunkid, pospos.word, 
lemmapos.value AS lemma, 
pospos.value AS pos, 
TEXT('content') AS classtype, 
TEXT('content') AS prevclasstype, 
TEXT('content') AS nextclasstype,
lexcgnphoneme.numsyll, 
lexcgnphoneme.sentencepos,
pospos.author 
INTO TABLE autoprominence FROM pospos, lemmapos, lexcgnphoneme 
WHERE pospos.id = lexcgnphoneme.id AND
      lexcgnphoneme.id = pospos.id AND
      pospos.id = lemmapos.id AND
      lemmapos.id = pospos.id AND
      lexcgnphoneme.id = lemmapos.id AND
      lemmapos.id = lexcgnphoneme.id;
      
GRANT SELECT ON autoprominence TO PUBLIC;
CREATE INDEX autoprominence_index on autoprominence (id);

UPDATE autoprominence SET classtype = 'function'
WHERE NOT word IN ('niet', 'geen') AND
      (pos IN ('LID', 'VZ', 'VG', 'VNW', 'TSW') OR
       pos = 'WW' AND lemma IN 
       ('zijn', 'hebben', 'zullen', 'worden'));

UPDATE autoprominence SET prevclasstype = previous.classtype 
FROM autoprominence AS previous
WHERE autoprominence.previd = previous.id AND
      previous.id = autoprominence.previd;
      
UPDATE autoprominence SET prevclasstype = next.classtype 
FROM autoprominence AS next
WHERE autoprominence.nextid = next.id AND
      next.id = autoprominence.nextid;
      
UPDATE autoprominence SET value = autoprominence.value + 1 
WHERE autoprominence.classtype = 'content';

UPDATE autoprominence SET value = value + 2 
WHERE classtype = 'content' AND
      (pos IN ('N', 'ADJ', 'TW') OR word IN ('niet', 'geen'));

UPDATE autoprominence SET value = value + 1 
WHERE classtype = 'content' AND 
      pos IN ('VNW','WW','BW') AND numsyll > 1 AND NOT word IN ('niet', 'geen');
      
UPDATE autoprominence SET value = autoprominence.value + 1 
FROM autoprominence AS previous
WHERE autoprominence.previd = previous.id AND
      previous.id = autoprominence.previd AND
      autoprominence.classtype = 'content' AND
      (
       autoprominence.sentencepos = 1 
       OR
       (
        autoprominence.prevclasstype != 'content' 
	AND
        (
	 previous.sentencepos = 1  
	 OR
         (
	  previous.prevclasstype != 'content' 
	  AND
          previous.sentencepos = 2
	 )
	)
       )
      );
      
UPDATE autoprominence SET value = autoprominence.value - 1 
FROM autoprominence AS previous
WHERE autoprominence.previd = previous.id AND
      previous.id = autoprominence.previd AND
      autoprominence.classtype = 'content' AND
      autoprominence.pos = 'N' AND previous.pos = 'ADJ';
      


