Exemplo n.º 1
0
 public static function get_tasks($user_id, $num = 1)
 {
     $res = sql_pe("\n            SELECT mw_id, tf_id, sent_id, mw_type\n            FROM mw_main\n            JOIN mw_tokens mwt USING (mw_id)\n            LEFT JOIN tokens t USING (tf_id)\n            WHERE status = " . self::NOT_READY . "\n            AND mw_id NOT IN (\n                SELECT DISTINCT mw_id\n                FROM mw_answers\n                LEFT JOIN mw_main USING (mw_id)\n                WHERE user_id = ?\n                AND status = " . self::NOT_READY . "\n            )\n            ORDER BY sent_id, mw_id\n            LIMIT ?\n        ", array($user_id, $num * 10));
     if (!sizeof($res)) {
         return array();
     }
     $out = array();
     $sent_id = $res[0]['sent_id'];
     $mw_type = $res[0]['mw_type'];
     $mw_id = $res[0]['mw_id'];
     $token_ids = array();
     foreach ($res as $row) {
         if ($mw_id != $row['mw_id']) {
             if (sizeof($token_ids) < 2) {
                 throw new Exception("Too few tokens");
             }
             $out[] = array('id' => $mw_id, 'token_ids' => $token_ids, 'context' => get_context_for_word($token_ids[0], sizeof($token_ids) + self::CONTEXT_WIDTH), 'caption' => self::$CAPTIONS[$mw_type]);
             if (sizeof($out) == $num) {
                 break;
             }
             $token_ids = array();
             $sent_id = $row['sent_id'];
             $mw_id = $row['mw_id'];
             $mw_type = $row['mw_type'];
         }
         if ($sent_id != $row['sent_id']) {
             throw new Exception("Inconsistent sentence id");
         }
         $token_ids[] = $row['tf_id'];
     }
     return $out;
 }
Exemplo n.º 2
0
<?php

