Python inexperienced person tries to knock 100 language processing 07-09

This is a continuation of this. Python inexperienced person tries to knock 100 language processing 00-04 https://qiita.com/earlgrey914/items/fe1d326880af83d37b22

Python inexperienced person tries to knock 100 language processing 05-06 https://qiita.com/earlgrey914/items/e772f1b7e5efea114e1d


07. Sentence generation by template

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.

e. what's this? Somehow it became so easy at once ...?

iinoka.py


x=12
y="temperature"
z=22.4

def moziKetsugo(x, y, z):
    print(str(x) + "of time" + y + "Is" + str(z))

moziKetsugo(x, y, z)
The temperature at 12:00 is 22.4

Is this right···? Isn't it too easy ...? Let's think from a slightly worn perspective. The problem is that if the argument is the string "x, y, z", then "y at x is z", If it is a variable x = 12, y = "temperature", z = 22.4, it will be returned by applying a character ** Isn't it a problem of creating a function? (If so, write it a little more clearly) If so ... In Java, the processing was changed depending on the presence or absence of arguments (I forgot what it was), but I wonder if I should do the same thing.

I googled. There seems to be a default argument.
Reference
https://note.nkmk.me/python-argument-default/

How about this.

enshu07.py


x=12
y="temperature"
z=22.4

def moziKetsugo(x="x", y="y", z="z"):
    print(str(x) + "of time" + y + "Is" + str(z))

#Just output
moziKetsugo()

#Output with arguments
moziKetsugo(x, y, z)
y at x is z
The temperature at 12:00 is 22.4

Well, I don't understand the intention of the question, so please let me know if anyone can understand it. I wonder if it's okay because it's so easy ...

08. Ciphertext

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 ・ Other characters are output as they are Use this function to encrypt / decrypt English messages. ** **

(I used the problem statement as the headline, but I'm sorry I didn't know how to break the headline, so it felt strange.)

cipher, high high ... chi, chi p ha (TOEIC340)

Well, write the composition for the time being. I'd like you to write as much plaintext as an example in the question sentence (Punpun)

punpun.py



genbun = "the magic words are squeamish ossifrage To know is to know that you know nothing That is the true meaning of knowledge"

def cipher(s):
    if s ==Lowercase letters:
        (219 -Character code)Replace with the character
return String after encryption
    else:
return Output as it is

#encryption
angobun = cipher(genbun))
print(angobun)

#Decryption
fukugobun = decryption(genbun)
print(fukugobun)

So, I wonder how to do character code conversion. What is the code converted to? Is UTF-8 okay? With UTF-8, the 219-character code part cannot be implemented ...?

When I was googled, it seems that python has a function ʻord () `** that returns an integer called a Unicode code point of ** characters. Maybe you can use this. It's unfriendly! !! !! (Punpun

Try out.

tameshi.py


print("a")
print(ord("a"))
a
97

Hmm, I see. Is ʻa`` 97`? Then what about the character string?

moziha.py


print("abc")
print(ord("abc"))
abc
Traceback (most recent call last):
  File "/home/ec2-user/knock/enshu08.py", line 2, in <module>
    print(ord("abc"))
TypeError: ord() expected a character, but string of length 3 found

