forked from nogibjj/coursera-applied-data-eng-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·55 lines (41 loc) · 1.2 KB
/
main.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
50
51
52
53
54
55
#!/usr/bin/env python3
"""
Main cli or app entry point
"""
import yake
import click
# create a function that reads a file
def read_file(filename):
with open(filename, encoding="utf-8") as myfile:
return myfile.read()
# def extract keywords
def extract_keywords(text):
kw_extractor = yake.KeywordExtractor()
keywords = kw_extractor.extract_keywords(text)
return keywords
# create a function that makes hash tags
def make_hashtags(keywords):
hashtags = []
for keyword in keywords:
hashtags.append("#" + keyword[0].replace(" ", ""))
return hashtags
@click.group()
def cli():
"""A cli for keyword extraction"""
@cli.command("extract")
@click.argument("filename", default="text.txt")
def extract(filename):
"""Extract keywords from a file"""
text = read_file(filename)
keywords = extract_keywords(text)
click.echo(keywords)
@cli.command("hashtags")
@click.argument("filename", default="text.txt")
def hashtagscli(filename):
"""Extract keywords from a file and make hashtags"""
text = read_file(filename)
keywords = extract_keywords(text)
hashtags = make_hashtags(keywords)
click.echo(hashtags)
if __name__ == "__main__":
cli()