Пример #1
0
 def add_comment(self, comment, label):
     if label == 1:
         m = self.pos
         self.pos_count += 1
     else:
         m = self.neg
         self.neg_count += 1
     content = split(comment, self.language)
     for i in range(len(content)):
         txt = "".join(content[i:i + self.gram_number])
         m[txt] = m.get(txt, 0) + 1
Пример #2
0
def send_mend_photo(message):
    if TextCheck.check_en(message.text):
        imgs = split(message.text)
        img = make_jpg(imgs)
        bot.send_photo(message.chat.id, img)
        incoming(message.text, message.chat.id, message.chat.username)

    else:
        bot.send_message(
            message.chat.id,
            'Пожалуйста, введите только одно слово на английском языке')
Пример #3
0
 def build_indices(self,data):
     word_count = {}
     for _,comment,_ in data:
         for word in split(comment,self.language):
             word_count[word] = word_count.get(word,0) + 1
     words = [(word_count[key],key) for key in word_count]
     words = sorted(words)
     length = len(words)
     for i in range(length):
         if i < self.head_word or length - i < self.tail_word:
             self.indices[words[i][1]] = self.oov_word
         else:
             self.indices[words[i][1]] = i + self.start_from - self.head_word
Пример #4
0
 def predict_label(self, review):
     b = math.log(self.pos_count / self.neg_count)
     comment = split(review, self.language)
     for i in range(len(comment)):
         txt = "".join(comment[i:i + self.gram_number])
         p_pos = math.log(
             self.pos.get(txt, self.smooth_weight) / self.pos_count)
         p_neg = math.log(
             self.neg.get(txt, self.smooth_weight) / self.neg_count)
         b = b + (p_pos - p_neg)
     if b > self.threshold:
         return 1
     else:
         return -1
Пример #5
0
from parser import parse, split


def helper(test):
    a, b = test
    seta = a[1][a[0] - 1]
    setb = b[1][b[0] - 1]
    common = [x for x in setb if x in seta]
    if len(common) == 1:
        return common[0]
    elif len(common) > 1:
        return 'Bad magician!'
    else:
        return 'Volunteer cheated!'


# schema here
schema = [(), [2, [int, 4, [split(int)]]]]

num_tests, tests = parse(schema)
for case, test in enumerate(tests):
    sol = helper(test)
    print 'Case #{}: {}'.format(case + 1, sol)
Пример #6
0
 def convert_to_labels(self,comment):
     return [self.indices.get(word,0) for word in split(comment,self.language)]
Пример #7
0
def parsePlaces(game):

    arr_of_places = []

    fpath = "./Game_Files/places.txt"
    with open(fpath) as f:
        read_data = f.read()
        data_chunks = read_data.split("***\n")

    ref_direction = {
        0: "n",
        1: "ne",
        2: "e",
        3: "se",
        4: "s",
        5: "sw",
        6: "w",
        7: "nw",
        8: "u",
        9: "d"
    }

    for p in data_chunks:
        doors = {
            "n": None,
            "ne": None,
            "e": None,
            "se": None,
            "s": None,
            "sw": None,
            "w": None,
            "nw": None,
            "u": None,
            "d": None
        }
        l = p.split("\n")
        name = l[0]
        adjacent_places = l[1].split(",")
        for a in adjacent_places:
            if a == "None":
                a = None
        doors_arr = l[2].split(",")

        if len(doors_arr) == 1:
            doors = {
                "n": None,
                "ne": None,
                "e": None,
                "se": None,
                "s": None,
                "sw": None,
                "w": None,
                "nw": None,
                "u": None,
                "d": None
            }
        else:
            door_dict = {
                "n": None,
                "ne": None,
                "e": None,
                "se": None,
                "s": None,
                "sw": None,
                "w": None,
                "nw": None,
                "u": None,
                "d": None
            }
            i = 0
            for d in doors_arr:
                if d != "None":
                    dir = ref_direction[i]
                    door_dict[dir] = d
                i += 1
            doors = door_dict

        place = g.Place(game, name, "day", "night", adjacent_places, None,
                        doors)
        arr_of_places.append(place)

    return arr_of_places