Exemple #1
0
 def about_helper(paragraph):
     paragraph = remove_punctuation(lower(paragraph)).split()
     # print(paragraph)
     for i in topic:
         if i in paragraph:
             return True
     return False
Exemple #2
0
 def f(x):
     splitted = split(x)
     ls = [lower(remove_punctuation(s)) for s in splitted]
     for item in ls:
         if item in topic:
             return True
     return False
Exemple #3
0
 def start(paragraphs):
     for word in topic:
         s = split(remove_punctuation(lower(paragraphs)))
         for i in s:
             if i == word:
                 return True
     return False
Exemple #4
0
 def search(paragraph):
     paragraph = split(lower(remove_punctuation(paragraph)))
     for i in topic:
         for j in paragraph:
             if i == j:
                 return True
     return False
Exemple #5
0
 def select(paragraph):
     remove_punctuation_paragraph = remove_punctuation(paragraph) # remove punction
     split_paragraph_list = split(remove_punctuation_paragraph)  #split paragraph
     for splited_paragraph in split_paragraph_list:  
         if lower(splited_paragraph) in topic:   # return a lowercased version 
             return True
     return False
Exemple #6
0
def about(topic):
    """Return a select function that returns whether
    a paragraph contains one of the words in TOPIC.

    Arguments:
        topic: a list of words related to a subject

    >>> about_dogs = about(['dog', 'dogs', 'pup', 'puppy'])
    >>> choose(['Cute Dog!', 'That is a cat.', 'Nice pup!'], about_dogs, 0)
    'Cute Dog!'
    >>> choose(['Cute Dog!', 'That is a cat.', 'Nice pup.'], about_dogs, 1)
    'Nice pup.'
    """
    assert all([lower(x) == x for x in topic]), 'topics should be lowercase.'
    # BEGIN PROBLEM 2
    "*** YOUR CODE HERE ***"

    def select(paragraph):
        paragraph = split(remove_punctuation(paragraph))
        for i in range(len(paragraph)):
            if lower(paragraph[i]) in topic:
                return True
        return False

    return select
Exemple #7
0
def about(topic):
    """Return a select function that returns whether a paragraph contains one
    of the words in TOPIC.

    >>> about_dogs = about(['dog', 'dogs', 'pup', 'puppy'])
    >>> choose(['Cute Dog!', 'That is a cat.', 'Nice pup!'], about_dogs, 0)
    'Cute Dog!'
    >>> choose(['Cute Dog!', 'That is a cat.', 'Nice pup.'], about_dogs, 1)
    'Nice pup.'
    """
    assert all([lower(x) == x for x in topic]), 'topics should be lowercase.'
    # BEGIN PROBLEM 2
    "*** YOUR CODE HERE ***"

    def f(p):
        p = remove_punctuation(p)
        p = lower(p)
        p = split(p)
        for i in p:
            for j in topic:
                if i == j:
                    return True
        return False

    return f
Exemple #8
0
def about(topic):
    """Return a select function that returns whether a paragraph contains one
    of the words in TOPIC.

    >>> about_dogs = about(['dog', 'dogs', 'pup', 'puppy'])
    >>> choose(['Cute Dog!', 'That is a cat.', 'Nice pup!'], about_dogs, 0)
    'Cute Dog!'
    >>> choose(['Cute Dog!', 'That is a cat.', 'Nice pup.'], about_dogs, 1)
    'Nice pup.'
    """
    assert all([lower(x) == x for x in topic]), 'topics should be lowercase.'

    # BEGIN PROBLEM 2
    "*** YOUR CODE HERE ***"

    def is_topic_mentioned(
            paragraph):  #parapraph is a string,choose from def choose
        lowered_paragraph = lower(paragraph)
        lowered_nopunc_paragraph = remove_punctuation(lowered_paragraph)
        lowered_nopunc_paragraph_list = split(lowered_nopunc_paragraph)
        for keyword in topic:
            if keyword in lowered_nopunc_paragraph_list:
                return True
        return False

    return is_topic_mentioned
Exemple #9
0
 def helper(str):
     str = split(remove_punctuation(lower(str)))
     for i in range(0, len(str)):
         for j in range(0, len(topic)):
             if (str[i] == topic[j]):
                 return True
     return False
Exemple #10
0
def about(topic):
    """Return a select function that returns whether a paragraph contains one
    of the words in TOPIC.

    >>> about_dogs = about(['dog', 'dogs', 'pup', 'puppy'])
    >>> choose(['Cute Dog!', 'That is a cat.', 'Nice pup!'], about_dogs, 0)
    'Cute Dog!'
    >>> choose(['Cute Dog!', 'That is a cat.', 'Nice pup.'], about_dogs, 1)
    'Nice pup.'
    """
    assert all([lower(x) == x for x in topic]), 'topics should be lowercase.'

    # BEGIN PROBLEM 2
    def select(string):
        # need to make lowercase and remove punctuation
        s = remove_punctuation(string)
        s = lower(s)
        # split for comparing
        s = split(s)
        for i in topic:
            if i in s:
                return True
        return False

    return select
