-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.php
97 lines (79 loc) · 2.09 KB
/
convert.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
<?php
require('src/FastText.php');
$rating_count = [
1 => 0,
2 => 0,
3 => 0,
4 => 0,
5 => 0,
6 => 0,
];
$valid_dirs = ['train', 'dev', 'test'];
if ( empty($argv[1]) || array_search($argv[1], $valid_dirs) === false )
{
die("Please specify which directory to convert. [dev,test of train]\n");
}
$set = $argv[1];
$fasttext = new FastText("norec_{$set}.txt");
$fasttext->uniqueTextsOnly();
$path = "conllu/$set/";
$d = dir($path);
echo "Path: " . $d->path . "\n";
$total_count = 0;
while (false !== ($file = $d->read()))
{
if ( pathinfo($file, PATHINFO_EXTENSION) == 'conllu')
{
//echo $file."\n";
$total_count++;
processFile( $path . $file );
if ( $total_count % 100 == 0 )
echo " {$total_count}\n";
}
}
$d->close();
print_r( $rating_count );
function processFile( $file )
{
global $rating_count, $fasttext;
$content = file_get_contents($file);
preg_match_all("/^# ([a-z _]+) = (.+)$/miu", $content, $m);
$rating = 0;
$title = '';
$language = '';
$text = '';
for( $x =0; $x < count($m[0]); $x++ )
{
$key = trim($m[1][$x]);
$value = trim( $m[2][$x] );
if ( $key == 'text' )
{
if ( empty($title) )
{
$title = $value;
$text .= $value . ".\n";
}
$text .= $value . "\n";
}
elseif ( $key == 'language' )
$language = $value;
elseif ( $key == 'rating' )
$rating = intval($value);
}
$rating_count[$rating]++;
//echo "TEXT: {$text}\n";
//echo "TITLE [{$language}]: {$title}\n";
//echo "LANGUAGE: {$language}\n";
//echo " --> rating: {$rating}\n";
$fasttext->save($rating, $text);
}
function cleanText( $text )
{
$text = mb_strtolower($text);
$text = str_replace(
['!', '?', ',', '.', '»', '–','«', '(', ')', '[', ']', '/', '\\', '+', '£', '$', '&', '=', ':', ';'], // '-',
' ',
$text);
$text = mb_eregi_replace('\\s+', " ", $text);
return trim( $text );
}