-
Notifications
You must be signed in to change notification settings - Fork 3
/
csv_count_duplicates.py
49 lines (35 loc) · 1.48 KB
/
csv_count_duplicates.py
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
"""
python csv_count_duplicates.py
Approximates the number of duplicates in a provided csv file to humanfirst upload
assumes humanfirst simple utterance upload format for model of
no header line
utterance,intent_name
"""
# *********************************************************************************************************************
# standard imports
# 3rd party imports
import pandas
import click
# custom imports
@click.command()
@click.option('-f', '--filename', type=str, required=True, help='Input File Path')
def main(filename: str) -> None:
"""Main Function"""
# read the csv into a dataframe
assert filename.endswith(".csv")
df = pandas.read_csv(filename, encoding="utf8", names=["utterance","intent_name"], dtype=str,delimiter=",")
# create a lowercase version
df["lowercase"] = df["utterance"].str.lower()
# replace punctuation with space
punct = r'[_\-\!$£%^&\"\'*@#\[\]\{\}\(\)\+\=\\\/¬`<>,.\?]'
df["no_punctuation"] = df["lowercase"].str.replace(punct,' ',regex=True)
# remove multiple white space
df["single_white_space"] = df["no_punctuation"].replace(r'[ ]+',' ',regex=True)
# stri[] whitespace
df["stripped"] = df["single_white_space"].str.strip()
# group by
df = df[["intent_name","stripped","utterance"]].groupby(["intent_name","stripped"]).count()
print(df)
print(df.sort_values("utterance",ascending=False).head(50))
if __name__ == '__main__':
main() # pylint: disable=no-value-for-parameter