#!/opt/local/bin/gawk -f

#
# Copyright(C) 2007-2012 National Institute of Information and Communications Technology
#

BEGIN {
  FS = "\t";

  if (ARGC < 3) {
    printf "usage: par2dat.awk <dictionary.dic> <reverse.dic> [<*.par>]\n" > "/dev/stderr";
    exit 1;
  }
  dicfile = ARGV[1];
  ARGV[1] = "";
  dirfile = ARGV[2];
  ARGV[2] = "";

  # 辞書の読み込み(w/ 形態素解析)+トライに格納
  ndic = dicread(dicfile, dicw, dicp, trie_flg, trie_val);
  ndic2 = dicread(dirfile, dicw2, dicp2, trie_flg2, trie_val2);
}

{
  cls = $1;
  n = split($3, surf, / /);
  n = split($4, base, / /);
  n = split($5, cpos, / /);
  n = split($6, fpos, / /);
  m = split($7, chnk, / /);
  m = split($8, head, / /);
  m = split($9, type, / /);
  chnk[m + 1] = n + 1;
  for (i = 1; i <= n; i++) if (fpos[i] == "*") fpos[i] = cpos[i];

  # 並列構造の係先の修正
  for (i = 1; i < m; i++) {
    if (type[i] == "P") {
      type[i] = type[head[i]];
      head[i] = head[head[i]];
      i--;
      continue;
    }
  }

  # 極性タグ付与(trie探索)
  for (i = 1; i <= n; i++) pole[i] = "*";
  nlist = lookup(n, base, 1, n, trie_flg, trie_val, listv, listb, listl);
  for (i = 0; i < nlist; i++) {
    j = listb[i] + listl[i];

    # 「嬉しい限り」のような表現
    if (fpos[j] == "副詞的名詞") continue;
    # 「大変」が副詞的用法の場合は例外(「作物には大変良い」など)
    if (dicw[listv[i]] == "大変だ" && (cpos[j + 1] == "形容詞" || cpos[j + 1] == "名詞" || cpos[j + 1] == "動詞")) continue;

    pole[j] = dicp[listv[i]];
    if (pole[j] == "0") pole[j] = "*";

    # 「非合法」「不完全」「未調査」など
    if (listb[i] >= 2 && pole[j] != "*" && cpos[j] == "名詞" && fpos[listb[i] - 1] == "ナ形容詞接頭辞" && (base[listb[i] - 1] == "非" || base[listb[i] - 1] == "不" || base[list[i] - 1] == "未")) {
      pole[j] = ((pole[j] == "*") ? "*" : ((pole[j] == "+") ? "-" : "+"));
    }
  }

  # 「悪さ」などの対処
  for (i = 1; i < n; i++) {
    if (pole[i] != "*" && cpos[i] == "形容詞" && fpos[i + 1] == "名詞性述語接尾辞") {
      pole[i + 1] = pole[i];
      pole[i] = "*";
    }
  }

  # 文節内に極性を持つ語が複数ある場合は最後のものだけを使う
  for (i = 1; i <= m; i++) {
    flag = 0;
    for (j = chnk[i + 1] - 1; j >= chnk[i]; j--) {
      if (pole[j] != "*") {
	if (flag == 0) {
	  flag = j;
	} else {
	  pole[j] = "*";
	}
      }
    }
  }

  for (i = 1; i <= n; i++) dir[i] = "*";
  nlist2 = lookup(n, base, 1, n, trie_flg2, trie_val2, listv2, listb2, listl2);
  for (i = 0; i < nlist2; i++) {
    j = listb2[i] + listl2[i];

    if (!(cpos[j] == "動詞" || cpos[j] == "形容詞" || cpos[j] == "名詞")) continue; #「ない／接尾辞」などを除く
    if (dicp2[listv2[i]] == "0") continue;	# 極性0は抑制用のダミー

    dir[j] = dicp2[listv2[i]];
  }
  for (i = 1; i <= m; i++) {
    dir2[i] = 0;
    for (j = chnk[i]; j < chnk[i + 1]; j++) {
      if (dir[j] == "+") dir2[i]++; else if (dir[j] == "-") dir2[i]--;
    }
    if (dir2[i] > 0) dir2[i] = "+"; else if (dir2[i] < 0) dir2[i] = "-"; else dir2[i] = "0";
  }

  # 機能語による否定
  # 「心配なし」など
  for (i = 1; i <= n - 1; i++) {
    if (pole[i] != "*" && (base[i + 1] == "なし" || base[i + 1] == "無し") && fpos[i + 1] == "名詞性名詞接尾辞") {
      pole[i] = ((pole[i] == "*") ? "*" : ((pole[i] == "+") ? "-" : "+"));
    }
  }
  # 普通の否定
  for (i = 1; i <= m; i++) {
    for (j = chnk[i]; j < chnk[i + 1]; j++) {
      l = j;
      if ((base[j] == "ない" && cpos[j] == "接尾辞") || (base[j] == "ぬ" && cpos[j] == "助動詞") || (base[j] == "にくい" && cpos[j] == "接尾辞") || (base[j] == "づらい" && cpos[j] == "接尾辞") || (base[j] == "まい" && fpos[j] == "助動詞") || (base[j] == "かねる" && fpos[j] == "動詞性接尾辞")) break;
      if (base[j] == "こと" && fpos[j] == "形式名詞" && (base[j + 1] == "ない" || base[j + 1] == "無い") && cpos[j + 1] == "形容詞") break;
    }
    if (j == chnk[i + 1]) continue;
    # 「にも関わらず」は例外
    if (base[l] == "ぬ" && base[l - 3] == "に" && base[l - 2] == "も" && (base[l - 1] == "かかわる" || base[l - 1] == "関わる")) continue;
    # 「〜ないかどうか」「〜しれない」「〜だけではない」「ばかりでなく」は例外
    for (j = chnk[i]; j < chnk[i + 1]; j++) {
      if (base[j] == "か" && (fpos[j] == "接続助詞" || fpos[j] == "終助詞")) break;
      if ((base[j] == "しれる" || base[j] == "知れる") && cpos[j] == "動詞") break;
      if (j < l && base[j] == "だけ" && base[j + 1] == "だ") break;
      if (j < l && base[j] == "ばかり" && base[j + 1] == "だ") break;
      if (j < l && base[j] == "ばかりだ") break;
      # 二重否定「〜しなければならない」「〜なくはない」
      if (j > l && ((base[j] == "ない" && cpos[j] == "接尾辞") || (base[j] == "ぬ" && cpos[j] == "助動詞") || (base[j] == "まい" && fpos[j] == "助動詞"))) break;
    }
    if (j < chnk[i + 1]) continue;
    for (j = chnk[i]; j < chnk[i + 1] && j < l; j++) {
      pole[j] = ((pole[j] == "*") ? "*" : ((pole[j] == "+") ? "-" : "+"));
    }
    if (dir2[i] == "-") {
      dir2[i] = "+";
    } else {
      dir2[i] = "-"; #デフォルトで+とする
    }
  }

  # 属性の整理
  for (i = 1; i <= m; i++) {
    pol[i] = "0";
    for (j = chnk[i + 1] - 1; j >= chnk[i]; j--) {
      if (pole[j] != "*") {
	pol[i] = ((pole[j] == "+") ? "1" : "2");
	break;
      }
    }
  }
  for (i = 1; i <= m; i++) {
    rev[i] = ((dir2[i] == "-") ? "1" : "0");
  }

  # 事例出力
  printf "%d", cls;
  for (i = 1; i <= m; i++) {
    s = "";
    b = "";
    c = "";
    f = "";
    for (j = chnk[i]; j < chnk[i + 1]; j++) {
      s = s surf[j] " ";
      b = b base[j] " ";
      c = c cpos[j] " ";
      f = f fpos[j] " ";
    }
    printf "\t%d\t%s\t%s\t%s\t%s\t%s\t%s", head[i], s, b, c, f, pol[i], rev[i];
  }
  printf "\n";
}
