-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
115 lines (88 loc) · 3.76 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import csv
import random
def getContextFromCSV():
# Open csv
try:
# with open('Test.csv', 'r') as file:
with open('Dataset.csv', 'r') as file:
reader = csv.DictReader(file)
# If job_link column is missing in file return an error
if 'context' not in reader.fieldnames:
print(f"Error: 'context' column not found in the CSV file.")
return None
# Store all the about sections in a list
listOfContext = []
for row in reader:
context = row['context']
listOfContext.append(context)
return listOfContext
# If file not found return an error
except FileNotFoundError:
print("Error: File not found.")
return None
def select_random_words(text):
# Split the text into words
words = text.split()
# Check if the number of words in the text is less than 5
if len(words) < 5:
return "Text contains fewer than 5 words."
# Randomly select 5 words from the list of words
selected_words = random.sample(words, 10)
return selected_words
def getAnswerFromCSV():
# Open csv
try:
# with open('Test.csv', 'r') as file:
with open('Dataset.csv', 'r') as file:
reader = csv.DictReader(file)
# If job_link column is missing in file return an error
if 'answer' not in reader.fieldnames:
print(f"Error: 'answer' column not found in the CSV file.")
return None
# Store all the about sections in a list
listOfAnswers = []
for row in reader:
answer = row['answer']
listOfAnswers.append(answer)
return listOfAnswers
# If file not found return an error
except FileNotFoundError:
print("Error: File not found.")
return None
def select_random_words(text):
# Split the text into words
words = text.split()
# Check if the number of words in the text is less than 5
if len(words) < 10:
return "Text contains fewer than 10 words."
# Randomly select 5 words from the list of words
selected_words = random.sample(words, 10)
return selected_words
def getHitRate(selected_words, Answer):
Answerlist = list(Answer.split(","))
print("AnswerList: ", Answerlist)
print("----------------------------------------------------------------------------------------------------------")
Intersection = set(selected_words).intersection(Answerlist)
HitRate = round(len(Intersection)/len(Answerlist) * 100, 2)
return HitRate
def main():
# Create list of context from the csv file
listOfContext = getContextFromCSV()
listOfAnswers = getAnswerFromCSV()
print("Size of listOfContext:", len(listOfContext), "Size of listOfAnswers:", len(listOfAnswers))
rn = random.randint(1, 600)
randomContext = listOfContext[rn]
Answer = listOfAnswers[rn]
print("----------------------------------------------------------------------------------------------------------")
print(randomContext)
print("----------------------------------------------------------------------------------------------------------")
print("Answer: ", Answer)
print("----------------------------------------------------------------------------------------------------------")
selected_words = select_random_words(randomContext)
print("selected words: ", selected_words, "\n")
Output = 'C++, Software Quality Engineering, SQL, Java, HTML/CSS'
RandomOutput = 'Web, people, design, SQL, process'
HitRate = getHitRate(selected_words, Answer)
print("\nHitrate:", HitRate, "%")
if __name__ == "__main__":
main()