I see. ** Pass one character at a time to ʻord () `**. OK.

Let's write this if statement first.

if.py


if s ==Lowercase letters:

It seems that there is a function for this if you google for "python lowercase judgment".

Determine lowercase (islower) Determined to be alphanumeric (isalnum) Judge whether it is an alphabetic character (isalpha)


Reference URL
https://hibiki-press.tech/learn_prog/python/isupper-islower/3728#islower

I was able to encrypt it

dekitayo.py


genbun = "aBc0"

def cipher(s):
    kaesubun = ""
    
    for i in s:
        #Judge whether it is alphabetic and lowercase
        if i.isalpha() and i.islower():
            kaesubun = kaesubun + chr(219-ord(i))
        else:
            kaesubun = kaesubun + i
    return kaesubun

angobun = cipher(genbun)
print(angobun)
zBx0

I forgot to write it above, but it seems that the conversion from ** characters to Unicode code points can be done with ʻord () and vice versa with chr () . ** ** And now I'm writing for i in s:`, but in this notation [Practice 03](https://qiita.com/earlgrey914/items/fe1d326880af83d37b22#%E3%83%91%E3% 83% 88% E3% 82% AB% E3% 83% BC% E3% 82% BF% E3% 82% AF% E3% 82% B7% E3% 83% BC% E3% 81% AE% E6% 96% 87% E5% AD% 97% E3% 82% 92% E5% 85% 88% E9% A0% AD% E3% 81% 8B% E3% 82% 89% E4% BA% A4% E4% BA% 92% E3% 81% AB% E9% 80% A3% E7% B5% 90% E3% 81% 97% E3% 81% A6% E6% 96% 87% E5% AD% 97% E5% 88% 97% E3% 83% 91% E3% 82% BF% E3% 83% 88% E3% 82% AF% E3% 82% AB% E3% 82% B7% E3% 83% BC% E3% 83% BC% E3% 82% What I noticed at 92% E5% BE% 97% E3% 82% 88) is huge. (Personal compliment point. Compliment everyone !!) ** If you weren't aware of this notation, you might have purposely divided the string character by character, put it in an array, took it out, and processed it. ** ~~ I wrote that in the Java era. ~~

Then decrypt. This is easy.

yoyu.py


genbun = "aBc0"

#Encryption function
def cipher(s):
    kaesubun = ""
    
    for i in s:
        #Judge whether it is alphabetic and lowercase
        if i.isalpha() and i.islower():
            kaesubun = kaesubun + chr(219-ord(i))
        else:
            kaesubun = kaesubun + i
    return kaesubun

#Decryption function
def decryption(s):
    kaesubun = ""
    
    for i in s:
        if i.isalpha() and i.islower():
            kaesubun = kaesubun + chr(219+ord(i))
        else:
            kaesubun = kaesubun + i
    return kaesubun


print("Original")
print(genbun)

angobun = cipher(genbun)
print("Cryptogram")
print(angobun)

fukugobun = decryption(angobun)
print("Decryption statement")
print(fukugobun)
Original
aBc0
Cryptogram
zBx0
Decryption statement
ŕBœ0

Ahhhhhhh! ?? !! ?? !! ??

If you try math on paper It was chr (219-ord (i)) instead of chr (219 + ord (i)). ** There is a pitfall where I think it's easy. Please use as a lesson () **

Completed with the help ↓.

enshu08.py


genbun = "aBcDeFghijKLM0123456789"

#Encryption function
def cipher(s):
    kaesubun = ""
    
    for i in s:
        #Judge whether it is alphabetic and lowercase
        if i.isalpha() and i.islower():
            kaesubun = kaesubun + chr(219-ord(i))
        else:
            kaesubun = kaesubun + i
    return kaesubun

#Decryption function
def decryption(s):
    kaesubun = ""
    
    for i in s:
        if i.isalpha() and i.islower():
            kaesubun = kaesubun + chr(219-ord(i))
        else:
            kaesubun = kaesubun + i
    return kaesubun


print("Original")
print(genbun)

angobun = cipher(genbun)
print("Cryptogram")
print(angobun)

fukugobun = decryption(angobun)
print("Decryption statement")
print(fukugobun)
Original
aBcDeFghijKLM0123456789
Cryptogram
zBxDvFtsrqKLM0123456789
Decryption statement
aBcDeFghijKLM0123456789

Next next! !!

  1. 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.

Japanese commuter pass is difficult.

First of all, the whole picture.

zenta.py


s = "I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind ."

#Make a list of the original text separated by a space
list = s.split()

kaesubun = []
for i in list:
The length of the string in the if list is greater than 4:
Randomly rearrange the order of the other characters, leaving the first and last characters
        kaesubun.append(Characters after replacement)
    else:
        #As it is without replacement
        kaesubun.append(i)

Yeah, it doesn't look that difficult. I wondered how to do "randomly rearrange the order of other characters".

Randomly select multiple elements (no duplication): random.sample ()

https://note.nkmk.me/python-random-choice-sample-choices/

I see. I'm going to use the standard library here for the first time to specify random, is that okay? Is that okay? Try random.

rand.py


import random

s = ["a", "b", "c", "d","e"]
print(random.sample(s, 5))
['c', 'e', 'b', 'a', 'd']

Yup. Good vibes. I was able to get 5 characters at random.

"Leave the beginning and end" is [Practice 02](https://qiita.com/earlgrey914/items/fe1d326880af83d37b22#02-%E3%83%91%E3%83%88%E3%82%AB % E3% 83% BC% E3% 82% BF% E3% 82% AF% E3% 82% B7% E3% 83% BC% E3% 83% 91% E3% 82% BF% E3% 83% 88% E3 Use slices as you did with% 82% AF% E3% 82% AB% E3% 82% B7% E3% 83% BC% E3% 83% BC). It can be said that "from the second character (1) to the character immediately before the end (-1)", so [1: -1]

onazi.py


s = ["a", "b", "c", "d", "e"]
print(s[1:-1])
['b', 'c', 'd']

Slice the "start" and "end" as well

nokosi.py


s = ["a", "b", "c", "d", "e"]
print(s[0])
print(s[-1])
['a']
['e']

Alright. Likely to go.

** After all, the notation of slices is a little troublesome. ** ** Even though s [-1] is the "last character" Is s [1: -1] "from the second character to the last character"? Isn't it a little difficult to understand?

↓ It might be better to write like this. (I don't know if you can write like this) "Last one character" s[-1: : ] "From the second character to the character immediately before the end" s[1:-1: ]

~ 20 minutes later ~ It's done.

enshu09.py


import random

s = "I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind ."

#Make a list of the original text separated by a space
kugitta_list = s.split()

kaesubun = []

#Processed word by word in the original text
for ichitango in kugitta_list:
    #If the word is larger than 4 letters
    if len(ichitango) > 4:
        kaesutango = []
        
        #List words by separating them character by character
        ichitango_kugitta_list= list(ichitango)
        
        #Make a list of characters excluding the first and last characters
        sentou_matsubi_nozoita_list = ichitango_kugitta_list[1:-1]
        
        #Put the first letter in the "words to return" list
        kaesutango.append(ichitango_kugitta_list[0])
        
        #Randomly get all characters from the list of characters excluding the first and last characters and put them in the "words to return" list
        kaesutango.extend(random.sample(sentou_matsubi_nozoita_list, len(sentou_matsubi_nozoita_list)))
        
        #Put the last character in the "words to return" list
        kaesutango.append(ichitango_kugitta_list[-1])
        
        #Put "words to return" in the list of "sentences to return"
        kaesubun.append(''.join(kaesutango))
    else:
        #Put it in the list of "returned sentences" as it is without replacing it
        kaesubun.append(ichitango)
        
#Converts the array of returned statements to a character string and outputs it (then specify a blank space as the delimiter for the elements of the array)
print(' '.join(kaesubun))

I c'ulnodt bvlieee that I could altluacy usraetdnnd what I was rdnieag : the paemonnhel pewor of the hmuan mind .

The first notation is ''. Join (kaesubun).

I'm converting a string list separated by one character into a string. If there is a list of [" a "," b "," c "] By writing ''. Join (list), ʻabcwill be output. By writing'a'. join (list), ʻa a b a c will be output. ("A" is the delimiter)

Well, I'm fine.

Alright! !! !! This completes all 10 questions in Chapter 1 of Language Processing! !! !! !!

** This article alone took 2 hours! !! !! !! !! !! !! !! (important)** ** Chapter 1 took 7 hours! !! !! !! !! !! (important)**

From tomorrow, we will start in Chapter 2.

Recommended Posts

Python inexperienced person tries to knock 100 language processing 14-16
Python inexperienced person tries to knock 100 language processing 07-09
Python inexperienced person tries to knock 100 language processing 10 ~ 13
Python inexperienced person tries to knock 100 language processing 05-06
Python inexperienced person tries to knock 100 language processing 00-04
100 Language Processing with Python Knock 2015
100 Language Processing Knock Chapter 1 (Python)
100 Language Processing Knock Chapter 2 (Python)
100 Language Processing Knock with Python (Chapter 1)
100 Language Processing Knock Chapter 1 in Python
100 Language Processing Knock with Python (Chapter 3)
Python beginner tried 100 language processing knock 2015 (05 ~ 09)
100 Language Processing Knock Chapter 1 by Python
Python beginner tried 100 language processing knock 2015 (00 ~ 04)
100 Language Processing Knock (2020): 28
100 Language Processing Knock (2020): 38
100 language processing knock 00 ~ 02
100 Language Processing Knock with Python (Chapter 2, Part 2)
100 Language Processing Knock with Python (Chapter 2, Part 1)
100 language processing knock 2020 [00 ~ 39 answer]
100 language processing knock 2020 [00-79 answer]
100 Language Processing Knock 2020 Chapter 1
100 Amateur Language Processing Knock: 17
100 language processing knock 2020 [00 ~ 49 answer]
Python: Natural language processing
100 Language Processing Knock-52: Stemming
100 Language Processing Knock Chapter 1
Introduction to Python language
100 Amateur Language Processing Knock: 07
100 Language Processing Knock 2020 Chapter 3
100 Language Processing Knock 2020 Chapter 2
100 Amateur Language Processing Knock: 09
100 Amateur Language Processing Knock: 47
100 Language Processing Knock-53: Tokenization
100 Amateur Language Processing Knock: 97
100 language processing knock 2020 [00 ~ 59 answer]
100 Amateur Language Processing Knock: 67
Entry where Python beginners do their best to knock 100 language processing little by little
100 language processing knock-92 (using Gensim): application to analogy data
[Chapter 3] Introduction to Python with 100 knocks of language processing
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Chapter 4] Introduction to Python with 100 knocks of language processing
100 Language Processing Knock-51: Word Clipping
100 Language Processing Knock-58: Tuple Extraction
100 Language Processing Knock-57: Dependency Analysis
100 language processing knock-50: sentence break
100 Language Processing Knock-25: Template Extraction
I tried 100 language processing knock 2020
100 language processing knock-56: co-reference analysis
Solving 100 Language Processing Knock 2020 (01. "Patatokukashi")
100 Amateur Language Processing Knock: Summary
100 Language Processing Knock-43: Extracted clauses containing nouns related to clauses containing verbs
[Python] Try to classify ramen shops by natural language processing
100 language processing knock-42: Display of the phrase of the person concerned and the person concerned
Leave the troublesome processing to Python
100 Language Processing Knock 2020 Chapter 2: UNIX Commands
100 Language Processing Knock 2015 Chapter 5 Dependency Analysis (40-49)
100 Language Processing Knock 2020 Chapter 4: Morphological Analysis
100 Language Processing Knock 2020 Chapter 9: RNN, CNN
100 language processing knock-76 (using scikit-learn): labeling
100 language processing knock-55: named entity extraction