|
1 <?php |
|
2 namespace CorpusParole\Libraries\Transcript; |
|
3 |
|
4 use CorpusParole\Models\Document; |
|
5 |
|
6 use EasyRdf\Literal; |
|
7 use DateTime; |
|
8 |
|
9 |
|
10 abstract class TranscriptConverterBase implements Transcriptconverterinterface { |
|
11 |
|
12 public function __construct(Document $document, $source, string $creationDate = null) { |
|
13 $this->resJSON = []; |
|
14 $this->document = $document; |
|
15 $this->source = $source; |
|
16 $this->creationDate = $creationDate; |
|
17 $this->mediaRefId = null; |
|
18 if(is_null($this->creationDate)) { |
|
19 $this->creationDate = (new DateTime())->format(DateTime::ATOM); |
|
20 } |
|
21 } |
|
22 |
|
23 public function addHeaderMeta() { |
|
24 |
|
25 $this->resJSON['format'] = 'http://advene.org/ns/cinelab/'; |
|
26 $this->resJSON["@context"] = [ |
|
27 "dc" => "http://purl.org/dc/elements/1.1/", |
|
28 "corpus" => "http://corpusdelaparole.huma-num.fr/ns/corpus#" |
|
29 ]; |
|
30 |
|
31 $title = $this->getSourceTitle(); |
|
32 if(is_null($title)) { |
|
33 $docTitle = $this->document->getTitle(); |
|
34 if($docTitle instanceof Literal) { |
|
35 $titleLanguage = $docTitle->getLang(); |
|
36 $title = $titleLanguage?[$titleLanguage => $docTitle->getValue()]:$docTitle->getValue(); |
|
37 } |
|
38 elseif(!is_null($docTitle)) { |
|
39 $title = (string)$docTitle; |
|
40 } |
|
41 } |
|
42 |
|
43 $this->title = null; |
|
44 |
|
45 if(is_string($title)) { |
|
46 $this->title = $title; |
|
47 } |
|
48 elseif(is_array($title) && count($title) == 1) { |
|
49 $this->title = [ "@language" => key($title), "@value" => current($title)]; |
|
50 } |
|
51 elseif(is_array($title)) { |
|
52 $this->title = array_reduce(array_keys($title),function($res, $k) use ($title) { |
|
53 array_push($res, ["@language" => $k, "@value" => $title[$k]]); |
|
54 return $res; |
|
55 }, []); |
|
56 } |
|
57 |
|
58 $this->resJSON['meta'] = [ |
|
59 'dc:creator' => config('corpusparole.transcript_default_creator'), |
|
60 'dc:contributor' => config('corpusparole.transcript_default_creator'), |
|
61 'dc:created' => $this->creationDate, |
|
62 'dc:modified' => $this->creationDate, |
|
63 'dc:title' => $this->title |
|
64 ]; |
|
65 } |
|
66 |
|
67 // get document title |
|
68 public function getSourceTitle() { |
|
69 return null; |
|
70 } |
|
71 |
|
72 // add media |
|
73 public function buildMedias() { |
|
74 |
|
75 $medias = []; |
|
76 |
|
77 $i = 1; |
|
78 foreach($this->document->getMediaArray() as $documentMedia) |
|
79 { |
|
80 if(0 !== strpos($documentMedia['format'], 'audio/')) { |
|
81 continue; |
|
82 } |
|
83 |
|
84 $mId = $this->document->getId()."_m$i"; |
|
85 $i++; |
|
86 if(is_null($this->mediaRefId) || $documentMedia['master']) { |
|
87 $this->mediaRefId = $mId; |
|
88 } |
|
89 array_push($medias, [ |
|
90 'id' => $mId, |
|
91 'origin' => 0, |
|
92 'unit' => 'ms', |
|
93 'url' => $documentMedia['url'], |
|
94 'meta' => [ |
|
95 'dc:duration' => $documentMedia['extent_ms'], |
|
96 'dc:title' => $this->title, |
|
97 'dc:format' => $documentMedia['format'], |
|
98 'corpus:master' => filter_var($documentMedia['master'], FILTER_VALIDATE_BOOLEAN) |
|
99 ] |
|
100 ]); |
|
101 } |
|
102 |
|
103 return $medias; |
|
104 |
|
105 } |
|
106 |
|
107 public function getMediaRefId() { |
|
108 return $this->mediaRefId; |
|
109 } |
|
110 |
|
111 public abstract function parseSource(); |
|
112 |
|
113 // add resources |
|
114 public function buildResources() { |
|
115 return []; |
|
116 } |
|
117 |
|
118 // add lists |
|
119 public function buildLists() { |
|
120 return []; |
|
121 } |
|
122 |
|
123 // add annotation types |
|
124 public function buildAnnotationTypes() { |
|
125 return []; |
|
126 } |
|
127 |
|
128 // add annotations |
|
129 public abstract function buildAnnotations(); |
|
130 |
|
131 protected function buildTextvalue($text, $language) { |
|
132 if(empty($language)) { |
|
133 return $text; |
|
134 } else { |
|
135 return ['@value' => $text, '@language' => $language]; |
|
136 } |
|
137 } |
|
138 |
|
139 /** |
|
140 * Convert xml to json. |
|
141 * return an PHP array ready for serialization |
|
142 */ |
|
143 function convertToJson() { |
|
144 |
|
145 $this->addHeaderMeta(); |
|
146 |
|
147 $this->resJSON['medias'] = $this->buildMedias(); |
|
148 |
|
149 $this->parseSource(); |
|
150 |
|
151 $this->resJSON['resources'] = $this->buildResources(); |
|
152 $this->resJSON['lists'] = $this->buildLists(); |
|
153 $this->resJSON['annotation-types'] = $this->buildAnnotationTypes(); |
|
154 $this->resJSON['annotations'] = $this->buildAnnotations(); |
|
155 |
|
156 return $this->resJSON; |
|
157 } |
|
158 |
|
159 } |