Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

# *-* coding: utf-8 *-* 

""" 

Module provides a class for partial cognate detection, expanding the LexStat class. 

 

""" 

from __future__ import print_function, division, unicode_literals 

from collections import defaultdict 

from itertools import combinations 

 

import numpy as np 

import networkx as nx 

 

import lingpy 

from lingpy.algorithm import clustering, extra 

from lingpy.compare.lexstat import LexStat 

from lingpy.util import combinations2, pb 

try: 

from lingpy.algorithm.cython import calign 

except ImportError: 

from lingpy.algorithm.cython import _calign as calign 

 

# taking functions from lexstat source code here 

def _charstring(id_, char='X', cls='-'): 

return '{0}.{1}.{2}'.format(id_, char, cls) 

 

def _get_slices(tokens, **keywords): 

""" 

Function returns a slice tuple that indicates the boundaries for words with\ 

morpheme annotations. 

""" 

kw = dict( 

sep=lingpy.settings.rcParams['morpheme_separator'], 

word_sep=lingpy.settings.rcParams['word_separator'], 

word_seps=lingpy.settings.rcParams['word_separators'], 

seps=lingpy.settings.rcParams['morpheme_separators'], 

tones='T', 

split_on_tones=True, 

) 

kw.update(keywords) 

morphemes = lingpy.sequence.sound_classes.tokens2morphemes(tokens, 

**kw) 

out = [] 

current = 0 

for morpheme in morphemes: 

out += [(current, current+len(morpheme))] 

current = current+len(morpheme) 

return out 

 

class Partial(LexStat): 

""" 

Extended class for automatic detection of partial cognates. 

 

Parameters 

---------- 

filename : str 

The name of the file that shall be loaded. 

model : :py:class:`~lingpy.data.model.Model` 

The sound-class model that shall be used for the analysis. Defaults to 

the SCA sound-class model. 

merge_vowels : bool (default=True) 

Indicate whether consecutive vowels should be merged into single tokens or kept 

apart as separate tokens. 

transform : dict 

A dictionary that indicates how prosodic strings should be simplified 

(or generally transformed), using a simple key-value structure with the 

key referring to the original prosodic context and the value to the new 

value. Currently, prosodic strings (see 

:py:meth:`~lingpy.sequence.sound_classes.prosodic_string`) offer 11 

different prosodic contexts. Since not all these are helpful in 

preliminary analyses for cognate detection, it is useful to merge 

some of these contexts into one. The default settings distinguish only 

5 instead of 11 available contexts, namely: 

 

* ``C`` for all consonants in prosodically ascending position, 

* ``c`` for all consonants in prosodically descending position, 

* ``V`` for all vowels, 

* ``T`` for all tones, and 

* ``_`` for word-breaks. 

 

Make sure to check also the "vowel" keyword when initialising a LexStat 

object, since the symbols you use for vowels and tones should be 

identical with the ones you define in your transform dictionary. 

vowels : str (default="VT\_") 

For scoring function creation using the 

:py:class:`~lingpy.compare.lexstat.LexStat.get_scorer` function, you 

have the possibility to use reduced scores for the matching of tones 

and vowels by modifying the "vscale" parameter, which is set to 0.5 as 

a default. In order to make sure that vowels and tones are properly 

detected, make sure your prosodic string representation of vowels 

matches the one in this keyword. Thus, if you change the prosodic 

strings using the "transform" keyword, you also need to change the 

vowel string, to make sure that "vscale" works as wanted in the 

:py:class:`~lingpy.compare.lexstat.LexStat.get_scorer` function. 

check : bool (default=False) 

If set to **True**, the input file will first be checked for errors 

before the calculation is carried out. Errors will be written to the 

file **errors**, defaulting to ``errors.log``. See also ``apply_checks`` 

apply_checks : bool (default=False) 

If set to **True**, any errors identified by `check` will be handled 

silently. 

no_bscorer: bool (default=False) 

If set to **True**, this will suppress the creation of a 

language-specific scoring function (which may become quite large and is 

additional ballast if the method "lexstat" is not used after all. If 

you use the "lexstat" method, however, this needs to be set to 

**False**. 

errors : str 

The name of the error log. 

 

Attributes 

---------- 

pairs : dict 

A dictionary with tuples of language names as key and indices as value, \ 

pointing to unique combinations of words with the same meaning in all \ 

language pairs. 

model : :py:class:`~lingpy.data.model.Model` 

The sound class model instance which serves to convert the phonetic 

data into sound classes. 

chars : list 

A list of all unique language-specific character types in the 

instantiated LexStat object. The characters in this list consist of 

 

* the language identifier (numeric, referenced as "langid" as a 

default, but customizable via the keyword "langid") 

* the sound class symbol for the respective IPA transcription value 

* the prosodic class value 

 

All values are represented in the above order as one string, separated 

by a dot. Gaps are also included in this collection. They are 

traditionally represented as "X" for the sound class and "-" for the 

prosodic string. 

rchars : list 

A list containing all unique character types across languages. In 

contrast to the chars-attribute, the "rchars" (raw chars) do not 

contain the language identifier, thus they only consist of two values, 

separated by a dot, namely, the sound class symbol, and the prosodic 

class value. 

scorer : dict 

A collection of :py:class:`~lingpy.algorithm.cython.misc.ScoreDict` 

objects, which are used to score the strings. LexStat distinguishes two 

different scoring functions: 

 

* rscorer: A "raw" scorer that is not language-specific and consists 

only of sound class values and prosodic string values. This scorer is 

traditionally used to carry out the first alignment in order to 

calculate the language-specific scorer. It is directly accessible as an 

attribute of the LexStat class 

(:py:class:`~lingpy.compare.lexstat.lexstat.rscorer`). The characters 

which constitute the values in this scorer are accessible via the 

"rchars" attribue of each lexstat class. 

* bscorer: The language-specific scorer. This scorer is made of unique 

language-specific characters. These are accessible via the "chars" 

attribute of each LexStat class. As the "rscorer", the "bscorer" can 

also be accessed directly as an attribute of the LexStat class  

(:py:class:`~lingpy.compare.lexstat.lexstat.bscorer`). 

 

Notes 

----- 

This method automatically infers partial cognate sets from data which was 

previously morphologically segmented.  

 

""" 

 

