forked from commonsense/conceptnet5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snakefile
845 lines (730 loc) · 25.2 KB
/
Snakefile
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
from snakemake.remote.HTTP import RemoteProvider as HTTPRemoteProvider
from conceptnet5.languages import COMMON_LANGUAGES, ATOMIC_SPACE_LANGUAGES
import os
HTTP = HTTPRemoteProvider()
# The directory containing the data files. By default, this is "data" under
# the current directory, but it can be overridden using the
# CONCEPTNET_BUILD_DATA environment variable. This will happen during testing.
DATA = os.environ.get("CONCEPTNET_BUILD_DATA", "data")
# Some build steps are difficult to run, so we've already run them and put
# the results in S3. Of course, that can't be the complete solution, because
# we have to have run those build steps first. So when USE_PRECOMPUTED is
# True, we will download the computed files; when it's False, we will compute
# them.
USE_PRECOMPUTED = not os.environ.get("CONCEPTNET_REBUILD_PRECOMPUTED")
# If USE_PRECOMPUTED is False, should we upload the files we compute so they
# can be used as precomputed files later? (Requires ConceptNet S3 credentials.)
UPLOAD = False
# If USE_MORPHOLOGY is true, we will build and learn from sub-words derived
# from Morfessor.
USE_MORPHOLOGY = False
# The versions of Wiktionary data to download. Updating these requires
# uploading new Wiktionary dumps to ConceptNet's S3.
WIKTIONARY_VERSIONS = {
# English wiktionary formatting has gotten even harder to deal with, let's
# stay with 2019 for now
'en': '20190101',
'fr': '20200301',
'de': '20200301'
}
WIKTIONARY_LANGUAGES = sorted(list(WIKTIONARY_VERSIONS))
# Version of Unicode CLDR data, which will be downloaded separately from the
# ConceptNet raw data
CLDR_VERSION = '36.1'
# If it's a .0 release of CLDR, the .0 is be omitted from the directory name.
# For example, if the current version is '36.0', the short version should be
# '36'.
CLDR_VERSION_SHORT = '36.1'
# Languages where morphemes should not be split anywhere except at spaces
ATOMIC_SPACE_LANGUAGES = {'vi'}
# Languages that the CLDR emoji data is available in. These match the original
# filenames, not ConceptNet language codes; they are turned into ConceptNet
# language codes by the reader.
#
# This list is the list of languages with emoji names in CLDR v34.
EMOJI_LANGUAGES = [
'af', 'am', 'ar', 'ar_SA', 'as', 'ast', 'az', 'be', 'bg', 'bn', 'br', 'bs', 'ca', 'ccp',
'chr', 'cs', 'cy', 'da', 'de', 'de_CH', 'el', 'en', 'en_001', 'en_AU', 'en_CA', 'en_GB',
'es', 'es_419', 'es_MX', 'es_US', 'et', 'eu', 'fa', 'fi', 'fil', 'fo', 'fr', 'fr_CA', 'ga',
'gd', 'gl', 'gu', 'he', 'hi', 'hr', 'hu', 'hy', 'ia', 'id', 'is', 'it', 'ja', 'ka', 'kab',
'kk', 'km', 'kn', 'ko', 'ku', 'ky', 'lo', 'lt', 'lv', 'mk', 'ml', 'mn', 'mr', 'ms', 'my',
'nb', 'ne', 'nl', 'nn', 'or', 'pa', 'pl', 'ps', 'pt', 'pt_PT', 'ro', 'ru', 'sd', 'si', 'sk',
'sl', 'sq', 'sr', 'sr_Cyrl', 'sr_Cyrl_BA', 'sr_Latn', 'sr_Latn_BA', 'sv', 'sw', 'ta', 'te',
'th', 'tk', 'to', 'tr', 'uk', 'ur', 'uz', 'vi', 'yue', 'yue_Hans', 'zh', 'zh_Hant',
'zh_Hant_HK', 'zu'
]
# Increment this number when we incompatibly change the parser
WIKT_PARSER_VERSION = "3"
RETROFIT_SHARDS = 6
PROPAGATE_SHARDS = 6
# Dataset filenames
# =================
# The goal of reader steps is to produce Msgpack files, and later CSV files,
# with these names.
#
# We distingish *core dataset names*, which collectively determine the set of
# terms that ConceptNet will attempt to represent, from the additional datasets
# that will mainly be used to find more information about those terms.
RAW_DATA_URL = "https://zenodo.org/record/3739540/files/conceptnet-raw-data-5.8.zip"
PRECOMPUTED_DATA_PATH = "/precomputed-data/2016"
PRECOMPUTED_DATA_URL = "https://conceptnet.s3.amazonaws.com" + PRECOMPUTED_DATA_PATH
PRECOMPUTED_S3_UPLOAD = "s3://conceptnet" + PRECOMPUTED_DATA_PATH
# We need an external vocabulary to refer to when miniaturizing our set of
# embeddings. In the normal case, this is the word2vec Google News vocabulary.
# In the test build, we don't have that, we only have a cut-down version of GloVe,
# so we'll switch it to that instead.
MINI_VOCAB_SOURCE = "/vectors/w2v-google-news.h5"
INPUT_EMBEDDINGS = [
'crawl-300d-2M', 'w2v-google-news', 'glove12-840B', 'fasttext-opensubtitles'
]
SOURCE_EMBEDDING_ROWS = 1500000
MULTILINGUAL_SOURCE_EMBEDDING_ROWS = 2000000
# If CONCEPTNET_BUILD_TEST is set, we're running the small test build.
TESTMODE = bool(os.environ.get("CONCEPTNET_BUILD_TEST"))
if TESTMODE:
# Use a throwaway database to store the ConceptNet data when testing
os.environ['CONCEPTNET_DB_NAME'] = 'conceptnet-test'
# Retrofit a tiny version of GloVe when testing
INPUT_EMBEDDINGS = ['glove12-840B']
SOURCE_EMBEDDING_ROWS = 5000
DATA = "testdata/current"
USE_PRECOMPUTED = True
HASH_WIDTH = 12
RAW_DATA_URL = "/missing/data"
PRECOMPUTED_DATA_URL = "/missing/data"
EMOJI_LANGUAGES = ['en', 'en_001']
MINI_VOCAB_SOURCE = "/vectors/glove12-840B.h5"
CORE_DATASET_NAMES = [
"jmdict/jmdict",
"nadya/nadya",
"ptt_petgame/api",
"opencyc/opencyc",
"verbosity/verbosity",
"wordnet/wordnet",
"cedict/cedict",
"kyoto_yahoo/facts"
]
CORE_DATASET_NAMES += ["conceptnet4/conceptnet4_flat_{}".format(num) for num in range(10)]
CORE_DATASET_NAMES += ["ptt_petgame/part{}".format(num) for num in range(1, 13)]
CORE_DATASET_NAMES += ["wiktionary/{}".format(lang) for lang in WIKTIONARY_LANGUAGES]
CORE_DATASET_NAMES += ["emoji/{}".format(lang) for lang in EMOJI_LANGUAGES]
DATASET_NAMES = CORE_DATASET_NAMES + ["dbpedia/dbpedia_en"]
if USE_MORPHOLOGY:
DATASET_NAMES += ["morphology/subwords-{}".format(lang) for lang in COMMON_LANGUAGES]
rule all:
input:
DATA + "/assertions/assertions.csv",
DATA + "/psql/edges.csv",
DATA + "/psql/edge_features.csv",
DATA + "/psql/edges_gin.shuf.csv",
DATA + "/psql/nodes.csv",
DATA + "/psql/sources.csv",
DATA + "/psql/relations.csv",
DATA + "/psql/done",
DATA + "/stats/languages.txt",
DATA + "/stats/language_edges.txt",
DATA + "/stats/relations.txt",
DATA + "/assoc/reduced.csv",
DATA + "/vectors/mini.h5"
rule evaluation:
input:
DATA + "/stats/eval-graph.png"
rule webdata:
input:
DATA + "/psql/edges.csv",
DATA + "/psql/edge_features.csv",
DATA + "/psql/edges_gin.shuf.csv",
DATA + "/psql/nodes.csv",
DATA + "/psql/sources.csv",
DATA + "/psql/relations.csv",
DATA + "/psql/done",
rule clean:
shell:
"for subdir in assertions assoc collated db edges morph psql tmp vectors stats; "
"do echo Removing {DATA}/$subdir; "
"rm -rf {DATA}/$subdir; done"
rule test:
input:
DATA + "/assertions/assertions.csv",
DATA + "/psql/done",
DATA + "/assoc/reduced.csv",
DATA + "/vectors/mini.h5",
DATA + "/vectors/plain/numberbatch-en.txt.gz",
# Downloaders
# ===========
rule download_raw_package:
output:
DATA + "/raw/conceptnet-raw-data-5.8.zip"
shell:
"wget -nv {RAW_DATA_URL} -O {output}"
# Get emoji data directly from Unicode CLDR
rule download_unicode_data:
output:
DATA + "/raw/cldr-common-" + CLDR_VERSION + ".zip"
shell:
"wget -nv https://conceptnet.s3.amazonaws.com/downloads/2020/cldr-common-{CLDR_VERSION}.zip -O {output}"
rule extract_raw:
input:
DATA + "/raw/conceptnet-raw-data-5.8.zip"
output:
DATA + "/raw/{dirname}/{filename}"
shell:
"unzip {input} raw/{wildcards.dirname}/{wildcards.filename} -d {DATA}"
# This rule takes precedence over extract_raw, extracting the emoji data from
# the Unicode CLDR zip file.
#
# TODO: integrate this with the rest of the raw data
rule extract_emoji_data:
input:
DATA + "/raw/cldr-common-" + CLDR_VERSION + ".zip"
output:
DATA + "/raw/emoji/{filename}"
shell:
# The -j option strips the path from the file we're extracting, so
# we can use -d to put it in exactly the path we need.
"unzip -j {input} common/annotations/{wildcards.filename} -d {DATA}/raw/emoji"
# Precomputation
# ==============
# This section is for tricky build steps where we would rather just distribute
# the result of the computation.
def find_wiktionary_input(wildcards):
if USE_PRECOMPUTED:
return []
else:
language = wildcards.language
version = WIKTIONARY_VERSIONS[wildcards.language]
filename = DATA + "/raw/wiktionary/{0}wiktionary-{1}-pages-articles.xml.bz2".format(
language, version
)
return [filename]
rule precompute_wiktionary:
input:
find_wiktionary_input
output:
DATA + "/precomputed/wiktionary/parsed-{version}/{language}.jsons.gz"
run:
if USE_PRECOMPUTED:
shell("wget {PRECOMPUTED_DATA_URL}/wiktionary/"
"parsed-{wildcards.version}/{wildcards.language}.jsons.gz "
"-O {output}")
else:
# This is a mess because, for most of these sub-steps, the file
# being output isn't {output} but its uncompressed version
shell("bunzip2 -c {input} "
"| wiktionary-parser {wildcards.language} "
"| gzip -c > {output}")
if UPLOAD:
shell("aws s3 cp {output} "
"{PRECOMPUTED_S3_UPLOAD}/wiktionary/"
"parsed-{wildcards.version}/{wildcards.language}.jsons.gz "
"--acl public-read")
# Readers
# =======
# These are steps that turn raw data into files of uncombined 'edges'.
rule read_conceptnet4:
input:
DATA + "/raw/conceptnet4/conceptnet4_flat_{num}.jsons",
DATA + "/db/wiktionary.db"
output:
DATA + "/edges/conceptnet4/conceptnet4_flat_{num}.msgpack"
run:
single_input = input[0]
shell("cn5-read conceptnet4 {single_input} {output}")
rule read_dbpedia:
input:
DATA + "/raw/dbpedia/instance_types_en.tql.bz2",
DATA + "/raw/dbpedia/interlanguage_links_en.tql.bz2",
DATA + "/raw/dbpedia/mappingbased_objects_en.tql.bz2",
DATA + "/stats/core_concepts.txt"
output:
DATA + "/edges/dbpedia/dbpedia_en.msgpack",
shell:
"cn5-read dbpedia {DATA}/raw/dbpedia "
"{output} "
"{DATA}/stats/core_concepts.txt "
rule read_jmdict:
input:
DATA + "/raw/jmdict/JMdict.xml"
output:
DATA + "/edges/jmdict/jmdict.msgpack"
shell:
"cn5-read jmdict {input} {output}"
rule read_nadya:
input:
DATA + "/raw/nadya/nadya-2017.csv",
DATA + "/db/wiktionary.db"
output:
DATA + "/edges/nadya/nadya.msgpack"
run:
single_input = input[0]
shell("cn5-read nadya {single_input} {output}")
rule read_ptt_petgame:
input:
DATA + "/raw/ptt_petgame/conceptnet_zh_{part}.txt"
output:
DATA + "/edges/ptt_petgame/{part}.msgpack"
shell:
"cn5-read ptt_petgame {input} {output}"
rule read_opencyc:
input:
DATA + "/raw/opencyc/opencyc-2012-05-10-readable.nq"
output:
DATA + "/edges/opencyc/opencyc.msgpack"
shell:
"cn5-read opencyc {input} {output}"
rule read_verbosity:
input:
DATA + "/raw/verbosity/verbosity.txt"
output:
DATA + "/edges/verbosity/verbosity.msgpack"
shell:
"cn5-read verbosity {input} {output}"
rule prescan_wiktionary:
input:
expand(
DATA + "/precomputed/wiktionary/parsed-{version}/{language}.jsons.gz",
version=[WIKT_PARSER_VERSION],
language=WIKTIONARY_LANGUAGES
)
output:
DATA + "/db/wiktionary.db"
shell:
"mkdir -p {DATA}/tmp && "
"cn5-read wiktionary_pre {input} {DATA}/tmp/wiktionary.db && "
"mv {DATA}/tmp/wiktionary.db {output}"
rule read_wiktionary:
input:
DATA + "/precomputed/wiktionary/parsed-%s/{language}.jsons.gz" % WIKT_PARSER_VERSION,
DATA + "/db/wiktionary.db"
output:
DATA + "/edges/wiktionary/{language}.msgpack",
shell:
"cn5-read wiktionary {input} {output}"
rule read_wordnet:
input:
DATA + "/raw/wordnet-rdf/wn31.nt"
output:
DATA + "/edges/wordnet/wordnet.msgpack",
shell:
"cn5-read wordnet {input} {output}"
rule read_emoji:
input:
DATA + "/raw/emoji/{language}.xml"
output:
DATA + "/edges/emoji/{language}.msgpack"
shell:
"cn5-read emoji {input} {output}"
rule read_cc_cedict:
input:
DATA + "/raw/cedict/cedict_1_0_ts_utf-8_mdbg.txt.gz"
output:
DATA + "/edges/cedict/cedict.msgpack",
shell:
"cn5-read cc_cedict {input} {output}"
rule read_kyoto_yahoo:
input:
DATA + "/raw/kyoto_yahoo/facts.tsv"
output:
DATA + "/edges/kyoto_yahoo/facts.msgpack"
shell:
"cn5-read kyoto_yahoo {input} {output}"
# Converting msgpack to csv
# =========================
rule edge_msgpack_to_csv:
input:
DATA + "/edges/{dir}/{filename}.msgpack"
output:
DATA + "/edges/{dir,[^/]+}/{filename}.csv"
shell:
"cn5-convert msgpack_to_tab_separated {input} {output}"
rule assertion_msgpack_to_csv:
input:
DATA + "/assertions/{filename}.msgpack"
output:
DATA + "/assertions/{filename}.csv"
shell:
"cn5-convert msgpack_to_tab_separated {input} {output}"
rule sort_edges:
input:
expand(DATA + "/edges/{dataset}.csv", dataset=DATASET_NAMES)
output:
DATA + "/collated/sorted/edges.csv"
shell:
"mkdir -p {DATA}/tmp && cat {input} | LC_ALL=C sort -T {DATA}/tmp | LC_ALL=C uniq > {output}"
rule shuffle_edges:
input:
DATA + "/collated/sorted/edges.csv"
output:
DATA + "/collated/sorted/edges-shuf.csv"
shell:
"shuf {input} > {output}"
rule combine_assertions:
input:
DATA + "/collated/sorted/edges.csv",
DATA + "/stats/core_concepts.txt"
output:
DATA + "/assertions/assertions.msgpack"
shell:
"cn5-build combine {input} {output}"
# Putting data in PostgreSQL
# ==========================
rule prepare_db:
input:
DATA + "/assertions/assertions.msgpack"
output:
DATA + "/psql/edges.csv",
DATA + "/psql/edge_features.csv",
temp(DATA + "/psql/edges_gin.csv"),
DATA + "/psql/nodes.csv",
DATA + "/psql/sources.csv",
DATA + "/psql/relations.csv"
shell:
"cn5-db prepare_data {input} {DATA}/psql"
rule shuffle_gin:
input:
DATA + "/psql/edges_gin.csv"
output:
DATA + "/psql/edges_gin.shuf.csv"
shell:
"shuf {input} > {output}"
rule load_db:
input:
DATA + "/psql/edges.csv",
DATA + "/psql/edge_features.csv",
DATA + "/psql/edges_gin.shuf.csv",
DATA + "/psql/nodes.csv",
DATA + "/psql/sources.csv",
DATA + "/psql/relations.csv"
output:
DATA + "/psql/done"
shell:
"cn5-db load_data {DATA}/psql && touch {output}"
rule load_simplified_edges_view:
output:
DATA + "/psql/done_view"
shell:
"cn5-db load_simplified_edges_view && touch {output}"
# Collecting statistics
# =====================
rule relation_stats:
input:
DATA + "/assertions/assertions.csv"
output:
DATA + "/stats/relations.txt"
shell:
"cut -f 2 {input} | LC_ALL=C sort | LC_ALL=C uniq -c "
"| LC_ALL=C sort -nbr > {output}"
rule all_terms:
input:
DATA + "/psql/nodes.csv"
output:
DATA + "/stats/terms.txt"
shell:
"cut -f 2 {input} > {output}"
rule core_concepts_left:
input:
expand(DATA + "/edges/{dataset}.csv", dataset=CORE_DATASET_NAMES)
output:
DATA + "/stats/core_concepts_left.txt"
shell:
"cut -f 3 {input} > {output}"
rule core_concepts_right:
input:
expand(DATA + "/edges/{dataset}.csv", dataset=CORE_DATASET_NAMES)
output:
DATA + "/stats/core_concepts_right.txt"
shell:
"cut -f 3 {input} > {output}"
rule core_concepts:
input:
DATA + "/stats/core_concepts_left.txt",
DATA + "/stats/core_concepts_right.txt"
output:
DATA + "/stats/core_concepts.txt"
shell:
"LC_ALL=C sort -u {input} > {output}"
rule concepts_left:
input:
DATA + "/assertions/assertions.csv"
output:
DATA + "/stats/concepts_left.txt"
shell:
"cut -f 3 {input} > {output}"
rule concepts_right:
input:
DATA + "/assertions/assertions.csv"
output:
DATA + "/stats/concepts_right.txt"
shell:
"cut -f 4 {input} > {output}"
rule concept_counts:
input:
DATA + "/stats/concepts_left.txt",
DATA + "/stats/concepts_right.txt"
output:
DATA + "/stats/concept_counts.txt"
shell:
"cat {input} | grep '^/c/' | cut -d '/' -f 1,2,3,4 "
"| LC_ALL=C sort | LC_ALL=C uniq -c > {output}"
rule core_concept_counts:
input:
DATA + "/stats/core_concepts_left.txt",
DATA + "/stats/core_concepts_right.txt"
output:
DATA + "/stats/core_concept_counts.txt"
shell:
"cat {input} | grep '^/c/' | cut -d '/' -f 1,2,3,4 "
"| LC_ALL=C sort | LC_ALL=C uniq -c > {output}"
rule language_stats:
input:
DATA + "/stats/concepts_left.txt",
DATA + "/stats/concepts_right.txt"
output:
DATA + "/stats/languages.txt"
shell:
"cat {input} | grep '^/c/' | LC_ALL=C sort | LC_ALL=C uniq | cut -d '/' -f 3 "
"| LC_ALL=C sort | LC_ALL=C uniq -c | sort -nbr > {output}"
rule language_edge_stats:
input:
DATA + "/stats/concepts_left.txt",
DATA + "/stats/concepts_right.txt"
output:
DATA + "/stats/language_edges.txt"
shell:
"cat {input} | grep '^/c/' | LC_ALL=C sort | cut -d '/' -f 3 "
"| LC_ALL=C sort | LC_ALL=C uniq -c | sort -nbr > {output}"
# Building associations
# =====================
rule assertions_to_assoc:
input:
DATA + "/assertions/assertions.msgpack"
output:
DATA + "/assoc/assoc-with-dups.csv"
shell:
"cn5-convert msgpack_to_assoc {input} {output}"
rule assoc_uniq:
input:
DATA + "/assoc/assoc-with-dups.csv"
output:
DATA + "/assoc/assoc.csv"
shell:
"LC_ALL=C sort {input} | LC_ALL=C uniq > {output}"
rule reduce_assoc:
input:
DATA + "/assoc/assoc.csv",
expand(DATA + "/vectors/{name}.h5", name=INPUT_EMBEDDINGS)
output:
DATA + "/assoc/reduced.csv"
shell:
"cn5-build reduce_assoc {input} {output}"
# Building the vector space
# =========================
rule convert_word2vec:
input:
DATA + "/raw/vectors/GoogleNews-vectors-negative300.bin.gz",
DATA + "/db/wiktionary.db"
output:
DATA + "/vectors/w2v-google-news.h5"
resources:
ram=24
run:
single_input = input[0]
shell("CONCEPTNET_DATA=data cn5-vectors convert_word2vec -n {SOURCE_EMBEDDING_ROWS} {single_input} {output}")
rule convert_glove:
input:
DATA + "/raw/vectors/glove12.840B.300d.txt.gz",
DATA + "/db/wiktionary.db"
output:
DATA + "/vectors/glove12-840B.h5"
resources:
ram=24
run:
single_input = input[0]
shell("CONCEPTNET_DATA=data cn5-vectors convert_glove -n {SOURCE_EMBEDDING_ROWS} {single_input} {output}")
rule convert_fasttext_crawl:
input:
DATA + "/raw/vectors/crawl-300d-2M.vec.gz",
DATA + "/db/wiktionary.db"
output:
DATA + "/vectors/crawl-300d-2M.h5"
resources:
ram=24
run:
single_input = input[0]
shell("CONCEPTNET_DATA=data cn5-vectors convert_fasttext -n {SOURCE_EMBEDDING_ROWS} {single_input} {output}")
rule convert_fasttext:
input:
DATA + "/raw/vectors/fasttext-wiki-{lang}.vec.gz",
DATA + "/db/wiktionary.db"
output:
DATA + "/vectors/fasttext-wiki-{lang}.h5"
resources:
ram=24
run:
single_input = input[0]
shell("CONCEPTNET_DATA=data cn5-vectors convert_fasttext -n {SOURCE_EMBEDDING_ROWS} -l {wildcards.lang} {single_input} {output}")
rule convert_lexvec:
input:
DATA + "/raw/vectors/lexvec.commoncrawl.300d.W+C.pos.vectors.gz",
DATA + "/db/wiktionary.db"
output:
DATA + "/vectors/lexvec-commoncrawl.h5"
resources:
ram=24
run:
single_input = input[0]
shell("CONCEPTNET_DATA=data cn5-vectors convert_fasttext -n {SOURCE_EMBEDDING_ROWS} {single_input} {output}")
rule convert_opensubtitles_ft:
input:
DATA + "/raw/vectors/ft-opensubtitles.vec.gz",
DATA + "/db/wiktionary.db"
output:
DATA + "/vectors/fasttext-opensubtitles.h5"
resources:
ram=24
run:
single_input = input[0]
shell("CONCEPTNET_DATA=data cn5-vectors convert_fasttext -n {MULTILINGUAL_SOURCE_EMBEDDING_ROWS} {single_input} {output}")
rule convert_polyglot:
input:
DATA + "/raw/vectors/polyglot-{language}.pkl",
DATA + "/db/wiktionary.db"
output:
DATA + "/vectors/polyglot-{language}.h5"
run:
single_input = input[0]
shell("CONCEPTNET_DATA=data cn5-vectors convert_polyglot -l {wildcards.language} {single_input} {output}")
rule retrofit:
input:
DATA + "/vectors/{name}.h5",
DATA + "/assoc/reduced.csv"
output:
temp(expand(DATA + "/vectors/{{name}}-retrofit.h5.shard{n}", n=range(RETROFIT_SHARDS)))
resources:
ram=24
shell:
"cn5-vectors retrofit -n {RETROFIT_SHARDS} {input} {DATA}/vectors/{wildcards.name}-retrofit.h5"
rule join_retrofit:
input:
expand(DATA + "/vectors/{{name}}-retrofit.h5.shard{n}", n=range(RETROFIT_SHARDS))
output:
DATA + "/vectors/{name}-retrofit.h5"
resources:
ram=24
shell:
"cn5-vectors join_shard_files -n {RETROFIT_SHARDS} {output}"
rule merge_intersect:
input:
expand(DATA + "/vectors/{name}-retrofit.h5", name=INPUT_EMBEDDINGS)
output:
DATA + "/vectors/numberbatch-retrofitted.h5",
DATA + "/vectors/intersection-projection.h5"
resources:
ram=24
shell:
"cn5-vectors intersect {input} {output}"
rule propagate:
input:
DATA + "/assoc/assoc.csv",
DATA + "/vectors/numberbatch-retrofitted.h5"
output:
temp(expand(DATA + "/vectors/numberbatch-biased.h5.shard{n}", n=range(PROPAGATE_SHARDS)))
resources:
ram=24
shell:
"cn5-vectors propagate -n {PROPAGATE_SHARDS} {input} {DATA}/vectors/numberbatch-biased.h5"
rule join_propagate:
input:
expand(DATA + "/vectors/numberbatch-biased.h5.shard{n}", n=range(PROPAGATE_SHARDS))
output:
DATA + "/vectors/numberbatch-biased.h5"
resources:
ram=24
shell:
"cn5-vectors join_shard_files -n {PROPAGATE_SHARDS} --sort {output}"
rule debias:
input:
DATA + "/vectors/numberbatch-biased.h5"
output:
DATA + "/vectors/numberbatch.h5"
resources:
ram=30
shell:
"cn5-vectors debias {input} {output}"
rule miniaturize:
input:
DATA + "/vectors/numberbatch-biased.h5",
DATA + MINI_VOCAB_SOURCE
output:
DATA + "/vectors/mini.h5"
resources:
ram=20
shell:
"cn5-vectors miniaturize {input} {output}"
rule export_text:
input:
DATA + "/vectors/numberbatch.h5",
output:
DATA + "/vectors/plain/numberbatch.txt.gz"
shell:
"cn5-vectors export_text {input} {output}"
rule export_english_text:
input:
DATA + "/vectors/numberbatch.h5",
output:
DATA + "/vectors/plain/numberbatch-en.txt.gz"
shell:
"cn5-vectors export_text -l en {input} {output}"
# Morphology
# ==========
rule prepare_vocab:
input:
DATA + "/stats/core_concept_counts.txt"
output:
DATA + "/morph/vocab/{language}.txt"
shell:
"cn5-build prepare_morphology {wildcards.language} {input} {output}"
rule morfessor_segmentation:
input:
DATA + "/morph/vocab/{language}.txt"
output:
DATA + "/morph/segments/{language}.txt"
run:
if wildcards.language in ATOMIC_SPACE_LANGUAGES:
shell("morfessor-train {input} -S {output} --traindata-list --nosplit-re '[^_].'")
else:
shell("morfessor-train {input} -S {output} -f '_' --traindata-list")
rule subwords:
input:
DATA + "/morph/segments/{language}.txt",
output:
DATA + "/edges/morphology/subwords-{language}.msgpack"
shell:
"cn5-build subwords {wildcards.language} {input} {output}"
# Evaluation
# ==========
rule compare_embeddings:
input:
DATA + "/raw/vectors/GoogleNews-vectors-negative300.bin.gz",
DATA + "/raw/vectors/glove12.840B.300d.txt.gz",
DATA + "/vectors/glove12-840B.h5",
DATA + "/raw/vectors/fasttext-wiki-en.vec.gz",
DATA + "/vectors/numberbatch-biased.h5",
DATA + "/vectors/numberbatch.h5",
DATA + "/raw/analogy/SAT-package-V3.txt",
DATA + "/psql/done"
output:
DATA + "/stats/evaluation.h5"
run:
input_embeddings = input[:-2]
input_embeddings_str = ' '.join(input_embeddings)
shell("cn5-vectors compare_embeddings {input_embeddings_str} {output}")
rule comparison_graph:
input:
DATA + "/stats/evaluation.h5"
output:
DATA + "/stats/eval-graph.png"
shell:
"cn5-vectors comparison_graph {input} {output}"
ruleorder:
join_retrofit > convert_polyglot > extract_emoji_data > extract_raw