Exemple #11
0
 def sentence_about(sentence):
     words = split(sentence)
     words = [lower(remove_punctuation(w)) for w in words]
     for w in words:
         if w in topic:
             return True
     return False
Exemple #12
0
 def helpler(paragraph):
     new_para = split(lower(remove_punctuation(paragraph)))
     for i in new_para:
         for x in topic:
             if i == x:
                 return True
     return False
Exemple #13
0
 def is_about_topic(s):
     low_s = lower(remove_punctuation(s))
     for _s in split(low_s):
         for _topic in topic:
             if _s == _topic:
                 return True
     return False
Exemple #14
0
    def __getitem__(self, index):
        text = self.text[index]
        text = utils.lower(text)
        text = utils.remove_hashtags(text)
        text = utils.remove_user_mentions(text)
        text = utils.remove_urls(text)

        data = np.array([
            config.identity_mat[config.vocabulary.index(i)]
            for i in list(text)[::-1] if i in config.vocabulary
        ],
                        dtype=np.float32)
        if len(data) > config.max_length:
            data = data[:config.max_length]
        elif 0 < len(data) < config.max_length:
            data = np.concatenate((data,
                                   np.zeros((config.max_length - len(data),
                                             config.number_of_characters),
                                            dtype=np.float32)))
        elif len(data) == 0:
            data = np.zeros((config.max_length, config.number_of_characters),
                            dtype=np.float32)

        label = self.labels[index]
        data = torch.Tensor(data)

        return data, label
Exemple #15
0
 def select(paragraphs):
     paragraphs = lower(paragraphs)
     paragraphs = remove_punctuation(paragraphs)
     list_paragraphs = split(paragraphs)
     for words in topic:
         if words in list_paragraphs:
             return True
     return False
 def select(paragraph):
     paragraph = remove_punctuation(paragraph)
     paragraph = lower(paragraph)
     # list of all words in paragraph
     paragraph = split(paragraph)
     for word in topic:
         if (word in paragraph):
             return True
     return False
Exemple #17
0
 def about_helper(words):
     words = remove_punctuation(words)
     words = lower(words)
     words = split(words)
     for i in topic:
         for j in words:
             if i == j:
                 return True
     return False
Exemple #18
0
 def is_topic_mentioned(
         paragraph):  #parapraph is a string,choose from def choose
     lowered_paragraph = lower(paragraph)
     lowered_nopunc_paragraph = remove_punctuation(lowered_paragraph)
     lowered_nopunc_paragraph_list = split(lowered_nopunc_paragraph)
     for keyword in topic:
         if keyword in lowered_nopunc_paragraph_list:
             return True
     return False
Exemple #19
0
    def valid_topic(paragraph):
        paragraph = remove_punctuation(paragraph)
        paragraph = lower(paragraph)
        split_paragraph = split(paragraph)

        for split_words in split_paragraph:
            if split_words in topic:
                return True
        return False
Exemple #20
0
    def func(paragraph):
        paragraph = split(remove_punctuation(lower(paragraph)))

        filtered = [x for x in topic if x in paragraph]

        if filtered != []:
            return True
        else:
            return False
Exemple #21
0
 def f(p):
     p = remove_punctuation(p)
     p = lower(p)
     p = split(p)
     for i in p:
         for j in topic:
             if i == j:
                 return True
     return False
Exemple #22
0
 def select(string):
     # need to make lowercase and remove punctuation
     s = remove_punctuation(string)
     s = lower(s)
     # split for comparing
     s = split(s)
     for i in topic:
         if i in s:
             return True
     return False
    def clean_doc(self, doc):
        # Remove special character and word too long and convert to lower case
        cleaned_doc = []
        doc = self.re.sub('', doc)
        for word in doc.strip().split():
            if MIN_WORD_LENGTH <= len(
                    word
            ) <= MAX_WORD_LENGTH and word not in self.en_stop_words:
                cleaned_doc.append(utils.lower(word))

        return " ".join(cleaned_doc)
Exemple #24
0
def about(topic):
    """Return a select function that returns whether a paragraph contains one
    of the words in TOPIC.

    >>> about_dogs = about(['dog', 'dogs', 'pup', 'puppy'])
    >>> choose(['Cute Dog!', 'That is a cat.', 'Nice pup!'], about_dogs, 0)
    'Cute Dog!'
    >>> choose(['Cute Dog!', 'That is a cat.', 'Nice pup.'], about_dogs, 1)
    'Nice pup.'
    """
    assert all([lower(x) == x for x in topic]), 'topics should be lowercase.'
    # BEGIN PROBLEM 2
    "*** YOUR CODE HERE ***"
