blob: 5d992127d41eaeec14115ed5c1d20260fd618a10 [file] [log] [blame]
Akron33f1dcb2016-10-29 17:27:23 +02001package Krawfish::Koral::Query::Span;
Akronee06a132017-12-08 16:59:27 +01002use Role::Tiny::With;
Akron2814aa92017-09-24 22:00:35 +02003use Krawfish::Util::Constants ':PREFIX';
Akron33f1dcb2016-10-29 17:27:23 +02004use Krawfish::Koral::Query::Term;
Akronc048b182017-06-13 01:29:03 +02005use Krawfish::Log;
Akron06eb4d32016-11-11 14:05:52 +01006use Scalar::Util qw/blessed/;
Akron33f1dcb2016-10-29 17:27:23 +02007use strict;
8use warnings;
9
Akronee06a132017-12-08 16:59:27 +010010with 'Krawfish::Koral::Query';
11
Akronbc7dd432017-07-18 14:21:51 +020012use constant DEBUG => 0;
Akronc048b182017-06-13 01:29:03 +020013
Akron33f1dcb2016-10-29 17:27:23 +020014sub new {
Akron02203f12017-12-09 13:55:34 +010015 my ($class, $span) = @_;
16
17 unless ($span) {
18 }
Akron33f1dcb2016-10-29 17:27:23 +020019
Akron06eb4d32016-11-11 14:05:52 +010020 # Span is a string
21 unless (blessed $span) {
22 return bless {
Akron2814aa92017-09-24 22:00:35 +020023 operands => [Krawfish::Koral::Query::Term->new(SPAN_PREF . $span)]
Akron06eb4d32016-11-11 14:05:52 +010024 }, $class;
25 };
26
27 bless {
Akron5b6264f2017-07-19 01:14:01 +020028 operands => [$span]
Akron06eb4d32016-11-11 14:05:52 +010029 }, $class;
Akron33f1dcb2016-10-29 17:27:23 +020030};
31
Akronb00c2be2017-08-16 14:45:07 +020032
Akron440799d2017-12-26 14:55:03 +010033# Type
34sub type {
35 'span';
36};
Akron774c5db2016-11-09 16:11:38 +010037
Akronb00c2be2017-08-16 14:45:07 +020038
Akron5b6264f2017-07-19 01:14:01 +020039# There are no classes allowed in spans
Akron5ddc38f2017-07-18 00:16:22 +020040sub remove_classes {
41 $_[0];
42};
Akron06eb4d32016-11-11 14:05:52 +010043
Akronb00c2be2017-08-16 14:45:07 +020044
Akron440799d2017-12-26 14:55:03 +010045# Serialize to KQ
Akron33f1dcb2016-10-29 17:27:23 +020046sub to_koral_fragment {
47 my $self = shift;
Akron06eb4d32016-11-11 14:05:52 +010048 my $span = {
49 '@type' => 'koral:span'
Akron33f1dcb2016-10-29 17:27:23 +020050 };
Akron5b6264f2017-07-19 01:14:01 +020051 if ($self->operand) {
52 $span->{wrap} = $self->operand->to_koral_fragment
Akron33f1dcb2016-10-29 17:27:23 +020053 };
Akron06eb4d32016-11-11 14:05:52 +010054
55 return $span;
Akron33f1dcb2016-10-29 17:27:23 +020056};
57
Akron17c0a3d2017-06-11 23:19:16 +020058
59# TODO: Some error handling
60sub normalize {
61 return $_[0];
62};
63
Akron3ab2e972017-08-02 19:10:10 +020064
Akron440799d2017-12-26 14:55:03 +010065# Turn terms into ids
Akron3ab2e972017-08-02 19:10:10 +020066sub identify {
Akronc048b182017-06-13 01:29:03 +020067 my ($self, $dict) = @_;
68
Akron3ab2e972017-08-02 19:10:10 +020069 # This is currently not supported
70 unless ($self->is_regex) {
Akronc048b182017-06-13 01:29:03 +020071
Akron3ab2e972017-08-02 19:10:10 +020072 my $term = $self->to_term;
73
74 print_log('kq_span', "Translate span $term to term_id") if DEBUG;
75
Akron2814aa92017-09-24 22:00:35 +020076 my $term_id = $dict->term_id_by_term(SPAN_PREF . $term);
Akron5864cf02017-08-02 19:38:41 +020077
Akron5a5595b2017-09-10 13:00:57 +020078 return $self->builder->nowhere unless defined $term_id;
Akron5864cf02017-08-02 19:38:41 +020079
Akron3d1df332017-12-23 16:21:21 +010080 return Krawfish::Koral::Query::Term->new($term_id);
Akron3ab2e972017-08-02 19:10:10 +020081 };
82
83 warn 'Regexes are currently not supported';
Akronc048b182017-06-13 01:29:03 +020084};
Akron17c0a3d2017-06-11 23:19:16 +020085
Akron55fb3082017-07-18 13:24:53 +020086
Akron5864cf02017-08-02 19:38:41 +020087# TODO:
88# Currently not supported
89sub is_regex {
90 0;
91};
92
93
94sub to_term {
95 $_[0]->operand->to_string;
96};
97
Akron48fabe52017-08-07 16:48:12 +020098
Akron17c0a3d2017-06-11 23:19:16 +020099# Todo: May be more complicated
100sub optimize {
Akron48fabe52017-08-07 16:48:12 +0200101 warn 'Span queries need to be identified before';
Akron17c0a3d2017-06-11 23:19:16 +0200102};
103
Akron48fabe52017-08-07 16:48:12 +0200104
Akron704ec062017-07-24 15:46:21 +0200105# A span may have length 0 in case it is an empty annotation
106# like a page break
107sub min_span {
108 0;
109};
110
111
112# A termGroup always spans exactly one token
113sub max_span {
114 return 0 if $_[0]->is_null;
115 -1;
116};
Akron17c0a3d2017-06-11 23:19:16 +0200117
Akronc3657bf2016-10-31 00:15:43 +0100118
Akron440799d2017-12-26 14:55:03 +0100119# Not unsorted
120sub maybe_unsorted {
121 0;
122};
Akronc3657bf2016-10-31 00:15:43 +0100123
Akron5864cf02017-08-02 19:38:41 +0200124
Akron944091b2016-11-24 16:40:58 +0100125# Todo: Change the term_type!
Akron02203f12017-12-09 13:55:34 +0100126sub from_koral {
127 my ($class, $kq) = @_;
Akron3feb4d82017-12-12 19:33:46 +0100128 my $qb = $class->builder;
Akron02203f12017-12-09 13:55:34 +0100129
130 # No wrap
131 unless ($kq->{'wrap'}) {
132
133 # TODO:
134 # This should return an error!
135 warn 'Wrap not supported!'
136 }
137
138 # Wrap is a term
139 else {
140 my $wrap = $kq->{wrap};
141 if ($wrap->{'@type'} eq 'koral:term') {
Akron3feb4d82017-12-12 19:33:46 +0100142 return $class->new($qb->from_koral_term($wrap)->term_type('span'));
Akron02203f12017-12-09 13:55:34 +0100143 }
144 else {
145 warn 'Wrap type not supported!'
146 };
147 }
148};
Akron944091b2016-11-24 16:40:58 +0100149
Akron5864cf02017-08-02 19:38:41 +0200150
Akron440799d2017-12-26 14:55:03 +0100151# Stringification
Akrona211bf52016-10-29 18:03:29 +0200152sub to_string {
Akron3d1df332017-12-23 16:21:21 +0100153 my ($self, $id) = @_;
154 return '<' . $self->operand->to_string($id) . '>';
Akrona211bf52016-10-29 18:03:29 +0200155};
156
Akron440799d2017-12-26 14:55:03 +0100157
Akron33f1dcb2016-10-29 17:27:23 +02001581;