Datasets:

Modalities:
Text
Formats:
json
Languages:
English
Libraries:
Datasets
pandas
License:
File size: 3,416 Bytes
f6dd396
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
from pathlib import Path
import re
import sys
from urllib.request import urlretrieve

import fasttext
import tqdm


LANG_THRESHOLD = 0.1
FASTTEXT_MODEL_URL = (
    "https://dl.fbaipublicfiles.com/fasttext/supervised-models/lid.176.bin"
)
JSON_SCHEMA_KEYWORDS = {
    "$anchor",
    "$comment",
    "$defs",
    "$dynamicAnchor",
    "$dynamicRef",
    "$id",
    "$recursiveAnchor",
    "$recursiveRef",
    "$ref",
    "$schema",
    "$vocabulary",
    "additionalItems",
    "additionalProperties",
    "allOf",
    "anyOf",
    "const",
    "contains",
    "contentEncoding",
    "contentMediaType",
    "contentSchema",
    "definitions",
    "dependencies",
    "dependentRequired",
    "dependentSchemas",
    "description",
    "disallow",
    "divisibleBy",
    "else",
    "enum",
    "exclusiveMaximum",
    "exclusiveMinimum",
    "extends",
    "format",
    "id",
    "if",
    "items",
    "maxContains",
    "maximum",
    "maxItems",
    "maxLength",
    "maxProperties",
    "minContains",
    "minimum",
    "minItems",
    "minLength",
    "minProperties",
    "multipleOf",
    "not",
    "oneOf",
    "pattern",
    "patternProperties",
    "prefixItems",
    "properties",
    "propertyNames",
    "required",
    "then",
    "title",
    "type",
    "unevaluatedItems",
    "unevaluatedProperties",
    "uniqueItems",
}

IGNORE_KEYWORDS = {
    "$id",
    "$schema",
    "$vocabulary",
    "format",
    "pattern",
    "type",
}


# Adapted from https://stackoverflow.com/a/37697078/123695
def identifier_split(id_str):
    return id_str
    return " ".join(
        re.sub("([A-Z][a-z]+)", r"_\1", re.sub("([A-Z]+)", r"_\1", id_str)).split("_")
    )


def collect_text(schema):
    """Generate a string of text from a schema, ignoring keywords"""
    text = ""

    if isinstance(schema, dict):
        for k, v in schema.items():
            # Ignore some keywords completely
            if k in IGNORE_KEYWORDS:
                continue

            # If the key is not a keyword, include it
            if k not in JSON_SCHEMA_KEYWORDS:
                text += " " + identifier_split(k)
            text += collect_text(v)

    elif isinstance(schema, list):
        text += " ".join(collect_text(v) for v in schema)

    elif isinstance(schema, str):
        # Include any found string values
        text += " " + schema

    return text.replace("\n", " ")


def get_languages(text):
    return {l.split("_")[-1]: p for (l, p) in zip(*model.predict(text, k=5))}


if __name__ == "__main__":
    # Download the language model if needed
    if not os.path.isfile("lid.176.bin"):
        urlretrieve(FASTTEXT_MODEL_URL, "lid.176.bin")
    model = fasttext.load_model("lid.176.bin")

    files = list(Path("valid_data").rglob("*.json"))
    for f in tqdm.tqdm(files):
        if not f.is_file():
            continue

        schema = json.load(f.open(encoding="utf-8"))
        schema_str = collect_text(schema)
        langs = get_languages(schema_str)
        top_lang, prob = max(langs.items(), key=lambda x: x[1])
        if prob < LANG_THRESHOLD:
            top_lang = None
        obj = {
            "repository": "/".join(f.parts[1:3]),
            "commit": f.parts[3],
            "path": str(Path(*f.parts[4:])),
            "language": top_lang,
            "languages": langs,
        }
        json.dump(obj, sys.stdout)
        sys.stdout.write("\n")