[自然言語処理100本ノック] 第1章

Published : 2020-05-30   Lastmod : 2021-11-07

## 第1章 準備運動

### github

• githubのjupyter notebook形式のファイルはこちら

### 筆者の環境

!sw_vers

ProductName:	Mac OS X
ProductVersion:	10.14.6
BuildVersion:	18G95

!python -V

Python 3.5.5 :: Anaconda, Inc.


## 解答

### 00問

a = 'stressed'

a[::-1]

'desserts'


### 01問

a = 'パタトクカシーー'

a[1::2]

'タクシー'


### 02問

a = 'パトカー'
b = 'タクシー'

''.join([ i + j for i,j in zip(a,b)])

'パタトクカシーー'


### 03問

a = 'Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.'

list(map(lambda x: len(x.replace(',','').replace('.','')), a.split(' ')))

[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]


### 04問

a = 'Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can.'
b = [1,5,6,7,8,9,15,16,19]

ret = {}
for i, s in enumerate(map(lambda x: x.replace(',','').replace('.',''), a.split(' '))):
if i + 1 in b:
ret.update({
s[0] : i + 1
})
else:
ret.update({
s[0:2] : i + 1
})

ret

{'Al': 13,
'Ar': 18,
'B': 5,
'Be': 4,
'C': 6,
'Ca': 20,
'Cl': 17,
'F': 9,
'H': 1,
'He': 2,
'K': 19,
'Li': 3,
'Mi': 12,
'N': 7,
'Na': 11,
'Ne': 10,
'O': 8,
'P': 15,
'S': 16,
'Si': 14}


### 05問

def n_gram(arg, n):
return [arg[i: i + n] for i in range(len(arg)) if i + n <= len(arg)]

a = 'I am an NLPer'

print(n_gram(a.split(' '), 2))
print(n_gram(a, 2))

[['I', 'am'], ['am', 'an'], ['an', 'NLPer']]
['I ', ' a', 'am', 'm ', ' a', 'an', 'n ', ' N', 'NL', 'LP', 'Pe', 'er']


### 06問

a = 'paraparaparadise'
b = 'paragraph'

X = set(n_gram(a,2))
Y = set(n_gram(b,2))

print('和集合 : ',X | Y)
print('積集合 : ', X ^ Y)
print('差集合 : ', X - Y)

if 'se' in X:
print('Xにseあり')
else:
print('Xにseなし')

if 'se' in Y:
print('Yにseあり')
else:
print('Yにseなし')

和集合 :  {'ap', 'is', 'ar', 'se', 'di', 'gr', 'ra', 'ag', 'ph', 'pa', 'ad'}

Xにseあり
Yにseなし


### 07問

def template(x,y,z):
return '{}時の{}は{}'.format(x,y,z)

template(12,'気温',22.4)

'12時の気温は22.4'


### 08問

def cipher(arg):
return ''.join([chr(219 - ord(i)) if i.islower() else i for i in arg])

orig = 'sdf234DSFsdf'
print('orig      : ',orig)
encrypt = cipher(orig)
print('encrypted : ',encrypt)
decrypt = cipher(encrypt)
print('decrypted : ',decrypt)
print('judgement : ', orig == decrypt)

orig      :  sdf234DSFsdf
encrypted :  hwu234DSFhwu
decrypted :  sdf234DSFsdf
judgement :  True


### 09問

import random

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

def _09(arg):
return ' '.join(list(map(lambda x: x[0] + ''.join(random.sample(x[1:-1], len(x[1:-1]))) + x[-1] if len(x) > 4 else x , a.split(' '))))

_09(a)

'I cu’noldt bveliee that I could aaluctly usnadnretd what I was ridneag : the poemeannhl peowr of the haumn mind .'