def __init__(self, infile, **keywords): 

 

kw = { 

"morphemes" : "morphemes", 

"partial_cognates" : "partial_cognate_sets" 

} 

kw.update(keywords) 

lingpy.compare.lexstat.LexStat.__init__(self, infile, **kw) 

self._morphemes = kw['morphemes'] 

self._partials = kw['partial_cognates'] 

 

def _get_partial_matrices( 

self, 

concept=False, 

method='sca', 

scale=0.5, 

factor=0.3, 

restricted_chars='_T', 

mode='global', 

gop=-2, 

restriction='', 

**keywords 

): 

""" 

Function creates matrices for the purpose of partial cognate detection. 

""" 

 

# set the defaults 

kw = dict( 

defaults=False, 

external_scorer=False, # external scoring function 

imap_mode= False, 

sep=lingpy.settings.rcParams['morpheme_separator'], 

word_sep=lingpy.settings.rcParams['word_separator'], 

word_seps=lingpy.settings.rcParams['word_separators'], 

seps=lingpy.settings.rcParams['morpheme_separators'], 

tones='T', 

split_on_tones=True 

) 

kw.update(keywords) 

 

def function(idxA, idxB, sA, sB, **keywords): 

if method == 'lexstat': 

args = [ 

self[idxA, self._numbers][sA[0]:sA[1]], 

self[idxB, self._numbers][sB[0]:sB[1]], 

[self.cscorer[_charstring( 

self[idxB, self._langid] 

), n] 

for n in self[idxA, self._numbers][sA[0]:sA[1]]], 

[self.cscorer[_charstring( 

self[idxA, self._langid]), n] 

for n in self[idxB, self._numbers][sB[0]:sB[1]]], 

self[idxA, self._prostrings][sA[0]:sA[1]], 

self[idxB, self._prostrings][sB[0]:sB[1]], 

1, 

scale, 

factor, 

self.cscorer, 

mode, 

restricted_chars, 

1] 

