line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
## -*- Mode: CPerl -*- |
2
|
|
|
|
|
|
|
## File: DiaColloDB::Document::JSON.pm |
3
|
|
|
|
|
|
|
## Author: Bryan Jurish <moocow@cpan.org> |
4
|
|
|
|
|
|
|
## Description: collocation db, source document, JSON |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package DiaColloDB::Document::JSON; |
7
|
1
|
|
|
1
|
|
7
|
use DiaColloDB::Document; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
8
|
1
|
|
|
1
|
|
5
|
use DiaColloDB::Utils qw(:json); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
9
|
1
|
|
|
1
|
|
250
|
use strict; |
|
1
|
|
|
|
|
12
|
|
|
1
|
|
|
|
|
242
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
##============================================================================== |
12
|
|
|
|
|
|
|
## Globals & Constants |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our @ISA = qw(DiaColloDB::Document); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
##============================================================================== |
17
|
|
|
|
|
|
|
## Constructors etc. |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
## $doc = CLASS_OR_OBJECT->new(%args) |
20
|
|
|
|
|
|
|
## + %args, object structure: |
21
|
|
|
|
|
|
|
## ( |
22
|
|
|
|
|
|
|
## ##-- document data |
23
|
|
|
|
|
|
|
## date =>$date, ##-- year |
24
|
|
|
|
|
|
|
## tokens =>\@tokens, ##-- tokens, including undef for EOS |
25
|
|
|
|
|
|
|
## meta =>\%meta, ##-- document metadata (e.g. author, title, collection, ...) |
26
|
|
|
|
|
|
|
## ) |
27
|
|
|
|
|
|
|
## + each token in @tokens is a HASH-ref {w=>$word,p=>$pos,l=>$lemma,...} |
28
|
|
|
|
|
|
|
sub new { |
29
|
0
|
|
|
0
|
1
|
|
my $that = shift; |
30
|
0
|
|
|
|
|
|
my $doc = $that->SUPER::new( |
31
|
|
|
|
|
|
|
@_, ##-- user arguments |
32
|
|
|
|
|
|
|
); |
33
|
0
|
|
|
|
|
|
return $doc; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
##============================================================================== |
37
|
|
|
|
|
|
|
## API: I/O |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
## $ext = $doc->extension() |
40
|
|
|
|
|
|
|
## + default extension, for Corpus::Compiled |
41
|
|
|
|
|
|
|
sub extension { |
42
|
0
|
|
|
0
|
0
|
|
return '.json'; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
##-------------------------------------------------------------- |
46
|
|
|
|
|
|
|
## API: I/O: parse |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
## $bool = $doc->fromFile($filename_or_fh, %opts) |
49
|
|
|
|
|
|
|
## + parse tokens from $filename_or_fh |
50
|
|
|
|
|
|
|
## + %opts : clobbers %$doc |
51
|
|
|
|
|
|
|
sub fromFile { |
52
|
0
|
|
|
0
|
1
|
|
my ($doc,$file,%opts) = @_; |
53
|
0
|
0
|
|
|
|
|
$doc = $doc->new() if (!ref($doc)); |
54
|
0
|
|
|
|
|
|
@$doc{keys %opts} = values %opts; |
55
|
0
|
0
|
|
|
|
|
$doc->{label} = ref($file) ? "$file" : $file; |
56
|
0
|
|
|
|
|
|
my $data = loadJsonFile($file); |
57
|
0
|
0
|
|
|
|
|
$doc->logconfess("fromFile(): failed to load JSON object from '$file'") |
58
|
|
|
|
|
|
|
if (!UNIVERSAL::isa($data,'HASH')); |
59
|
0
|
|
|
|
|
|
@$doc{keys %$data} = values %$data; |
60
|
0
|
|
|
|
|
|
return $doc; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
##============================================================================== |
64
|
|
|
|
|
|
|
## Footer |
65
|
|
|
|
|
|
|
1; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
__END__ |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
|