-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathgrammar.py
More file actions
63 lines (52 loc) · 2.29 KB
/
grammar.py
File metadata and controls
63 lines (52 loc) · 2.29 KB
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
import logging
import os
import nltk
nltk.download('punkt_tab')
nltk.download('averaged_perceptron_tagger_eng')
nltk.download('wordnet')
nltk.download('wordnet_ic')
import addConventions
# #looking into the bundled nltk_data first (frozen app)
APP_DIR = os.path.dirname(os.path.abspath(__file__))
BUNDLED_NLTK = os.path.join(APP_DIR, "nltk_data")
if os.path.exists(BUNDLED_NLTK):
nltk.data.path.insert(0, BUNDLED_NLTK)
# #trying to load the resources, but DO NOT download at runtime on client machines
MISSING_NLTK = []
def _ensure_resource(res_name, path):
try:
nltk.data.find(path)
except LookupError:
# we don't download here — we just record that it's missing
# MISSING_NLTK.append(res_name)
nltk.download('punkt_tab')
nltk.download('averaged_perceptron_tagger_eng')
nltk.download('wordnet')
_ensure_resource("punkt", "tokenizers/punkt")
_ensure_resource("averaged_perceptron_tagger", "taggers/averaged_perceptron_tagger")
_ensure_resource("wordnet", "corpora/wordnet")
class GrammarChecker:
tokenizedSentences = []
checkAllSentences = False
def checkGrammar(self, transcriptionText: str, checkAllSentences: bool):
self.checkAllSentences = checkAllSentences
if "punkt" in MISSING_NLTK:
logging.warning("grammar.py: punkt missing, using fallback sentence split.")
self.tokenizedSentences = [s.strip() for s in transcriptionText.split(".") if s.strip()]
else:
self.tokenizedSentences = nltk.sent_tokenize(transcriptionText)
def getNextCorrection(self):
corrected = ""
if len(self.tokenizedSentences) == 0:
return (None, None)
while len(self.tokenizedSentences):
sentenceToCorrect = addConventions.correctSentence(self.tokenizedSentences[0])
if (self.tokenizedSentences[0] != sentenceToCorrect) or self.checkAllSentences:
del self.tokenizedSentences[0]
return (corrected, str(sentenceToCorrect))
else:
corrected += str(self.tokenizedSentences[0]) + "\n"
del self.tokenizedSentences[0]
return (corrected, None)
def getInflectionalMorphemes(self, converting: str):
return addConventions.addInflectionalMorphemes(converting)