elif method == 'sca': 

args = [ 

[n.split('.', 1)[1] for n in self[idxA, 

self._numbers][sA[0]:sA[1]]], 

[n.split('.', 1)[1] for n in self[idxB, 

self._numbers][sB[0]:sB[1]]], 

self[idxA, self._weights][sA[0]:sA[1]], 

self[idxB, self._weights][sB[0]:sB[1]], 

self[idxA, self._prostrings][sA[0]:sA[1]], 

self[idxB, self._prostrings][sB[0]:sB[1]], 

gop, 

scale, 

factor, 

self.rscorer, 

mode, 

restricted_chars, 

1] 

return calign.align_pair(*args)[2] 

 

concepts = [concept] if concept else sorted(self.rows) 

 

# we have two basic constraints in the algorithm: 

# a) set cognacy between morphemes in the same word to zero 

# b) set cognacy for those parts to zero which are superceded by 

# another part in all comparisons of two words 

# essentially, setting things to zero, means setting them to 1, since 

# we are dealing with distances here 

for c in concepts: 

 

indices = self.get_list(row=c, flat=True) 

matrix = [] 

tracer = [] 

 

# first assemble all partial parts 

trace = defaultdict(list) # stores where the stuff is in the matrix 

count = 0 

for idx in indices: 

 

# we need the slices for both words, so let's just take the 

# tokens for this time 

tokens = self[idx, self._segments] 

 

# now get the slices with the function 

slices = _get_slices(tokens, **kw) 

 

for i,slc in enumerate(slices): 

tracer += [(idx, i, slc)] 

trace[idx] += [(i, slc, count)] 

count += 1 

 

if kw['imap_mode']: 

# now, iterate for each string pair, asses the scores, and make 

# sure, we only assign the best of those to the matrix 

 

matrix = [[0 for i in tracer] for j in tracer] 

# reset the self-constraints (we missed it before) 

 

 

for idxA, idxB in combinations(indices, r=2): 

# iterate over all parts 

scores = [] 

idxs = [] 

for i,sliceA,posA in trace[idxA]: 

for j,sliceB,posB in trace[idxB]: 

d = function(idxA, idxB, sliceA, sliceB) 

scores += [d] 

idxs += [(posA,posB)] 

 

visited_seqs = set([]) 

while scores: 

min_score_index = scores.index(min(scores)) 

min_score = scores.pop(min_score_index) 

posA, posB = idxs.pop(min_score_index) 

if posA in visited_seqs or posB in visited_seqs: 

matrix[posA][posB] = 1 

matrix[posB][posA] = 1 

else: 

matrix[posA][posB] = min_score 

matrix[posB][posA] = min_score 

visited_seqs.add(posA) 

visited_seqs.add(posB) 

for idx in indices: 

for i,(_,sliceA,posA) in enumerate(trace[idx]): 

for j,(_,sliceB,posB) in enumerate(trace[idx]): 

 

if i < j: 

matrix[posA][posB] = 1 

matrix[posB][posA] = 1 

else: 

matrix = [] 

for (idxA, posA, sliceA), (idxB, posB, sliceB) in combinations(tracer, r=2): 

 

if idxA == idxB: 

d = 1 

else: 

try: 

d = function(idxA, idxB, sliceA, sliceB) 

except ZeroDivisionError: 

lingpy.log.warn( 

"Encountered Zero-Division for the comparison of " 

"{0} and {1}".format( 

''.join(self[idxA, self._tokens]), 

''.join(self[idxB, self._tokens]))) 

d = 100 

matrix += [d] 

matrix = lingpy.algorithm.misc.squareform(matrix) 

