"Knock 100 Language Processing published in Inui / Okazaki Laboratory of Tohoku University .tohoku.ac.jp/nlp100/) ”is famous as a collection of problems for students and working adults studying language processing.
So I'm also a Tohoku University student, so although my major is different, I tried Knock 100 language processing.
Get a string in which the characters of the string "stressed" are arranged in reverse (from the end to the beginning).
words = "stressed"
print(words[::-1])
[result] desserts
Take out the 1st, 3rd, 5th, and 7th characters of the character string "Patatokukashi" and get the concatenated character string.
words = "Patatoku Kashii"
print(words[::2])
[result] Police car
Obtain the character string "Patatokukashi" by alternately connecting the characters "Police car" + "Taxi" from the beginning.
word1 = "Police car"
word2 = "taxi"
for i in range(4):
print(word1[i]+word2[i],end="")
[result] Patatoku Kashii
Break down the sentence "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
PI = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
PI = PI.replace('.','')
PI = PI.replace(',','')
PI = PI.split()
ans = [len(num) for num in PI]
ans
[result] [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]
Break down the sentence "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can." Into words 1, 5, 6, 7, 8, 9, 15, 16, The 19th word is the first character, and the other words are the first two characters, and the associative array (dictionary type or map type) from the extracted character string to the word position (what number of words from the beginning) Create.
element = "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."
dict = {}
list = [1,5,6,7,8,9,15,16,19]
for i,j in enumerate(element.split()):
if i+1 in list:
dict[i+1] = j[0]
else:
dict[i+1] = j[0:2]
print(dict)
[result] {1: 'H', 2: 'He', 3: 'Li', 4: 'Be', 5: 'B', 6: 'C', 7: 'N', 8: 'O', 9: 'F', 10: 'Ne', 11: 'Na', 12: 'Mi', 13: 'Al', 14: 'Si', 15: 'P', 16: 'S', 17: 'Cl', 18: 'Ar', 19: 'K', 20: 'Ca'}
Create a function that creates an n-gram from a given sequence (string, list, etc.). Use this function to get the word bi-gram and the letter bi-gram from the sentence "I am an NLPer".
def n_gram(target, n):
result = []
for i in range(len(target) - n + 1):
result.append(target[i:i + n])
return result
target = "I am an NLPer"
words_target = target.split()
print("[Word bi-gram]")
print(n_gram(words_target, 2))
print("[Character bi-gram]")
print(n_gram(target, 2))
[result] [Word bi-gram] [['I', 'am'], ['am', 'an'], ['an', 'NLPer']] [Character bi-gram] ['I ', ' a', 'am', 'm ', ' a', 'an', 'n ', ' N', 'NL', 'LP', 'Pe', 'er']
Find the set of characters bi-grams contained in "paraparaparadise" and "paragraph" as X and Y, respectively, and find the union, intersection, and complement of X and Y, respectively. In addition, find out if the bi-gram'se'is included in X and Y.
def n_gram(target, n):
result = []
for i in range(0,len(target) - n + 1):
result.append(target[i:i + n])
return result
text_x = "paraparaparadise"
text_y = "paragraph"
set_x = set(n_gram(text_x, 2))
set_y = set(n_gram(text_y, 2))
print("X:",end="")
print(set_x)
print("Y:",end="")
print(set_y)
print("----")
print("[Union]")
# print(set_x | set_y)But OK
print(set_x.union(set_y))
print("[Intersection]")
# print(set_x & set_y)But OK
print(set_x.intersection(set_y))
print("[Difference set]")
# print(set_x - set_y)But OK
print(set_x.difference(set_y))
print("----")
print("se in X: ",end="")
print('se' in set_x)
print("se in Y: ",end="")
print('se' in set_y)
[result] X:{'ar', 'ap', 'ad', 'di', 'ra', 'pa', 'se', 'is'} Y:{'ar', 'ap', 'ag', 'gr', 'ph', 'ra', 'pa'} ---- [Union] {'ar', 'ap', 'ag', 'ad', 'gr', 'ph', 'di', 'ra', 'pa', 'se', 'is'} [Intersection] {'pa', 'ar', 'ap', 'ra'} [Difference set] {'is', 'ad', 'di', 'se'} ---- se in X: True se in Y: False
Implement a function that takes arguments x, y, z and returns the string "y at x is z". Furthermore, set x = 12, y = "temperature", z = 22.4, and check the execution result.
def createSentence(x,y,z):
print("{}of time".format(x) + y + "Is{}".format(z))
createSentence(12,"temperature",22.4)
[result] The temperature at 12:00 is 22.4
Implement the function cipher that converts each character of the given character string with the following specifications.
- Replace with (219 --character code) characters in lowercase letters
- Other characters are output as they are Use this function to encrypt / decrypt English messages.
def cipher(s):
result =''
for i in s:
if i.islower():
result += chr(219-ord(i))
else:
result += i
return result
text = "Man is but a reed, the most feeble thing in the nature, but he is a thinking reed. "
cipher(text)
[result] 'Mzm rh yfg z ivvw, gsv nlhg uvvyov gsrmt rm gsv mzgfiv, yfg sv rh z gsrmprmt ivvw. '
Create a program that randomly rearranges the order of the other letters, leaving the first and last letters of each word for the word string separated by spaces. However, words with a length of 4 or less are not rearranged. Give an appropriate English sentence (for example, "I couldn't believe that I could actually understand what I was reading: the phenomenal power of the human mind.") And check the execution result.
import random
def typoglycemia(target):
res = []
for s in target.split():
if len(s) < 5:
res.append(s)
else:
head = s[0]
tail = s[-1]
inner = list(s[1:-1])
random.shuffle(inner)
res.append(head+"".join(inner)+tail)
return " ".join(res)
target = "I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind ."
print(typoglycemia(target))
[result] I cud'lont bivleee that I colud aaclutly unnsetadrd what I was rindaeg : the phnomeneal pwoer of the human mind .
Please point out any mistakes. I will fix it.
Recommended Posts