File size: 2,622 Bytes
a99eadd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b00bd4
a99eadd
 
 
 
 
 
 
 
660d674
a99eadd
 
 
 
cffe6c7
a99eadd
 
7a75841
a99eadd
 
972b153
a99eadd
972b153
 
 
a99eadd
972b153
 
 
 
 
 
 
 
 
 
a99eadd
 
972b153
a99eadd
 
6b00bd4
a99eadd
 
 
 
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
import duckdb

# schema
# | column_name | string |
# |-------------|--------|
# | data_type   | string |
# | owner       | VARCHAR |
# | repo        | VARCHAR |
# | repo_dir    | VARCHAR |
# | file_rel_repo | VARCHAR |
# | file_rel_outdir | VARCHAR |
# | size | BIGINT |

if __name__ == "__main__":
    # Define filtering conditions
    # Exclude versioned documentation folders
    # Exclude specific examples from 'appwrite' repo except for version 1.7.x
    conditions = [
        "file_rel_repo NOT LIKE '/docs/versioned_docs%'",
        "NOT (repo = 'appwrite' AND file_rel_repo LIKE 'docs/examples%' AND file_rel_repo NOT LIKE 'docs/examples/1.7.x/%')",
        "file_rel_repo NOT LIKE '%/individual/%'",
    ]
    conditions_str = " AND ".join(conditions)

    # Build the query with CTEs for readability
    query = f"""
    -- Step 1: Filter the base data (filename column is precomputed)
    WITH filtered AS (
        SELECT *
        FROM 'output/cleaned_texts_on_metadata_only.parquet'
        WHERE {conditions_str}
    ),
    -- Step 2: Rank by mtime within each (owner, repo, filename) group to identify latest versions
    ranked_mtime AS (
        SELECT *, ROW_NUMBER() OVER (PARTITION BY owner, repo, file_rel_repo ORDER BY mtime DESC) as rn_mtime
        FROM filtered
    ),
    -- Step 3: Keep only the latest version for duplicates, but preserve all README.md and Contributing.md files
    filtered_dup AS (
        SELECT * FROM ranked_mtime
        WHERE rn_mtime = 1 OR file_rel_repo LIKE 'README.md' OR file_rel_repo ILIKE 'contributing.md'
    ),
    -- Step 4: Remove YAML front matter and drop short documents
    frontmatter_stripped AS (
        SELECT *, REGEXP_REPLACE(content, '(?s)^---\\s*\n.*?\n---\\s*\n*', '') AS content_stripped
        FROM filtered_dup
    ),
    length_filtered AS (
        SELECT *
        FROM frontmatter_stripped
        WHERE LENGTH(TRIM(content_stripped)) >= 150
    ),
    -- Step 5: Rank by cleaned content to remove duplicate content, keeping the first occurrence
    ranked AS (
        SELECT *, ROW_NUMBER() OVER (PARTITION BY content_stripped ORDER BY owner, repo, file_rel_repo) as rn
        FROM length_filtered
    )
    -- Final selection: unique content after filename deduplication
    SELECT owner, repo, repo_dir, file_rel_repo, file_rel_outdir, size, mtime, lang, content_stripped AS content
    FROM ranked
    WHERE rn = 1
    """  # FIXME: contributing filter currently does nothing.

    rel = duckdb.sql(query)
    rel.to_parquet("gooddocs.parquet")
    print(f"Filtered documents saved to gooddocs.parquet ({rel.shape[0]} items)")