if not concept: 

yield c, tracer, matrix 

else: 

yield matrix 

 

def partial_cluster( 

self, 

method='sca', 

threshold=0.45, 

scale=0.5, 

factor=0.3, 

restricted_chars='_T', 

mode='overlap', 

cluster_method='infomap', 

gop=-1, 

restriction='', 

ref='', 

external_function=None, 

split_on_tones=True, 

**keywords): 

""" 

Cluster the words into partial cognate sets. 

 

Function for flat clustering of words into cognate sets. 

 

Parameters 

---------- 

method : {'sca','lexstat','edit-dist','turchin'} (default='sca') 

Select the method that shall be used for the calculation. 

cluster_method : {'upgma','single','complete', 'mcl'} (default='upgma') 

Select the cluster method. 'upgma' (:evobib:`Sokal1958`) refers to 

average linkage clustering, 'mcl' refers to the "Markov Clustering 

Algorithm" (:evobib:`Dongen2000`). 

threshold : float (default=0.3) 

Select the threshold for the cluster approach. If set to c{False}, 

an automatic threshold will be calculated by calculating the 

average distance of unrelated sequences (use with care). 

scale : float (default=0.5) 

Select the scale for the gap extension penalty. 

factor : float (default=0.3) 

Select the factor for extra scores for identical prosodic segments. 

restricted_chars : str (default="T\_") 

Select the restricted chars (boundary markers) in the prosodic 

strings in order to enable secondary alignment. 

mode : {'global','local','overlap','dialign'} (default='overlap') 

Select the mode for the alignment analysis. 

verbose : bool (default=False) 

Define whether verbose output should be used or not. 

gop : int (default=-2) 

If 'sca' is selected as a method, define the gap opening penalty. 

restriction : {'cv'} (default="") 

Specify the restriction for calculations using the edit-distance. 

Currently, only "cv" is supported. If *edit-dist* is selected as 

*method* and *restriction* is set to *cv*, consonant-vowel matches 

will be prohibited in the calculations and the edit distance will 

be normalized by the length of the alignment rather than the length 

of the longest sequence, as described in :evobib:`Heeringa2006`. 

inflation : {int, float} (default=2) 

Specify the inflation parameter for the use of the MCL algorithm. 

expansion : int (default=2) 

Specify the expansion parameter for the use of the MCL algorithm. 

 

""" 

kw = dict( 

imap_mode = True, 

post_processing = False, 

inflation=2, 

expansion=2, 

max_steps=1000, 

add_self_loops=True, 

sep=lingpy.settings.rcParams['morpheme_separator'], 

word_sep=lingpy.settings.rcParams['word_separator'], 

word_seps=lingpy.settings.rcParams['word_separators'], 

seps=lingpy.settings.rcParams['morpheme_separators'], 

mcl_logs=lambda x: -np.log2((1 - x) ** 2) 

) 

kw.update(keywords) 

 

# check for parameters and add clustering, in order to make sure that 

# analyses are not repeated 

if not hasattr(self, 'params'): 

self.params = {} 

self.params['partial_cluster'] = "{0}_{1}_{2:.2f}".format( 

method, cluster_method, threshold) 

self._stamp += '# Partial Cluster: ' + self.params['partial_cluster'] 

 

matrices = self._get_partial_matrices(method=method, scale=scale, 

factor=factor, restricted_chars=restricted_chars, mode=mode, 

gop=gop, imap_mode=kw['imap_mode'], 

split_on_tones=split_on_tones) 

k = 0 

C = defaultdict(list) # stores the pcogids 

G = {} # stores the graphs 

with pb(desc='PARTIAL SEQUENCE CLUSTERING', total=len(self.rows)) as progress: 

for concept, trace, matrix in matrices: 

progress.update(1) 

lingpy.log.info('Analyzing concept {0}...'.format(concept)) 

if external_function: 

c = external_function(threshold, matrix, 

taxa=list(range(len(matrix))), revert=True) 

