forked from lachlanpage/Markov-Chain-Sentence-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_utilities.py
87 lines (60 loc) · 2.94 KB
/
graph_utilities.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os
import shutil
def display_polarity_graph(sentiment_polarity):
"""
Display the sentiment polarity as a horizontal bar in the terminal.
The function scales the sentiment polarity to fit within the width of the terminal,
with some padding on either side. It then constructs a string representing a horizontal
bar, with filled '█' characters representing the scaled polarity, and empty '░' characters
filling in the rest of the bar. It then prints this bar to the terminal, along with the original
sentiment polarity value.
Parameters:
sentiment_polarity (float): The sentiment polarity to display, expected to be between -1 and 1.
Returns:
None
"""
# Get terminal width and subtract 14 for padding on each side
# bar_width = (get_terminal_width() - 14)
bar_width = 30
# Scale value to fit the bar width
scaled_value = int((sentiment_polarity + 1) / 2 * bar_width)
# Build bar string using Unicode characters
bar = '|' + '█' * scaled_value + '░' * (bar_width - scaled_value) + '|'
# print(f"Sentiment Polarity: {sentiment_polarity:.4f}")
print(f"{-1 :<0} {bar} {1 :>0}")
def display_subjectivity_graph(sentiment_subjectivity):
"""
Display the sentiment subjectivity as a horizontal bar in the terminal.
The function scales the sentiment subjectivity to fit within the width of the terminal,
with some padding on either side. It then constructs a string representing a horizontal
bar, with filled '█' characters representing the scaled subjectivity, and empty '░' characters
filling in the rest of the bar. It then prints this bar to the terminal, along with the original
sentiment subjectivity value.
Parameters:
sentiment_subjectivity (float): The sentiment subjectivity to display, expected to be between 0 and 1.
Returns:
None
"""
bar_width = 30
# Scale value to fit the bar width
scaled_value = int(sentiment_subjectivity * bar_width)
# Build bar string using Unicode characters
bar = '|' + '█' * scaled_value + '░' * (bar_width - scaled_value) + '|'
# print(f"Sentiment Subjectivity: {sentiment_subjectivity:.4f}")
print(f"{0 :<0} {bar} {1 :>0}")
def get_terminal_width():
"""
Get the width of the terminal in characters.
The function first attempts to retrieve the terminal size using the `os` module.
If this fails (for instance, if the code is not being run in a terminal), it falls
back to using the `shutil` module, with a fallback size of 80x24.
Returns:
int: The width of the terminal in characters. If the width can't be determined, it returns 80.
"""
try:
# Try to get the size using the os module
columns, _ = os.get_terminal_size(0)
except OSError:
# If that fails, use the shutil module and also provide a fallback
columns = shutil.get_terminal_size(fallback=(80, 24)).columns
return columns