1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ニートの自然言語処理100本ノック:7

Posted at

"""
06. 集合
"paraparaparadise""paragraph"に含まれる文字bi-gramの集合を,それぞれ, XとYとして求め,XとYの和集合,積集合,差集合を求めよ.さらに,'se'というbi-gramがXおよびYに含まれるかどうかを調べよ.
"""
import itertools

# bi-gramを作る
def make_str_bigram(text,n=2):
    
    text = list(text)
    iterr = itertools.combinations(text,n)
    lists = []
    for i in iterr:
        lists.append(i)
    return lists
    

def ck_groups(X,Y): # 集合をチェックする
    union = X.union(Y) # 和集合
    diff = X.difference(Y) # 差集合
    product = X.intersection(Y) # 積集合
    print("和集合:{0}\n差集合:{1}\n積集合:{2}\n".format(union,diff,product))

    return union,diff,product


def find_se(X,Y):

    if 'se' in X or 'se' in Y:
        if 'se' in X:
            print('seはXに含まれる')
        else:
            print('seはYに含まれる')
    else:
        print('seは含まれない。')
        


text1 = "paraparaparadise"
text2 = "paragraph"

input_X = set(make_str_bigram(text1,2))
input_Y = set(make_str_bigram(text2,2))

ck_groups(input_X,input_Y)

find_se(input_X,input_Y)
和集合:{('a', 'g'), ('a', 'h'), ('d', 'i'), ('g', 'a'), ('p', 'h'), ('r', 'h'), ('d', 'e'), ('p', 'd'), ('s', 'e'), ('r', 'a'), ('r', 'd'), ('p', 'g'), ('g', 'h'), ('p', 'a'), ('r', 'g'), ('a', 'i'), ('a', 's'), ('g', 'r'), ('a', 'd'), ('p', 'i'), ('r', 'p'), ('r', 'i'), ('p', 'p'), ('i', 's'), ('p', 'e'), ('i', 'e'), ('r', 'e'), ('g', 'p'), ('a', 'e'), ('p', 'r'), ('r', 's'), ('r', 'r'), ('p', 's'), ('a', 'p'), ('a', 'r'), ('a', 'a'), ('d', 's')}
差集合:{('r', 'i'), ('a', 'e'), ('r', 's'), ('i', 's'), ('p', 'd'), ('s', 'e'), ('p', 's'), ('r', 'd'), ('d', 'i'), ('p', 'e'), ('i', 'e'), ('a', 'i'), ('r', 'e'), ('a', 's'), ('a', 'd'), ('p', 'i'), ('d', 's'), ('d', 'e')}
積集合:{('p', 'r'), ('p', 'p'), ('a', 'a'), ('r', 'r'), ('r', 'a'), ('a', 'p'), ('p', 'a'), ('a', 'r'), ('r', 'p')}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?