2018-11-16
¶Link Grammar 5.4.4, test_grammar
updated 2018-10-19.
This notebook is shared as static Agglomerative-Clustering-2018-11-16.html
import os, sys, time
module_path = os.path.abspath(os.path.join('..'))
if module_path not in sys.path: sys.path.append(module_path)
from src.grammar_learner.utl import UTC
from src.grammar_learner.read_files import check_dir
from src.grammar_learner.write_files import list2file
from src.grammar_learner.widgets import html_table
from src.grammar_learner.pqa_table import table_rows, wide_rows
tmpath = module_path + '/tmp/'
check_dir(tmpath, True, 'none')
table = []
start = time.time()
print(UTC(), ':: module_path =', module_path)
out_dir = module_path + '/output/Agglomerative-Clustering-' + str(UTC())[:10]
corpus = 'CDS-caps-br-text+brent9mos'
corpus = 'CDS-caps-br-text'
dataset = 'LG-English'
dataset = 'LG-English-clean-clean' # 2018-10-29: only 100% parsed
runs = (1,1)
kwargs = {
'left_wall' : '' ,
'period' : False ,
'context' : 2 ,
'word_space' : 'sparse' ,
'clustering' : ['agglomerative', 'ward'],
'cluster_range' : 200 ,
'clustering_metric' : ['silhouette', 'cosine'],
'grammar_rules' : 2 ,
'tmpath' : tmpath ,
'verbose' : 'min' ,
'template_path' : 'poc-turtle',
'linkage_limit' : 1000 }
lines = [[33, corpus, 'LG-English' , 0, 0, 'none'],
[34, corpus, 'LG-English' , 0, 0, 'rules']]
rp = module_path + '/data/CDS-caps-br-text+brent9mos/LG-English'
rp = module_path + '/data/CDS-caps-br-text/LG-English' # shorter test
rp = module_path + '/data/CDS-caps-br-text/LG-English-clean-clean'
cp = rp # corpus path = reference_path :: use 'gold' parses as test corpus
%%capture
line = [lines[0]]
a, _, header, log = wide_rows(lines, out_dir, cp, rp, runs, **kwargs)
display(html_table([header] + a))
display(html_table([header] + a))
%%capture
corpus = 'CDS-caps-br-text+brent9mos'
a2, _, header, log = wide_rows(lines, out_dir, cp, rp, runs, **kwargs)
corpus = 'CDS-caps-br-text'
display(html_table([header] + a2))
%%capture
t1 = []
n = 0
for linkage in ['ward', 'complete', 'average']:
n += 1
m = 0
for affinity in ['euclidean', 'manhattan', 'cosine']:
if linkage == 'ward' and affinity != 'euclidean': continue
m += 1
lines[0][0] = round(n + 0.1*m, 1)
m += 1
lines[1][0] = round(n + 0.1*m, 1)
kwargs['clustering'] = ['agglomerative', linkage, affinity]
a, _, header, log = wide_rows(lines, out_dir, cp, rp, runs, **kwargs)
t1.extend(a)
table.extend(a)
display(html_table([header] + t1))
%%capture
t3 = []
for linkage in ['ward', 'average', 'complete']:
n += 1
m = 0
for affinity in ['euclidean', 'manhattan', 'cosine']:
for kwargs['cluster_range'] in [300, 400, 500]:
if linkage == 'ward' and affinity != 'euclidean': continue
m += 1
line[0][0] = round((n + 0.1*m), 1)
kwargs['clustering'] = ['agglomerative', linkage, affinity]
a, _, header, log = wide_rows(line, out_dir, cp, rp, runs, **kwargs)
t3.extend(a)
table.extend(a)
display(html_table([header] + t3))
⇒ NEXT: complete/average
linkage, cosine
affinity, 400-500 clusters
%%capture
affinity = 'cosine'
t4 = []
for kwargs['cluster_range'] in [500,400,300]:
n += 1
m = 0
for linkage in ['complete', 'average']:
for knn in [None, 50, 20, 10]:
if linkage == 'ward' and affinity != 'euclidean': continue
m += 1
line[0][0] = round((n + m* 0.1), 1)
kwargs['clustering'] = ['agglomerative', linkage, affinity, knn]
a, _, header, log = wide_rows(line, out_dir, cp, rp, runs, **kwargs)
t4.extend(a)
table.extend(a)
display(html_table([header] + t4))
display(html_table([header] + table))
print(UTC(), ':: finished, elapsed', str(round((time.time()-start)/3600.0, 1)), 'hours')
table_str = list2file(table, out_dir + '/table.txt')
print('Results saved to', out_dir + '/table.txt')