jaskeeratk/talk-type
0
1import sys
2sys.modules["torch.__path__._path"] = None
3import whisper
4from pydub import AudioSegment
5import language_tool_python
6import difflib
7from difflib import SequenceMatcher
8import re
9from sklearn.feature_extraction.text import TfidfVectorizer
10from sklearn.metrics.pairwise import cosine_similarity
11import requests
12from sentence_transformers import SentenceTransformer,util
13import textstat
14
15# genai.configure(api_key="AIzaSyDbLVqMRmvaib00wGUReqT0ZPan-8IBoG0")
16
17def transcribed_audio(audio_path,model_size='tiny'):
18 model=whisper.load_model(model_size)
19 result=model.transcribe(audio_path)
20 return result['text']
21
22def calculate_fluency(audio_path,transcribed_text):
23 audio=AudioSegment.from_file(audio_path)
24 duration=len(audio)/1000
25 wrd_cnt=len(transcribed_text.split())
26 return round((wrd_cnt/duration)*60,2)
27
28def check_grammar(text):
29 # model=genai.GenerativeModel("gemini-1.5-flash")
30 # prompt=f"Please review the following text for grammar, punctuation, and clarity. Return suggestions only if there are errors:\n\n{text}"
31 # response=model.generate_content(prompt)
32 # return response.text
33 url = "https://api.languagetool.org/v2/check"
34 params = {
35 "text": text,
36 "language": "en-US"
37 }
38 response = requests.post(url, data=params)
39 return response.json()
40
41def correct_text(text,matches):
42 corrected=text
43 offset_shift=0
44 for match in matches:
45 replacement=match["replacements"][0]["value"] if match['replacements'] else ''
46 start=match['offset']+offset_shift
47 end=start+match['length']
48 corrected=corrected[:start]+replacement+corrected[end:]
49 offset_shift+=len(replacement)-match['length']
50 return corrected
51
52def check_email_format(text):
53 issues=[]
54 if "Subject:" not in text:
55 issues.append("Subject is missing")
56 if not any(word.lower().startswith(greetings) for word in text.splitlines() for greetings in ['dear','respected']):
57 issues.append("Missing greeting like 'Dear Sir/Madam'")
58 if not any(word.lower() in text.lower() for word in ["thank you","regards","sincerely"]):
59 issues.append("Missing a proper closing")
60 lines = text.strip().split('\n')
61 last_line = lines[-1].strip()
62
63 if not (last_line and last_line[0].isupper() and last_line.replace(" ", "").isalpha()):
64 issues.append("Missing or incomplete name at the end of the email.")
65 return issues
66model=SentenceTransformer('all-MiniLM-L6-v2')
67
68def check_relevance(user_Answer,reference):
69 user_embedding=model.encode(user_Answer,convert_to_tensor=True)
70 ref_embedding=model.encode(reference,convert_to_tensor=True)
71 similarity=util.pytorch_cos_sim(user_embedding,ref_embedding)
72 return float(similarity[0][0])
73
74def preprocess(text):
75 text=re.sub(r'[^\w\s]','',text)
76 return text.lower().split()
77
78def compare_text(reference,spoken):
79 ref_words=preprocess(reference)
80 spoken_words=preprocess(spoken)
81 matcher=SequenceMatcher(None,ref_words,spoken_words)
82 missing=[]
83 extra=[]
84 for tag,i1,i2,j1,j2 in matcher.get_opcodes():
85 if tag=='delete':
86 missing.extend(ref_words[i1:i2])
87 elif tag=='insert':
88 extra.extend(spoken_words[j1:j2])
89 elif tag=='replace':
90 missing.extend(ref_words[i1:i2])
91 extra.extend(spoken_words[j1:j2])
92 return {
93 "missing":missing,
94 "extra":extra,
95 "accuracy":round((len(ref_words)-len(missing))/len(ref_words)*100,2)
96 }
97
98def compare_description(user_text,actual_text):
99 vectorizer=TfidfVectorizer().fit_transform([user_text,actual_text])
100 vectors=vectorizer.toarray()
101 similarity=cosine_similarity([vectors[0]],[vectors[1]])[0][0]
102 return round(similarity*100,2)
103
104def check_article_format(text):
105 issues=[]
106 lines=text.split("\n")
107 lines=[line.strip() for line in lines if line.strip()]
108
109 if len(lines)<3:
110 issues.append("Too short to be a proper article")
111 if(len(lines[0].split()))>10:
112 issues.append("Title is too long or missing")
113 paragraph=text.strip().split('\n\n')
114 if len(paragraph)<3:
115 issues.append("Article must contain atleast 3 paragraphs(introduction,body and conclusion)")
116 conclusion_found=any(
117 phrase in text.lower()
118 for phrase in ["in conclusion","to conclude","to sum up","at the end","overall","in short","in a nutshell","in summary","finally","as a result","all in all","ultimately","taking everything in account","after all is said and done"]
119 )
120 if not conclusion_found:
121 issues.append("Conslusion seems to be missing")
122 return issues
123
124def analyze_vocab(text):
125 return {
126 "Difficult words":textstat.difficult_words(text),
127 "Lexicon Count":textstat.lexicon_count(text)
128 }
129def vocab_feedback(vocab):
130 diff_words=vocab['Difficult words']
131 lexicon_count=vocab['Lexicon Count']
132 feedback=[]
133 if diff_words <= 5:
134 feedback.append("❌ Very limited use of advanced vocabulary. Try using more formal or specific words.")
135 elif diff_words <= 15:
136 feedback.append("⚠️ Basic vocabulary used. Consider adding more variety.")
137 elif diff_words <= 25:
138 feedback.append("✅ Moderately rich vocabulary — appropriate for formal writing.")
139 else:
140 feedback.append("✅ Excellent use of advanced vocabulary.")
141 if lexicon_count < 40:
142 feedback.append("❌ Too short — try to elaborate more on your points.")
143 elif lexicon_count <= 60:
144 feedback.append("⚠️ Slightly brief — consider expanding your response.")
145 elif lexicon_count <= 90:
146 feedback.append("✅ Good word count — well-developed message.")
147 else:
148 feedback.append("✅ Thorough and detailed. Shows strong effort.")
149
150 return feedback