require_once '../lib/header_ajax.php';
require_once '../lib/lib_annot.php';
$result['context'] = array();
$tf_id = (int) $_POST['tf_id'];
$dir = isset($_POST['dir']) ? (int) $_POST['dir'] : 0;
if ($tf_id && $dir) {
    $wds = get_context_for_word($tf_id, -1, $dir, 0);
    foreach ($wds['context'] as $word) {
        $result['context'][] = htmlspecialchars($word);
    }
}
log_timing(true);
die(json_encode($result));
Exemplo n.º 3
0
function get_annotation_packet($pool_id, $size, $user_id = 0, $timeout = 0)
{
    global $config;
    $r = sql_fetch_array(sql_query("SELECT status, t.gram_descr, revision, pool_type, doc_link FROM morph_annot_pools p LEFT JOIN morph_annot_pool_types t ON (p.pool_type = t.type_id) WHERE pool_id={$pool_id}"));
    if ($r['status'] != MA_POOLS_STATUS_IN_PROGRESS) {
        throw new Exception();
    }
    $packet = array('my' => 0, 'editable' => 1, 'pool_type' => $r['pool_type'], 'has_manual' => (bool) $r['doc_link'], 'gram_descr' => explode('@', $r['gram_descr']));
    if (!$user_id) {
        $user_id = $_SESSION['user_id'];
    }
    $flag_new = 0;
    $pool_revision = $r['revision'];
    //if the user has something already reserved, let's start with that (but only if the poolid is the same!)
    $res = sql_query("SELECT instance_id, sample_id FROM morph_annot_instances WHERE sample_id IN (SELECT sample_id FROM morph_annot_samples WHERE pool_id={$pool_id}) AND user_id={$user_id} AND answer=0 LIMIT {$size}");
    if (!sql_num_rows($res)) {
        //ok, we should find new samples
        //first, check non-owned ones
        $rejected = get_rejected_samples($user_id, $pool_id);
        $res = get_free_samples($user_id, $pool_id, $size, false, $rejected);
        $flag_new = 1;
        if (!sql_num_rows($res)) {
            //if nothing found, check owned but outdated ones
            $res = get_free_samples($user_id, $pool_id, $size, true, $rejected);
        }
    }
    if (!sql_num_rows($res)) {
        return false;
    }
    if ($timeout > SEC_PER_DAY) {
        $timeout = SEC_PER_DAY;
    }
    if (!$timeout) {
        $timeout = $config['misc']['morph_annot_timeout'];
    }
    //when the timeout will be - same for each sample
    $ts_finish = time() + $timeout;
    if ($flag_new) {
        sql_begin();
    }
    $gram_descr = array();
    while ($r = sql_fetch_array($res)) {
        $r1 = sql_fetch_array(sql_query("SELECT tf_id, rev_text FROM tf_revisions WHERE tf_id = (SELECT tf_id FROM morph_annot_samples WHERE sample_id = " . $r['sample_id'] . " LIMIT 1) AND rev_id <= {$pool_revision} ORDER BY rev_id DESC LIMIT 1"));
        $instance = get_context_for_word($r1['tf_id'], $config['misc']['morph_annot_user_context_size']);
        try {
            $pset = new MorphParseSet($r1['rev_text']);
            $lemmata = array();
            foreach ($pset->parses as $p) {
                $lemmata[] = $p->lemma_text;
            }
            $instance['lemmata'] = implode(', ', array_unique($lemmata));
        } catch (Exception $e) {
            // some old revisions may be broken, no big deal
        }
        $instance['id'] = $r['instance_id'];
        $instance['sample_id'] = $r['sample_id'];
        $packet['instances'][] = $instance;
        if ($flag_new) {
            sql_query("UPDATE morph_annot_instances SET user_id='{$user_id}', ts_finish='{$ts_finish}' WHERE instance_id= " . $r['instance_id'] . " LIMIT 1");
        }
    }
    if ($flag_new) {
        sql_commit();
    }
    $packet['current_annotators'] = get_current_annotators($user_id);
    return $packet;
}
Exemplo n.º 4
0
function get_search_results($query, $exact_form = true)
{
    $forms = array($query);
    if (!$exact_form) {
        include_once 'lib_dict.php';
        $forms = get_all_forms_by_lemma_text($query);
    }
    $r = sql_fetch_array(sql_query("\n        SELECT COUNT(*)\n        FROM form2tf\n        WHERE form_text IN (" . join(",", array_map('sql_quote', $forms)) . ")\n    "));
    $out = array('total' => $r[0], 'results' => array());
    $res = sql_query("\n        SELECT tf_id\n        FROM form2tf\n        WHERE form_text IN (" . join(",", array_map('sql_quote', $forms)) . ")\n        LIMIT 100\n    ");
    while ($r = sql_fetch_array($res)) {
        $out['results'][] = get_context_for_word($r['tf_id'], 20);
    }
    return $out;
}
Exemplo n.º 5
0
function get_pending_updates($skip = 0, $limit = 500)
{
    check_permission(PERM_DICT);
    $out = array('revisions' => array(), 'header' => array());
    $r = sql_fetch_array(sql_query("SELECT COUNT(*) cnt FROM updated_tokens"));
    $out['cnt_tokens'] = $r['cnt'];
    $r = sql_fetch_array(sql_query("SELECT COUNT(*) cnt FROM updated_forms"));
    $out['cnt_forms'] = $r['cnt'];
    $res = sql_query("SELECT rev_id FROM dict_revisions WHERE f2l_check=0 LIMIT 1");
    $out['outdated_f2l'] = sql_num_rows($res);
    // header
    $res = sql_query("\n        SELECT dict_revision, lemma_id, lemma_text, COUNT(token_id) AS cnt\n        FROM updated_tokens ut\n        LEFT JOIN dict_revisions dr ON (ut.dict_revision = dr.rev_id)\n        LEFT JOIN dict_lemmata dl USING (lemma_id)\n        GROUP BY dict_revision\n        ORDER BY dict_revision\n    ");
    $sum = 0;
    // to count pages
    while ($r = sql_fetch_array($res)) {
        $out['header'][] = array('lemma' => $r['lemma_text'], 'lemma_id' => $r['lemma_id'], 'revision' => $r['dict_revision'], 'count' => $r['cnt'], 'skip' => $sum);
        $sum += $r['cnt'];
    }
    // main table
    $res = sql_pe("\n        SELECT token_id, tf_text, sent_id, dict_revision, lemma_id, dr.set_id,\n            tfr.rev_text AS token_rev_text\n        FROM updated_tokens ut\n        LEFT JOIN dict_revisions dr ON (ut.dict_revision = dr.rev_id)\n        LEFT JOIN tokens tf ON (ut.token_id = tf.tf_id)\n        LEFT JOIN tf_revisions tfr USING (tf_id)\n        WHERE tfr.is_last = 1\n        ORDER BY dict_revision, token_id\n        LIMIT ?, ?\n    ", array($skip, $limit));
    $out['pages'] = array('active' => $limit ? floor($skip / $limit) : 0, 'total' => $limit ? $out['cnt_tokens'] / $limit : 1);
    $t = array();
    $last = NULL;
    foreach ($res as $r) {
        if ($last && $last['dict_revision'] != $r['dict_revision']) {
            $out['revisions'][] = array('tokens' => $t, 'id' => $last['dict_revision'], 'diff' => dict_diff($last['lemma_id'], $last['set_id']));
            $t = array();
        }
        $context = get_context_for_word($r['token_id'], 4);
        $context['context'][$context['mainword']] = '<b>' . htmlspecialchars($context['context'][$context['mainword']]) . '</b>';
        $t[] = array('id' => $r['token_id'], 'text' => $r['tf_text'], 'sentence_id' => $r['sent_id'], 'context' => join(' ', $context['context']), 'is_unkn' => preg_match('/v="UNKN"/', $r['token_rev_text']), 'human_edits' => check_for_human_edits($r['token_id']));
        $last = $r;
    }
    if (sizeof($t)) {
        $out['revisions'][] = array('tokens' => $t, 'id' => $last['dict_revision'], 'diff' => dict_diff($last['lemma_id'], $last['set_id']));
    }
    return $out;
}