-
Notifications
You must be signed in to change notification settings - Fork 0
/
_commontools.php
191 lines (178 loc) · 7.74 KB
/
_commontools.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
require_once('_db/_db.php');
function fileExtension($str) {
$name_parts = explode('.', $str);
if (count($name_parts)==1) {
return '';
}
else {
return $name_parts[count($name_parts)-1];
}
}
function urlStr($str, $keepExtension=true) {
// Supprime les caractères non-ASCII, remplace les espaces par des underscores et retourne le string en caractères minuscules
$extension = fileExtension($str);
if ($extension!=='') {
$str = substr($str, 0, strlen($str)-strlen($extension)-1);
}
$url = strtolower( preg_replace('/[^\a-zA-Z0-9_]/', '', str_replace(' ', '_', stripslashes(trim($str)))) );
return $url.($keepExtension?'.'.$extension:'');
}
function splitToSentences($text, $implodeDelimiters = true) {
$sentences = preg_split('/([^M][\.…!?])/', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
if ($implodeDelimiters && count($sentences)>1) {
for ($i=0; $i<count($sentences); $i+=2) {
$sentences[$i] = trim($sentences[$i].$sentences[$i+1]); // Get back separators and append them to end of sentences (otherwise we lose the "M." and punctuation marks)
$sentences[$i+1] = ''; // By definition, separators are odd-numbered
}
$sentences = array_values(array_filter($sentences)); // Renumber indexes
}
return $sentences;
}
function readerFormat($text) {
return preg_replace('/_([^\s][^_]*[^\s])_/', '<em>$1</em>', $text);
}
function updateSentencesRefsCache($chapter_id = -1, $project_id = -1) {
if ($project_id==-1) {
$project_id = $_SESSION['project_id'];
}
$cacheFilter = array('project_id' => $project_id);
if ($chapter_id>-1) {
$cacheFilter['chapter_id'] = $chapter_id;
}
db_d('sentences_refs_cache', $cacheFilter);
$chapterFilter = array('project_id' => $project_id);
if ($chapter_id>-1) {
$chapterFilter['id'] = $chapter_id;
}
$entities = array();
$entities_r = db_s('entities', array('project_id' => $project_id, '!status' => 'rejected'));
while ($ent = db_fetch($entities_r)) {
$entities[$ent['name']] = $ent['id'];
$synonyms = explode(',', $ent['synonyms']);
foreach ($synonyms as $syn) {
if ($syn!='') {
$entities[$syn] = $ent['id'];
}
}
}
$chapters_r = db_s('chapters', $chapterFilter);
db_x('ALTER TABLE sentences_refs_cache DISABLE KEYS;');
while ($chap = db_fetch($chapters_r)) {
$sentences = array_merge(array($chap['title']), splitToSentences($chap['text']));
foreach ($sentences as $sidx => $sent) {
$sentEntities = array();
foreach ($entities as $ent => $ent_id) {
if (strstr($sent, $ent)) {
$sentEntities[] = $ent_id;
}
}
$sentEntities = array_unique($sentEntities);
foreach ($sentEntities as $ent_id) {
db_i('sentences_refs_cache', array('entity_id' => $ent_id, 'project_id' => $project_id, 'chapter_id' => $chap['id'], 'sentence' => $sidx));
}
}
}
db_x('ALTER TABLE sentences_refs_cache ENABLE KEYS;');
}
function duplicateProject($refProjectId) {
$currentProject = db_fetch(db_s('projects', array('id' => $refProjectId)));
$currentProject['version'] = $currentProject['version']+1;
unset($currentProject['id']);
$newProjectId = db_i('projects', $currentProject);
$chapters_r = db_s('chapters', array('project_id' => $refProjectId), array('id' => 'ASC'));
while ($chapter = db_fetch($chapters_r)) {
unset($chapter['id']);
$chapter['project_id'] = $newProjectId;
db_i('chapters', $chapter);
}
$entities_r = db_s('entities', array('project_id' => $refProjectId), array('id' => 'ASC'));
while ($entity = db_fetch($entities_r)) {
$oldEntityId = $entity['id'];
unset($entity['id']);
$entity['project_id'] = $newProjectId;
$newEntityId = db_i('entities', $entity);
db_u('chapters', array('project_id' => $newProjectId, 'place' => $oldEntityId), array('place' => $newEntityId));
}
updateSentencesRefsCache(-1, $newProjectId);
return $newProjectId;
}
function mergeEntities($project_id, $id_main, $id_synonym) {
$main = db_fetch(db_s('entities', array('id' => $id_main)));
$synonym = db_fetch(db_s('entities', array('id' => $id_synonym)));
$oldSynonyms = explode(',', $main['synonyms']);
$synonyms = array_filter(array_merge($oldSynonyms, array($synonym['name'])));
db_u('entities', array('id' => $id_main), array('synonyms' => implode(',', $synonyms), 'status' => 'corrected'));
db_u('entities', array('id' => $id_synonym), array('status' => 'rejected'));
db_u('sentences_refs_cache', array('entity_id' => $id_synonym, 'project_id' => $project_id), array('entity_id' => $id_main));
}
function defineDefaultChapterTimes() {
$mois = array('janvier','février','mars','avril','mai','juin','juillet','août','septembre','octobre','novembre','décembre');
$chapters_r = db_s('chapters', array('project_id' => $_SESSION['project_id']), array('tome' => 'ASC', 'number' => 'ASC'));
while ($chapter = db_fetch($chapters_r)) {
$words = preg_split('/[\s\.,;:\?!«»\(\)"]/', $chapter['text']);
foreach ($words as $w) {
if (is_numeric($w)) {
if ($w>1500 && $w<2500) {
}
}
elseif (in_array($w, $mois)) {
}
}
}
}
function defineDefaultChapterPlacesAndEntityColors() {
// Set default colors to the 2/3 most represented entities _________________________________________________________________________________________________
$entities = array();
$ent_r = db_s('entities', array('project_id' => $_SESSION['project_id']));
while ($ent = db_fetch($ent_r)) {
$quotes = db_count(db_s('sentences_refs_cache', array('entity_id' => $ent['id'], 'project_id' => $_SESSION['project_id'])));
@$entities[$ent['class']][$ent['id']] = $quotes;
}
$classes = array('place', 'character');
foreach ($classes as $class) {
$i = 1;
$sub = $entities[$class];
arsort($sub);
foreach ($sub as $e => $_) {
if ($i<16) {
db_u('entities', array('id' => $e), array('color' => $i));
}
$i++;
}
}
// Guess places of each chapter from the most cited ones ___________________________________________________________________________________________________
$chapters_r = db_s('chapters', array('project_id' => $_SESSION['project_id']), array('tome' => 'ASC', 'number' => 'ASC'));
$lastGuess = '';
while ($chapter = db_fetch($chapters_r)) {
$placeGuess_r = db_x('select count(entity_id) c, entity_id from (sentences_refs_cache src left outer join entities e on src.entity_id=e.id) where src.project_id='.$_SESSION['project_id'].' and chapter_id='.$chapter['id'].' and e.class="place" group by entity_id order by c desc', false);
if ($placeGuess = db_fetch($placeGuess_r)) {
db_u('chapters', array('id' => $chapter['id']), array('place' => $placeGuess['entity_id']));
$lastGuess = $placeGuess['entity_id'];
}
else {
db_u('chapters', array('id' => $chapter['id']), array('place' => $lastGuess));
}
}
// Fill-in default notices _________________________________________________________________________________________________________________________________
$ent_r = db_s('entities', array('project_id' => $_SESSION['project_id']));
while ($ent = db_fetch($ent_r)) {
if ($firstSentence = db_fetch(db_s('sentences_refs_cache', array('entity_id' => $ent['id'], 'project_id' => $_SESSION['project_id']), array('chapter_id' => 'ASC', 'sentence' => 'ASC')))) {
$chap = db_fetch(db_s('chapters', array('id' => $firstSentence['chapter_id'])));
$chapSent = splitToSentences($chap['text']);
db_u('entities', array('id' => $ent['id']), array('notice' => $chapSent[$firstSentence['sentence']].' ('.$chap['tome'].'.'.$chap['chapter'].'/'.$firstSentence['sentence'].')'));
}
}
}
function loadStoryInSession(){
$_SESSION['story'] = array('stock' => array(), 'tomes' => array());
$chapters_r = db_s('chapters', array('project_id' => $_SESSION['project_id']), array('tome' => 'ASC', 'number' => 'ASC'));
while ($chapter = db_fetch($chapters_r)) {
if ($chapter['tome'] == null) {
$_SESSION['story']['stock'][] = $chapter;
}
else {
@$_SESSION['story']['tomes'][$chapter['tome']][] = $chapter;
}
}
}