Find the set of character 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.
nlp06.py
#!usr/bin/env python
#coding:UTF-8
def char_ngram(n,seq):
li = []
for i in range(len(seq)):
li.append(seq[i:i+n])
return li
seq1 = "paraparaparadise"
seq2 = "paragraph"
X = char_ngram(2,seq1)
Y = char_ngram(2,seq2)
Z = ['se']
logical_sum = set(X).union(Y)
logical_product = set(X).intersection(Y)
logical_difference = set(X).symmetric_difference(Y)
print "Union:",
print logical_sum
print "Intersection:",
print logical_product
print "Difference set:",
print logical_difference
print "bi called se-Is gram included in X?",
print('se' in X)
print "bi called se-Is gram included in Y?",
print('se' in Y)
Execution result Union: set (['e','ad','ag','di','h','is','ap','pa','ra','ph','ar', 'se','gr']) Intersection: set (['ap','pa','ar','ra']) Complement: set (['e','gr','ag','di','h','is','ph','se','ad']) Is bi-gram called se included in X? True Is the bi-gram called se included 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.
nlp07.py
def combine(x,y,z):
s1 = "of time"
s2 = "Is"
seq = str(x)+s1+str(y)+s2+str(z)
return seq
x = 12
y = "temperature"
z = 22.4
print combine(x,y,z)
Execution result The temperature at 12:00 is 22.4
Implement the function cipher that converts each character of the given character string according to the following specifications. Replace with (219 --character code) characters in lowercase letters Output other characters as they are Use this function to encrypt / decrypt English messages.
nlp08.py
#!usr/bin/env python
#coding:UTF-8
def cipher(str):
enc = ""
for char in str:
if 97 <= ord(char)<= 123:
enc +=chr(219-ord(char))
else:
enc +=char
return enc
str = "Machine Learning"
print cipher(str)#encryption
print cipher(cipher(str))#Composite
Execution result Mzxsrmv Lvzimrmt Machine Learning
09. Typoglycemia 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.
nlp09.py
#!usr/bin/env python
#coding:UTF-8
#Typoglycemia
import random
def Typo(word):
if len(word)>4:
wordli = list(word[1:-1])
random.shuffle(wordli)
neword = word[0] + ''.join(wordli)+ word[-1]
return neword
else:
return word
str="I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind."
typostr=""
for word in str.replace("."," .").split():
typostr +=" "+Typo(word)
print(typostr)
Execution result I clundo't bivelee that I colud allctuay uedtnasrnd what I was riednag : the phnoneeaml peowr of the hmaun mind .
There should be a better way to write the typostr + =" "+ Typo (word)
part,
Recommended Posts