def interpolationLaplaceCalc(): train, y = preprocessing.train_split( 'text_files/outputs/preprocessing.txt') del y trainCount = Counter(train) unigramCount = models.laplaceUnigram(train) bigramCounts = Counter([(word, train[i + 1]) for i, word in enumerate(train[:-1])]) trigramCounts = Counter([(word, train[i + 1], train[i + 2]) for i, word in enumerate(train[:-2])]) first, last = endpoint.tools.stringParserNoCount( request.args.get('firstword'), request.args.get('lastword')) if first == "" and last == "": probability = 0 else: probability = pc.calculateProbabilityLaplaceInterpolation( trainCount, unigramCount, bigramCounts, trigramCounts, first, last) return render_template('%s.html' % "probabilityCalcInt", value=str(probability), type="laplaceInt", flavour="laplace", back="probCalcInt")
def interpolationUnkGen(): train, y = preprocessing.train_split( 'text_files/outputs/preprocessing.txt') del y unkUnigram = models.unkUnigram(train) unkBigram = models.unkBigram(train) unkTrigram = models.unkTrigram(train) first, last, count = endpoint.tools.stringParser( request.args.get('firstword'), request.args.get('lastword'), request.args.get('count')) sentence = tg.generateTextInterpolation(unkUnigram, unkBigram, unkTrigram, first, lastWord=last, count=count) fullSentence = "" for word in sentence: fullSentence += " " + word return render_template('%s.html' % "textGenInt", value=fullSentence, type="unkInt", flavour="unk", back="testGenInt")
def laplaceTrigramGenerator(): train, y = preprocessing.train_split( 'text_files/outputs/preprocessing.txt') del y bigramCounts = Counter([(word, train[i + 1]) for i, word in enumerate(train[:-1])]) trigramCounts = Counter([(word, train[i + 1], train[i + 2]) for i, word in enumerate(train[:-2])]) first, last, count = endpoint.tools.stringParser( request.args.get('firstword'), request.args.get('lastword'), request.args.get('count')) sentence = tg.generateTextFromLaplaceTrigram(train, bigramCounts, trigramCounts, first, lastWord=last, count=count) fullSentence = "" for word in sentence: fullSentence += " " + word return render_template('%s.html' % "textGen", value=fullSentence, type="laplaceTrigram", flavour="laplace", back="testGen")
def vanillaTrigramGenerator(): train, y = preprocessing.train_split( 'text_files/outputs/preprocessing.txt') del y vanillaBigram = models.vanillaBigram(train) vanillaTrigram = models.vanillaTrigram(train) first, last, count = endpoint.tools.stringParser( request.args.get('firstword'), request.args.get('lastword'), request.args.get('count')) sentence = tg.generateTextFromTrigram(vanillaBigram, vanillaTrigram, first, lastWord=last, count=count) fullSentence = "" for word in sentence: fullSentence += " " + word return render_template('%s.html' % "textGen", value=fullSentence, type="vanillaTrigram", flavour="vanilla", back="testGen")
def unkBigramCalc(): train, y = preprocessing.train_split( 'text_files/outputs/preprocessing.txt') del y unkBigram = models.unkBigram(train) print(request.args.get('count')) first, last = endpoint.tools.stringParserNoCount( request.args.get('firstword'), request.args.get('lastword')) if first == "" and last == "": probability = 0 else: probability = pc.calculateProbabilityFromBigram(unkBigram, first, last) return render_template('%s.html' % "probabilityCalc", value=str(probability), type="unkBigram", flavour="unk", back="probCalc")
def interpolationVanillaCalc(): train, y = preprocessing.train_split( 'text_files/outputs/preprocessing.txt') del y vanillaUnigram = models.vanillaUnigram(train) vanillaBigram = models.vanillaBigram(train) vanillaTrigram = models.vanillaTrigram(train) first, last = endpoint.tools.stringParserNoCount( request.args.get('firstword'), request.args.get('lastword')) if first == "" and last == "": probability = 0 else: probability = pc.calculateProbabilityInterpolation( vanillaUnigram, vanillaBigram, vanillaTrigram, first, last) return render_template('%s.html' % "probabilityCalcInt", value=str(probability), type="vanillaInt", flavour="vanilla", back="probCalcInt")
def interpolationlaplaceGen(): train, y = preprocessing.train_split( 'text_files/outputs/preprocessing.txt') del y vanillaBigram = models.vanillaBigram(train) trainCount = Counter(train) unigramCount = models.laplaceUnigram(train) bigramCounts = Counter([(word, train[i + 1]) for i, word in enumerate(train[:-1])]) trigramCounts = Counter([(word, train[i + 1], train[i + 2]) for i, word in enumerate(train[:-2])]) first, last, count = endpoint.tools.stringParser( request.args.get('firstword'), request.args.get('lastword'), request.args.get('count')) sentence = tg.generateTextLaplaceInterpolation(trainCount, unigramCount, bigramCounts, vanillaBigram, trigramCounts, first, lastWord=last, count=count) fullSentence = "" for word in sentence: fullSentence += " " + word return render_template('%s.html' % "textGenInt", value=fullSentence, type="laplaceInt", flavour="laplace", back="testGenInt")
def main(): print("Started API") x, y = preprocessing.train_split('text_files/outputs/preprocessing.txt')