Skip to content

Commit ac523f8

Browse files
committed
Add analyzer to count occurrences of the given phrase
This currently prints a message to stdout for now, but we will probably switch to printing a dataframe in the future (with ica.output_results).
1 parent 47ab3b6 commit ac523f8

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

ica/analyzers/count_phrase.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python3
2+
3+
import re
4+
5+
import ica
6+
7+
8+
def main() -> None:
9+
10+
cli_args = ica.get_cli_args()
11+
phrase = cli_args.args[0]
12+
if not phrase:
13+
raise Exception("Phrase to count must be provided")
14+
dfs = ica.get_dataframes(
15+
contact_name=cli_args.contact_name, timezone=cli_args.timezone
16+
)
17+
filtered_messages = dfs.messages[~dfs.messages["is_reaction"]]
18+
count = (
19+
filtered_messages["text"]
20+
.str.findall(phrase, flags=re.IGNORECASE)
21+
.str.len()
22+
.sum()
23+
)
24+
print(f"The phrase '{phrase}' appears {count} times.")
25+
26+
27+
if __name__ == "__main__":
28+
main()

ica/cli.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ def get_cli_args() -> argparse.Namespace:
6464
help="the path of the file to export analyzer results to; required when"
6565
" exporting Excel (xlsx) files",
6666
)
67+
parser.add_argument(
68+
"args",
69+
nargs="*",
70+
help="any additional arguments you want to pass to the analyzer",
71+
)
6772

6873
return parser.parse_args()
6974

0 commit comments

Comments
 (0)