python 및 머신러닝/Coding The Matrix
[Coding The Matrix] 코딩 더 매트릭스 - Chapter 1.6 역 인덱스
java개발자
2015. 8. 13. 12:05
# -*- coding: utf-8 -*-
from myutil import consolePrintWithLineNumber as c
'''
Created on 2015. 8. 12.
@author: Administrator
'''
# 35p
# 1.6 Lab: 파이썬-모듈 및 제어 구조-역 인덱스
import math
from random import randint
import dictutil
# helc(math) #도움말 페이지
c(math.sqrt(3))
# 36p
# c(math.sqrt(-1)) # ValueError: math domain error
c(math.cos(math.pi))
c(math.log(math.e))
c(randint(1, 5))
# Task1.6.2 문제 이해 안됨;;; 균등하게 선택된 문자열이 무엇인가? 그냥 랜덤하게 선택하라고?
def movie_review(name_list): return name_list[randint(0, len(name_list) - 1)]
c(movie_review(["terminator genisys", "See it", "A gem"]))
# 이클립스로 하면 같은 경로의 파일을 가져오지 못한다. 다른 방법 필요...
# import 관련 참고문서 : http://felixblog.tistory.com/57
# http://aw2s0m2.blogspot.kr/2011/02/import.html
# > 이클립스로 할 경우, 프로젝트 프로퍼티에 관련 폴더를 추가해야 한다.
# >> http://stackoverflow.com/questions/4631377/unresolved-import-issues-with-pydev-and-eclipse
c(dictutil.dict2list(1, 2))
# 37p
c(dictutil.listrange2dict(['A', 'B', 'C']))
def test():
for x in [1, 2, 3]:
y = x * x
c(y)
test()
# 38p
# 39p
# f = open('stories_big.txt')
# for line in f:
# c(line)
f = open('stories_small.txt')
stories = list(f)
c(len(stories))
mystr = 'Ask not what you can do for your country'
c(mystr.split())
c(list(enumerate(['A', 'B', 'C']))) # enumerate object...
c([i * s for (i, s) in enumerate(['A', 'B', 'C', 'D', 'E'])])
# 40p
# Task1.6.6 한국사람이 번역한 것이 아닌 것 같다.-_-% 마치 구글번역기를 돌린 것 같은 부자연스러운 문맥. 갑자기 너무 어려운 문제가 나왔다.
def makeInverseIndex(strlist):
dic = {} # 딕셔너리
for (index, line) in enumerate(strlist):
wordList = line.split()
for word in wordList:
if word in dic:
dic[word].add(index) # 기존 집합에 원소 추가
else:
dic[word] = {index} # 집합 생성
return dic
inverseIndex = makeInverseIndex(["a b cc d e f g", "1 2 3 4", "a b cc", "3 4"])
c(inverseIndex) # {'4': {1, 3}, 'g': {0}, 'a': {0, 2}, 'd': {0}, 'e': {0}, '2': {1}, 'b': {0, 2}, '1': {1}, 'cc': {0, 2}, '3': {1, 3}, 'f': {0}}
# Task1.6.7
def orSearch(inverseIndex, querylist):
s = {} # 집합
for word in querylist:
if(len(s) == 0):
s = inverseIndex[word]
else:
s.update(inverseIndex[word]) # 집합 합치기
return s
c(orSearch(inverseIndex, ["1", "cc"])) # {0, 1, 2}
# Task1.6.8
def andSearch(inverseIndex, querylist):
s = {}
for word in querylist:
if(len(s) == 0):
s = inverseIndex[word]
else:
s.intersection_update(inverseIndex[word])
return s
c(andSearch(inverseIndex, ["a", "b", "cc"]))
inverseIndex2 = makeInverseIndex(list(open('stories_small.txt')))
c(inverseIndex2)
콘솔 출력 결과 20 > 1.7320508075688772 24 > -1.0 25 > 1.0 27 > 3 31 > terminator genisys 40 > 1 42 > {'B': 1, 'A': 0, 'C': 2} 47 > 1 47 > 4 47 > 9 57 > 50 60 > ['Ask', 'not', 'what', 'you', 'can', 'do', 'for', 'your', 'country'] 61 > [(0, 'A'), (1, 'B'), (2, 'C')] 62 > ['', 'B', 'CC', 'DDD', 'EEEE'] 78 > {'f': {0}, 'e': {0}, 'g': {0}, '3': {1, 3}, '4': {1, 3}, '2': {1}, 'd': {0}, 'a': {0, 2}, 'b': {0, 2}, 'cc': {0, 2}, '1': {1}} 90 > {0, 1, 2} 102 > {0, 2} 106 > {'guess': {46}, 'window': {25}, 'coined': {6}, '5,442': {9}, 'lights': {11, 46}, ,,,,,,,,,,,,'Sales': {35}, 'data': {2}, 'Zadar': {25}, 'produce': {4, 13, 31}}