NC_Crime / NC_Crime.py
zwn22's picture
Update NC_Crime.py
a13fd66 verified
raw
history blame
4.57 kB
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# TODO: Address all TODOs and remove all explanatory comments
"""IMPORTANT:
Given the disparate sizes and column naming conventions of each raw dataset,
it was NOT FEASIBLE to streamline the entire cleaning process within a single Python (.py) file.
# Therefore, a Jupyter notebook has been made available for those interested in delving into the intricacies of how the unified dataset was crafted.
"""
import csv
import json
import os
from typing import List
import datasets
import logging
import pandas as pd
# TODO: Add BibTeX citation
# Find for instance the citation on arxiv or on the dataset repo/website
_CITATION = """\
@InProceedings{huggingface:dataset,
title = {NC Crime Dataset},
author={huggingface, Inc.
},
year={2024}
}
"""
# TODO: Add description of the dataset here
# You can copy an official description
_DESCRIPTION = """\
The dataset, compiled from public police incident reports across various cities in North Carolina, covers a period from the early 2000s through to 2024. It is intended to facilitate the study of crime trends and patterns.
"""
# TODO: Add a link to an official homepage for the dataset here
_HOMEPAGE = ""
# TODO: Add the licence for the dataset here if you can find it
_LICENSE = ""
# TODO: Add link to the official dataset URLs here
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
_URL = ""
_URLS = ""
class NCCrimeDataset(datasets.GeneratorBasedBuilder):
"""Dataset for North Carolina Crime Incidents."""
_URLS = _URLS
VERSION = datasets.Version("1.0.0")
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"year": datasets.Value("int64"),
"city": datasets.Value("string"),
"crime_major_category": datasets.Value("string"),
"crime_detail": datasets.Value("string"),
"latitude": datasets.Value("float64"),
"longitude": datasets.Value("float64"),
"occurance_time": datasets.Value("string"),
"clear_status": datasets.Value("string"),
"incident_address": datasets.Value("string"),
"notes": datasets.Value("string"),
"crime_severity": datasets.Value("string"),
}),
citation=_CITATION,
)
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
# Use the raw GitHub link to download the CSV file
downloaded_file_path = dl_manager.download_and_extract(
"https://raw.githubusercontent.com/zening-wang2023/NC-Crime-Dataset/main/NC_v1.csv.zip"
)
unzipped_file_path = os.path.join(downloaded_file_path, "NC_v1.csv")
# Return a list of split generators
return [
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": unzipped_file_path})
]
def _generate_examples(self, filepath):
# Read the CSV file
df = pd.read_csv(filepath) ## just for test
# Iterate over the rows and yield examples
for i, row in df.iterrows():
yield i, {
"year": int(row["year"]),
"city": row["city"],
"crime_major_category": row["crime_major_category"],
"crime_detail": row["crime_detail"],
"latitude": float(row["latitude"]),
"longitude": float(row["longitude"]),
"occurance_time": row["occurance_time"],
"clear_status": row["clear_status"],
"incident_address": row["incident_address"],
"notes": row["notes"],
"crime_severity": row["crime_severity"],
}