diff --git a/README.md b/README.md index 0cbda48..2323469 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ $git clone [the URL] + ## Manage your versions: to create the branch on your local machine: @@ -19,3 +20,4 @@ download all branches from the remote: #### Note: The solutions can be found in the solutions branch. + diff --git a/up1/AllSonnets.txt b/up1solution/AllSonnets.txt similarity index 100% rename from up1/AllSonnets.txt rename to up1solution/AllSonnets.txt diff --git a/up1/UP1 Spec.pdf b/up1solution/UP1 Spec.pdf similarity index 100% rename from up1/UP1 Spec.pdf rename to up1solution/UP1 Spec.pdf diff --git a/up1solution/indexer.py b/up1solution/indexer.py new file mode 100755 index 0000000..3b0fa9b --- /dev/null +++ b/up1solution/indexer.py @@ -0,0 +1,84 @@ +# -*- coding: utf-8 -*- +""" +Created on Sat Jul 5 11:38:58 2014 + +@author: zzhang +""" +import pickle + +class Index: + def __init__(self, name): + self.name = name + self.msgs = []; + self.index = {} + self.total_msgs = 0 + self.total_words = 0 + + def get_total_words(self): + return self.total_words + + def get_msg_size(self): + return self.total_msgs + + def get_msg(self, n): + return self.msgs[n] + + def add_msg(self, m): + self.msgs.append(m) + self.total_msgs += 1 + + def add_msg_and_index(self, m): + self.add_msg(m) + line_at = self.total_msgs - 1 + self.indexing(m, line_at) + + def indexing(self, m, l): + words = m.split() + self.total_words += len(words) + for wd in words: + if wd not in self.index: + self.index[wd] = [l,] + else: + self.index[wd].append(l) + + def search(self, term): + msgs = [] + if (term in self.index.keys()): + indices = self.index[term] + msgs = [(i, self.msgs[i]) for i in indices] + return msgs + +class PIndex(Index): + def __init__(self, name): + super().__init__(name) + roman_int_f = open('roman.txt.pk', 'rb') + self.int2roman = pickle.load(roman_int_f) + roman_int_f.close() + self.load_poems() + + # load poems + def load_poems(self): + lines = open(self.name, 'r').readlines() + for l in lines: + self.add_msg_and_index(l.rstrip()) + + def get_poem(self, p): + p_str = self.int2roman[p] + '.' + p_next_str = self.int2roman[p + 1] + '.' + [(go_line, m)] = self.search(p_str) + poem = [] + end = self.get_msg_size() + while go_line < end: + this_line = self.get_msg(go_line) + if this_line == p_next_str: + break + poem.append(this_line) + go_line += 1 + return poem + +if __name__ == "__main__": + sonnets = PIndex("AllSonnets.txt") + p3 = sonnets.get_poem(3) + print(p3) + s_love = sonnets.search("love") + print(s_love) \ No newline at end of file diff --git a/up1solution/indexer_implement.py b/up1solution/indexer_implement.py new file mode 100644 index 0000000..fab4643 --- /dev/null +++ b/up1solution/indexer_implement.py @@ -0,0 +1,119 @@ +# -*- coding: utf-8 -*- +import pickle +import string + + +class Index: + def __init__(self, name): + self.name = name + self.msgs = []; + self.index = {} + self.total_msgs = 0 + self.total_words = 0 + + def get_total_words(self): + return self.total_words + + def get_msg_size(self): + return self.total_msgs + + def get_msg(self, n): + return self.msgs[n] + + def add_msg(self, m): + self.msgs.append(m) + self.total_msgs += 1 + + def add_msg_and_index(self, m): + self.add_msg(m) + line_at = self.total_msgs - 1 + self.indexing(m, line_at) + + def indexing(self, m, l): + # m = m.strip(string.punctuation + '\n') + #print(m) + words = m.split() + self.total_words += len(words) + for wd in words: + wd = wd.strip(string.punctuation + '\n') + #print(wd) + if wd not in self.index: + self.index[wd] = [l] + else: + self.index[wd].append(l) + + def search(self, term): + msgs = [] + ##Here, term is a phrase containts a few words. + words = term.split() + indices = set(self.index[words.pop()]) + while words: + ##Pick out those duplicated + indices.intersection_update(set(self.index[words.pop()])) + for i in indices: + if term in self.msgs[i]: ## refine the indices with the term + msgs.append((i, self.msgs[i])) + return msgs + +# terms = term.split() +# if terms[0] in self.index.keys(): +# indices = self.index[terms[0]] +# for i in indices: +# if term in self.msgs[i]: +# msgs.append((i, self.msgs[i])) +# return msgs +# msgs = [] +# if (term in self.index.keys()): +# indices = self.index[term] +# msgs = [(i, self.msgs[i]) for i in indices] +# return msgs + +class PIndex(Index): + def __init__(self, name): + super().__init__(name) + roman_int_f = open('roman.txt.pk', 'rb') + self.int2roman = pickle.load(roman_int_f) + #print(self.int2roman) + roman_int_f.close() + self.load_poems() + + # load poems + def load_poems(self): + lines = open(self.name, 'r').readlines() + for l in lines: + if l.rstrip(string.punctuation + '\n') in self.int2roman.values(): + #print(l.rstrip(string.punctuation + '\n')) + self.add_msg_and_index('chapter' + l.strip()) + #print(('chapter' + l).strip(string.punctuation + '\n')) + else: + self.add_msg_and_index(l.strip()) + + def get_poem(self, p): + p_str = 'chapter' + self.int2roman[p] + p_next_str = 'chapter' + self.int2roman[p + 1] + '.' + [(go_line, m)] = self.search(p_str) + poem = [] + end = self.get_msg_size() + theFirst = True + while go_line < end: + this_line = self.get_msg(go_line) + if theFirst: + this_line = this_line[7:] #remove the 'charpter' + theFirst = False + if this_line == p_next_str: + break + poem.append(this_line) + go_line += 1 + return poem + +if __name__ == "__main__": + sonnets = PIndex("AllSonnets.txt") + p3 = sonnets.get_poem(3) + print(p3) + s_love = sonnets.search("love in") + print(s_love) + s_look = sonnets.search("look in") + print(s_look) + s_look = sonnets.search("look into") + print(s_look) + diff --git a/up1/indexer_student.py b/up1solution/indexer_student.py similarity index 100% rename from up1/indexer_student.py rename to up1solution/indexer_student.py diff --git a/up1/p1.txt b/up1solution/p1.txt similarity index 100% rename from up1/p1.txt rename to up1solution/p1.txt diff --git a/up1/roman.txt b/up1solution/roman.txt similarity index 100% rename from up1/roman.txt rename to up1solution/roman.txt diff --git a/up1/roman.txt.pk b/up1solution/roman.txt.pk similarity index 100% rename from up1/roman.txt.pk rename to up1solution/roman.txt.pk diff --git a/up1/roman2num.py b/up1solution/roman2num.py similarity index 100% rename from up1/roman2num.py rename to up1solution/roman2num.py diff --git a/up2/UP2 Spec_ managing the group membership.pdf b/up2/UP2 Spec_ managing the group membership.pdf deleted file mode 100644 index 370e12d..0000000 Binary files a/up2/UP2 Spec_ managing the group membership.pdf and /dev/null differ diff --git a/up2/chat_group_student.py b/up2/chat_group_student.py deleted file mode 100755 index bb7243f..0000000 --- a/up2/chat_group_student.py +++ /dev/null @@ -1,77 +0,0 @@ -S_ALONE = 0 -S_TALKING = 1 - -#============================================================================== -# Group class: -# member fields: -# - An array of items, each a Member class -# - A dictionary that keeps who is a chat group -# member functions: -# - join: first time in -# - leave: leave the system, and the group -# - list_my_peers: who is in chatting with me? -# - list_all: who is in the system, and the chat groups -# - connect: connect to a peer in a chat group, and become part of the group -# - disconnect: leave the chat group but stay in the system -#============================================================================== - -class Group: - - def __init__(self): - self.members = {} - self.chat_grps = {} - self.grp_ever = 0 - - def join(self, name): - self.members[name] = S_ALONE - return - - - #implement - def is_member(self, name): - return False - - #implement - def leave(self, name): - return - - #implement - def find_group(self, name): - found = False - group_key = 0 - return found, group_key - - #implement - def connect(self, me, peer): - #if peer is in a group, join it - peer_in_group, group_key = self.find_group(peer) - # otherwise, create a new group with you and your peer - return - - #implement - def disconnect(self, me): - # find myself in the group, quit - return - - def list_all(self): - # a simple minded implementation - full_list = "Users: ------------" + "\n" - full_list += str(self.members) + "\n" - full_list += "Groups: -----------" + "\n" - full_list += str(self.chat_grps) + "\n" - return full_list - - #implement - def list_me(self, me): - # return a list, "me" followed by other peers in my group - my_list = [] - return my_list - -if __name__ == "__main__": - g = Group() - g.join('a') - g.join('b') - print(g.list_all()) - - g.connect('a', 'b') - print(g.list_all())