Exemple #25
0
def about(topic):
    """Return a select function that returns whether a paragraph contains one
    of the words in TOPIC.
    构造一个select函数,参数为sentence,检查topic的单词是否在sentence之中
    @return function

    >>> about_dogs = about(['dog', 'dogs', 'pup', 'puppy'])
    >>> choose(['Cute Dog!', 'That is a cat.', 'Nice pup!'], about_dogs, 0)
    'Cute Dog!'
    >>> choose(['Cute Dog!', 'That is a cat.', 'Nice pup.'], about_dogs, 1)
    'Nice pup.'
    """
    assert all([lower(x) == x for x in topic]), 'topics should be lowercase.'
    # BEGIN PROBLEM 2
    "*** YOUR CODE HERE ***"
    # def helper(sentence):
    #     sentence_list = split(lower(remove_punctuation(sentence)))
    #     for word in topic:
    #         if word in sentence_list:
    #             return True
    #     return False
    # return helper
    return lambda sentence: any(word in split(lower(remove_punctuation(sentence))) for word in topic)
Exemple #26
0
def autocorrect(user_word, valid_words, diff_function, limit):
    """Returns the element of VALID_WORDS that has the smallest difference
    from USER_WORD. Instead returns USER_WORD if that difference is greater
    than LIMIT.
    """
    # BEGIN PROBLEM 5
    "*** YOUR CODE HERE ***"
    user_word_after = remove_punctuation(lower(user_word))
    diff_list = []
    for i in valid_words:
        if user_word_after == i:
            return user_word
        else:
            diff_list += [diff_function(user_word_after, i, limit)]
    if min(diff_list) > limit:
        return user_word
    else:
        return valid_words[diff_list.index(min(diff_list))]
Exemple #27
0
def about(topic):
    """Return a select function that returns whether a paragraph contains one
    of the words in TOPIC.

    >>> about_dogs = about(['dog', 'dogs', 'pup', 'puppy'])
    >>> choose(['Cute Dog!', 'That is a cat.', 'Nice pup!'], about_dogs, 0)
    'Cute Dog!'
    >>> choose(['Cute Dog!', 'That is a cat.', 'Nice pup.'], about_dogs, 1)
    'Nice pup.'
    """
    assert all([lower(x) == x for x in topic]), 'topics should be lowercase.'

    def helper(sentence):
        sentence = split(lower(remove_punctuation(sentence)))
        for i in range(len(topic)):
            if topic[i] in sentence:
                return True
        return False

    return helper
Exemple #28
0
def about(topic):
    """Return a select function that returns whether a paragraph contains one
    of the words in TOPIC.

    >>> about_dogs = about(['dog', 'dogs', 'pup', 'puppy'])
    >>> choose(['Cute Dog!', 'That is a cat.', 'Nice pup!'], about_dogs, 0)
    'Cute Dog!'
    >>> choose(['Cute Dog!', 'That is a cat.', 'Nice pup.'], about_dogs, 1)
    'Nice pup.'
    """
    assert all([lower(x) == x for x in topic]), 'topics should be lowercase.'
    # BEGIN PROBLEM 2
    def select(paragraphs):
        paragraphs = lower(paragraphs)
        paragraphs = remove_punctuation(paragraphs)
        list_paragraphs = split(paragraphs)
        for words in topic:
            if words in list_paragraphs:
                return True
        return False
    return select
Exemple #29
0
def about(topic):
    """Return a select function that returns whether a paragraph contains one
    of the words in TOPIC.

    >>> about_dogs = about(['dog', 'dogs', 'pup', 'puppy'])
    >>> choose(['Cute Dog!', 'That is a cat.', 'Nice pup!'], about_dogs, 0)
    'Cute Dog!'
    >>> choose(['Cute Dog!', 'That is a cat.', 'Nice pup.'], about_dogs, 1)
    'Nice pup.'
    """
    assert all([lower(x) == x for x in topic]), 'topics should be lowercase.'
    # BEGIN PROBLEM 2
    "*** YOUR CODE HERE ***"
    def select(paragraph):
        remove_punctuation_paragraph = remove_punctuation(paragraph) # remove punction
        split_paragraph_list = split(remove_punctuation_paragraph)  #split paragraph
        for splited_paragraph in split_paragraph_list:  
            if lower(splited_paragraph) in topic:   # return a lowercased version 
                return True
        return False
    return select    
Exemple #30
0
def about(topic):
    """Return a select function that returns whether a paragraph contains one
    of the words in TOPIC.

    >>> about_dogs = about(['dog', 'dogs', 'pup', 'puppy'])
    >>> choose(['Cute Dog!', 'That is a cat.', 'Nice pup!'], about_dogs, 0)
    'Cute Dog!'
    >>> choose(['Cute Dog!', 'That is a cat.', 'Nice pup.'], about_dogs, 1)
    'Nice pup.'
    """
    assert all([lower(x) == x for x in topic]), 'topics should be lowercase.'

    # BEGIN PROBLEM 2
    def helper(str):
        str = split(remove_punctuation(lower(str)))
        for i in range(0, len(str)):
            for j in range(0, len(topic)):
                if (str[i] == topic[j]):
                    return True
        return False

    return helper