-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminingMSCpatterns.php
More file actions
147 lines (94 loc) · 5.68 KB
/
Copy pathminingMSCpatterns.php
File metadata and controls
147 lines (94 loc) · 5.68 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
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
<?php
$start = microtime(true);
//variables
$suport = 0.02;
$totalTransact = 2493;
$candidate1 = array('NPV','VBG','NN','NPO','VB','JJ','NNS','VBZ','NPSP','RB','VBN','NPG','W','VBD','JJS','VBP','JJR','RBR','PRP','RBS','IN','Z','CC');
$candidate2 = array('time','person','state',' body',' change',' contact','stative','all','artifact','action','relation','communication','motion',
'social','consumption','possession','creation','cognition','object','shape','location','attribute','group','phenomenon',
'quantity', 'plant','perception','feeling','competition','food','event','organization','animal','emotion','process','substance',
'motive','pert','weather','ppl', 'other', '-');
$outPut = explode("\n", file_get_contents('msc-microposts2016test.txt'));
$cms = cmsCandidate($candidate1, $candidate2, $outPut, $suport, $totalTransact);
cmsCandidateMining($cms, $cmsInteract = $cms, $totalTransact, $suport);
//Para determinar padrão de tamanho 1, combinando tag e sentido em uma expressão regular.
//Retorna todas as combinações e faz um parser conforme um valor de suporte.
function cmsCandidate($candidate1, $candidate2, $outPut, $suport, $totalTransact){
foreach($candidate1 as $cand1){
foreach($candidate2 as $cand2){
$pattern = '/'.$cand1.'\s.*'.$cand2.'/';
//Retorna todos os pares chave => valor onde o valor tem match com pattern
$cmsFilter = preg_grep($pattern, $outPut);
if (!empty($cmsFilter) && (count($cmsFilter)/$totalTransact) >= $suport) {
$cmsCandidateMining[$cand1.','.$cand2] = cmsParse($cmsFilter);
}
}
}
putCmsMining($cmsCandidateMining);
return $cmsCandidateMining;
}
function cmsParse($cmsFilter){
foreach($cmsFilter as $str){
$str = explode("\t", $str);
$cms[$str[0]] = $str[2];
}
return $cms;
}
//faz a combinação entre padrão de tamanho 1 x tamanho 1, padrão de tamanho 1 x tamanho 2... de maneira recursiva
function cmsCandidateMining($cms, $cmsInteract, $totalTransact, $suport){
foreach($cms as $key1 => $idTweet1){
foreach($cmsInteract as $key2 => $idTweet2){
if($key1 != $key2){
$cmsIntersect = cmsIntersect($key1, $idTweet1, $key2, $idTweet2, $totalTransact, $suport);
// if (!empty($cmsIntersect) && (count($cmsIntersect)/$totalTransact) >= $suport)
if (!empty($cmsIntersect)) $cmsCandidateMining[$key1.' '.$key2] = $cmsIntersect;
}
}
}
if(!empty($cmsCandidateMining)){
putCmsMining($cmsCandidateMining);
cmsCandidateMining($cms, $cmsCandidateMining, $totalTransact, $suport);
}
}
// cmsIntersect: contém a intersecção entre os padrões preservando o par 'id cms' => 'id tweet', com alinhamento à esquerda.
// Verifica o suporte do resultado.
// cmsIntersectFilter: filtra os valores de mesmo 'id tweet' entre idTweet1 e idTweet2 (array usado para interar os padrões), e então
// verifica a ordem dos componentes, 'id cms'.
function cmsIntersect($key1, $idTweet1, $key2, $idTweet2, $totalTransact, $suport){
$cmsPlus = array();
$cmsIntersect = array_intersect($idTweet1, $idTweet2);
if (!empty($cmsIntersect) && (count($cmsIntersect)/$totalTransact) >= $suport){
//echo "o valor de $key1 $key2 tem: ". count($cmsIntersect)." <br >";
foreach ($cmsIntersect as $idCms => $idTweet){
$cmsIntersectFilter = array_filter($idTweet2, function($value, $key) use ($idCms, $idTweet) { return $value == $idTweet && $key > $idCms; }, ARRAY_FILTER_USE_BOTH);
if (!empty($cmsIntersectFilter)){
//echo "$key1 e $key2 o id $idCms e $idTweet: <br >";
foreach($cmsIntersectFilter as $key => $value){ $cmsPlus[$idCms.'-'.$key] = $value; }
}
}
}
return $cmsPlus;
}
// apenas para guardar os padrões minerados em dois arquivos, um com os padrões sem um sentido definido (cmsInduciton-microposts2016test-pattern),
// e o outro com os padrões candidatos (cms-microposts2016test-pattern).
function putCmsMining($cmsCandidateMining){
global $start;
// filtra os padrões com sentido indefinido, ex.: NPV,-
$cmsCandidateInduction = array_filter($cmsCandidateMining, function($key) { return substr_count($key, '-') >= 1; }, ARRAY_FILTER_USE_KEY);
//tamanho do padrão
$patternLen = substr_count(key($cmsCandidateInduction), ',');
//quantidade de padrões
$qtd = count($cmsCandidateInduction);
// guardar o conteúdo anterior
file_put_contents("weakly-disambiguated-msc$patternLen-$qtd.txt", json_encode($cmsCandidateInduction));
// filtra os padrões com sentido
$cmsCandidatePattern = array_filter($cmsCandidateMining, function($key) { return substr_count($key, '-') == 0; }, ARRAY_FILTER_USE_KEY);
$patternLen = substr_count(key($cmsCandidatePattern), ',');
//quantidade de tweets com o padrão
$qtd = count($cmsCandidatePattern);
file_put_contents("strongly-disambiguated-msc$patternLen-$qtd.txt", json_encode($cmsCandidatePattern));
$execTime = microtime(true) - $start;
echo "strongly-disambiguated-msc$patternLen-$qtd.txt, tempo de execusão ". number_format($execTime, 6) ." s<br >";
$start = microtime(true);
}
?>