-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsynonyms.php
More file actions
50 lines (35 loc) · 1.14 KB
/
synonyms.php
File metadata and controls
50 lines (35 loc) · 1.14 KB
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
<?php
// Change memory limit
ini_set('memory_limit', '1024M');
// Source data file
$dataFile = 'data/mobythes.aur';
// Structured data file
$finalFile = 'structured/synonyms.sql';
// Check and open source file line by line
$lines = file($dataFile, FILE_IGNORE_NEW_LINES);
if (!$lines)
exit;
// Check and open Structured data file for write
$structuredFile = fopen($finalFile, 'a');
if (!$structuredFile)
exit;
// Define group number for every line (for every word and it's synonyms)
$groupId = 1;
// Handle every line
foreach ($lines as $line) {
// Explode line by comma
$commaSeparated = explode(',', $line);
// Create Mysql insert query
$structuredQuery = "\r\nINSERT INTO synonyms (`id`, `group`, `word`) VALUES \r\n";
// Create query for every word
foreach ($commaSeparated as $word) {
$structuredQuery .= '("", '.$groupId.', "'.$word.'"),';
}
$groupId++;
// Edit query string for converting last comma to semicolon
$finalQuery = substr($structuredQuery, 0, -1).';';
// Add string to our file
fwrite($structuredFile, $finalQuery."\r\n");
}
// Close file
fclose($structuredFile);