top of page

R code To convert Input English To Piglatin | R Programming Assignment Help



Pig Latin is an encrypted word in English, which is generated by doing the following alterations: The first vowel occurring in the input word is placed at the start of the new word along with the remaining alphabet of it. The alphabet is present before the first vowel is shifted, at the end of the new word it is followed by “ay”.

Examples:

Input: s = "paris"
Output: arispay

Input: s = "amazon"
Output: amazonay

Here is details of this R programming Project:

Your task is to create your own Pig Latin translator in R studio. You have to code steps 1, 2 and 3 from Learning the Rules instructions here. Step 4 is too complicated, so we skip it.


You have to submit a single file PigLatin.r of a R script. No need to submit a data file or your output file. I will run your code on my data and see your result.


Your R script should be able to:

1. Open text file. Use file football.txt as an example. Do not "hardcode" path to your file. Learn about "working directory" in R.

2. Read text.

3. Convert it into Pig Latin

4. As you produce a written Pig Latin, try to take care about punctuation and capital letters where it is appropriate and/or possible. You don't need hypinations as in the instruction examples as they are for speaking Pig Latin.

5. Save result as a text file football_PigLatin.txt.

6. Use Create R code to convert input English to piglatin as per rules given in link https://www.wikihow.com/Speak-Pig-Latin

we need to implement step 1 to step 3 (step 4 not required).

7. Loops shouldn't be used but should use R functions and there should be usage of Regular expressions.

8. Punctuations should be put back on converted text


To obtain the maximum available marks you should aim to:

1. Code all requested components.

2. Aim for optimised code in terms of computational overhead. E.g. it is not always possible to avoid loops, however you should aim to avoid loops where possible or use them as efficiently as possible (eg do more than one thing in a loop).

3. Use a clear coding style. Code clarity is an important part of your submission. Thus you should choose meaningful variable names and adopt the use of comments - you don't need to comment every single line, as this will affect readability - however you should aim to comment at least each section of code.

4. Have the code run successfully.

5. Document code limitations including, but not limited to, the requested functionalities.



Sample Starter Code Script

# Open .txt file for reading
football = open('football.txt','r')
# Read the text file and store it as 'football'
football = football.read()

# Change the text into lower case
football = football.lower()

#Spliting the text into list with individual words
football = football.split()

# Next we create the vowels and alphabets variables

# We create the vowels list
vowels=['a','e','i','o','u']

# We create the alphabet list
alphabets=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
consonant= ['b','c','d','f','g','h','i','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z']
# Next we create the punctuation list

punctuations=[',','.','"',"'",'?',"!","'"]

# Next we create a blank string variable
pigLatin=''

# Next we create a function to translate words starting with vowel
# We also take care of punctuation for words ending with either one or two non-alphabet as well as words starting with punctuations.
def vowel(word):
    if word[len(word)-2] in punctuations and word[len(word)-1] in punctuations:
             pigLatin= word[0:len(word)-2]+'yay'+word[len(word)-2:]+' '
    elif word[len(word)-1] in punctuations:
             pigLatin= word[0:len(word)-1]+'yay'+word[len(word)-1]+' '
    else:
        pigLatin= word+'yay'+' '
    return(pigLatin)
 
# Next we create a function to accept returned values from Vowel function
# Also, translate words starting with consonants while taking care of punctuations    
def translator(word):    
    if word[0] in vowels or word[0] in punctuations and word[1] in vowels:
        pigLatin=vowel(word)
    elif word[0] in consonant or word[0] in punctuations and word[1] in consonant:
        for j in range(len(word)):
# We use IF funtion here to to extract consonant appearing before a vowel or a 'y'
          if word[j]in vowels or word[j]== 'y':
# We first work with words that end with punctuations
# We check if the last character is a punctuation or not
              if word[len(word)-2] in punctuations and word[len(word)-1] in punctuations:
# We translate the words that starts with punctuation and ends with punctuation first
                  if word[0] in punctuations:
                      pigLatin= word[0]+word[j:len(word)-2]+word[1:j]+'ay'+word[len(word)-2:]+' '
# Here we translate the words that starts with a consonant and ends with punctuation.
                  else:
                   pigLatin= word[j:len(word)-2]+word[0:j]+'ay'+word[len(word)-2:]+' '
              elif word[len(word)-1] in punctuations:
# We translate the words that starts with punctuation and ends with punctuation first
                  if word[0] in punctuations:
                      pigLatin= word[0]+word[j:len(word)-1]+word[1:j]+'ay'+word[len(word)-1]+' '
# Here we translate the words that starts with a consonant and ends with punctuation.
                  else:
                      pigLatin= word[j:len(word)-1]+word[0:j]+'ay'+word[len(word)-1]+' '
# We now translate words that end with alphabets
              else:
# We translate the words that starts with consonant and ends with punctuation.
                  if word[0] in punctuations:
                      pigLatin= word[0]+word[j:len(word)]+word[1:j]+'ay'+' '
# We translate the words that starts with consonant and ends with alphabets after which we break.
                  else:
                      pigLatin= word[j:len(word)]+word[0:j]+'ay'+' '
              break
          else:
              j=j+1
    else:
        pigLatin =word+' '
    return(pigLatin)

# Here we pass the 'word' to the translator function to and the translated words are stored as 'transaltions' 

translation=''
for word in football:
    translation += translator(word)

# Next we change the cases the sentenses
# We split the string into sentenses by sep='.'
translation=translation.split('.')

# We Create a blank string variable (PigLatin1)
pigLatin1=''

# We now use a for-loop to iterate through the sentences and change the first letter to uppercase.
for i in range(len(translation)):
    for j in range(len(translation[i])):
     if translation[i][j] in alphabets:
         pigLatin1+=translation[i][0:j]+translation[i][j:len(translation[i])].capitalize()+'.'
         break
     j+=1
i+=1

football_PigLatin = open("football_PigLatin.txt","w+")
football_PigLatin = open("football_PigLatin.txt","w")
football_PigLatin.write(pigLatin1)
football_PigLatin.close()


Contact Us to get help in R Programming Projects, R Programming Assignments, R Programming Homework and get help with our R Programming Expert,


Send your requirement details at realcode4you@gmail.com and get instant help with an affordable prices.

Recent Posts

See All
bottom of page