elif cluster_method == 'infomap': 

c = extra.infomap_clustering(threshold, 

matrix, taxa=list(range(len(matrix))), 

revert=True) 

elif cluster_method == 'mcl': 

c = clustering.mcl(threshold, matrix, 

taxa = list(range(len(matrix))), 

max_steps=kw['max_steps'], 

inflation=kw['inflation'], 

expansion=kw['expansion'], 

add_self_loops=kw['add_self_loops'], 

logs=kw['mcl_logs'], 

revert=True) 

elif cluster_method in ['upgma', 'single', 'complete', 'ward']: 

c = clustering.flat_cluster(cluster_method, 

threshold, matrix, 

revert=True) 

else: 

raise ValueError("No suitable cluster method specified.") 

 

for i,(idx,pos,slc) in enumerate(trace): 

C[idx] += [c[i] + k] 

if kw['post_processing']: 

_g = nx.Graph() 

for i,(idx,pos,slc) in enumerate(trace): 

_g.add_node((i,idx,pos)) 

remove_edges = [] 

for (i, n1), (j, n2) in combinations2(enumerate(_g.nodes())): 

if C[n1[1]][n1[2]] == C[n2[1]][n2[2]]: 

_g.add_edge(n1, n2) 

if n1[1] == n2[1]: 

# get scores for n1 and n2 with all the rest in 

# the matrix to decide for one 

sn1, sn2 = 0, 0 

for i,row in enumerate(matrix): 

sn1 += matrix[i][n1[0]] 

sn2 += matrix[i][n2[0]] 

sn1 = sn1 / len(matrix) 

sn2 = sn2 / len(matrix) 

if sn1 <= sn2: 

remove_edges += [n2] 

else: 

remove_edges += [n1] 

for node in remove_edges: 

for edge in sorted(_g[node]): 

_g.remove_edge(node, edge) 

 

for i,coms in enumerate(nx.connected_components(_g)): 

cogid = i + 1 + k 

for j,idx,pos in coms: 

C[idx][pos] = cogid 

 

G[concept] = _g 

 

k += max(c.values()) 

self.add_entries(ref or self._partials, C, lambda x: x) 

self.graphs = G 

 

def add_cognate_ids(self, source, target, idtype='strict', override=False): 

""" 

Compute normal cognate identifiers from partial cognate sets. 

 

Parameters 

---------- 

source: str 

Name of the source column in your wordlist file. 

target : str 

Name of the target column in your wordlist file. 

idtype : str (default="strict") 

Select between "strict" and "loose". 

override: bool (default=False) 

Specify whether you want to override existing columns. 

 

Notes 

----- 

While the computation of strict cognate IDs from partial cognate IDs is 

straightforward and just judges those words as cognate which are 

identical in all their parts, the computation of loose cognate IDs 

constructs a network between all words, draws lines between all words 

that share a common morpheme, and judges all connected components in this 

network as cognate. 

""" 

if idtype == 'strict': 

 

tmp = defaultdict(list) 

for k in self._data: 

tmp[tuple(self[k, source])] += [k] 

idx = 1 

D = {} 

for vals in tmp.values(): 

for k in vals: 

D[k] = idx 

idx += 1 

self.add_entries(target, D, lambda x: x, override=override) 

elif idtype == 'loose': 

 

D = {} 

idx = 1 

for c in self.rows: 

idxs = self.get_list(row=c, flat=True) 

srcs = [self[k, source] for k in idxs] 

 

# get connected components 

g = nx.Graph() 

g.add_nodes_from(idxs) 

for (i, cogsA), (j, cogsB) in combinations2(zip(idxs, srcs)): 

if [x for x in cogsA if x in cogsB]: 

g.add_edge(i, j) 

for i,comps in enumerate(nx.connected_components(g)): 

for comp in comps: 

D[comp] = idx + i 

idx += (i+1) 

self.add_entries(target, D, lambda x: x, override=override) 

else: 

raise ValueError("The value you selected is not available.")