Login
Order Now
Support
Python Task on Trigrams

Python Task on Trigrams

  • 29th Jul, 2022
  • 15:39 PM
import json
import random
import sys

def build_trigrams(words):
    trigrams = {}
    splitWord=words.split()
   
    for i in range(len(splitWord)-2): 
        pair=""
        pair=splitWord[i]+" "+splitWord[i+1]
        follower=splitWord[i+2]
        
        if pair in trigrams:
            trigrams.setdefault(pair,[]).append(follower)
        else:
            temp={pair:[follower]}
            trigrams.update(temp)
    return trigrams
    

def build_text(trigrams):
    newText=""
    while(True):
       strt=random.choice(list(trigrams))
       
       if(strt[0].isupper()):
           break
    
    
    
    newText+=strt
    newText+=" "
    while(True):
        if len(newText)>1000:
            break
        if strt in trigrams:

            
            getValue=trigrams.get(strt)
            number=random.randint(0,len(getValue)-1)
            newText+=getValue[number]
            newText+=" "
            
            
            temp=newText.split()
            
            strt=""
            strt=temp[-2]+" "+temp[-1]
            
            
        else:
            break
    return newText
    
def read_in_data(fileName):
    flag=0;
    try:
       file=open(fileName,"r")
    except Exception as e:
        print(e)
        sys.exit(1)
    
    finalText=[]
    line=file.readline()
    while(line):
        if flag==1:    
           if(line.startswith("***")):
               break
           finalText.append(line)
        if(line.startswith("***")):
            flag=1
        
        line=file.readline()
    words=""
    
    
    for i in finalText:
        if(i.startswith("ADVENTURE") or i.startswith("I.") or i.startswith("   I.") or i.startswith("II.") or i.startswith("  II.") or i.startswith("III.") or i.startswith(" III.") or i.startswith("IV.") or i.startswith("  IV.") or i.startswith("V.") or i.startswith("   V.") or i.startswith("VI.") or i.startswith("  VI.") or i.startswith("VII.") or i.startswith(" VII.") or i.startswith("VIII.") or i.startswith("VIII.") or i.startswith("IX.") or i.startswith("  IX.") or i.startswith("X.") or i.startswith("   X.")or i.startswith("XI.") or i.startswith("  XI.")or i.startswith("XII.") or i.startswith(" XII.")):
            continue
        if(i=="\n"):
            continue
        words+=i
    
    words=words.replace("\n"," ")
    words=words.replace('"',' ')
    return words
    
    
if __name__ == "__main__":
    
    try:
        filename = sys.argv[1]
    except IndexError:
        print("You must pass in a filename")
        sys.exit(1)

    words=read_in_data(filename)
        
    word_pairs = build_trigrams(words)
    new_text = build_text(word_pairs)
    print(new_text)

Share this post

assignment helpassignment helperassignment expertsassignment writing services