예제 #1
0
def count_words(sentence):
    n = len(sentence)
    # parsing
    s = sentence[0:n].lower()
    i = 0
    while i < n:
        if not isalnum(s[i]) and not isspace(s[i]):
            if i == 0:
                s = s[i + 1:]
            elif i == n - 1:
                s = s[0:i]
            elif s[i] == '\'' and isalpha(s[i - 1]) and isalpha(s[i + 1]):
                i += 1
            else:
                s = s[:i] + " " + s[i + 1:]
        elif isspace(s[i]):
            if isspace(s[i - 1]):
                # there can only be one space between words
                s = s[:i] + s[i + 1:]
            elif isalnum(s[i - 1]) and s[i] != ' ':
                # this will make it easier to split the lines
                s = s[:i] + " " + s[i + 1:]
            else:
                i += 1
        else:
            i += 1
        n = len(s)
    parsed_l = s.strip().split(' ')
    # searching
    counts = dict()
    for i in parsed_l:
        counts[i] = counts.get(i, 0) + 1
    return counts
def remove_punctuation(string):
    '''Write a short Python function that takes a string
    s, representing a sentence, and returns a copy of
    the string with all punctuation removed. For exam-
    ple, if given the string "Lets try, Mike.", this function
    would return "Lets try Mike".
    >>> remove_punctuation('Lets try, Mike.')
    'Lets try Mike'
    >>> remove_punctuation('OMG!, this is really amazing! @.@')
    'OMG this is really amazing '
    '''
    return ''.join([l for l in string if ascii.isalnum(l) or ascii.isblank(l)])
예제 #3
0
    def get_keywords(self):

        keywords_list = []
        for keyword in self.meta_keywords:
            flag = False
            for letter in keyword:
                if isalnum(letter):
                    flag = True
                    break
            if flag == True:
                keywords_list.append(self.clean_data(keyword))

        return keywords_list
예제 #4
0
    def savepath(self, name, ctx):
        # Chop out all non-alphabetic/digits
        # Otherwise this might be dangerous
        name = ''.join([c for c in name if isalnum(c)]).upper()

        # Hash the context
        ctxhash = hashlib.md5(ctx.encode()).hexdigest()

        # Get the prefix and make sure it exists
        prefix = self.config.get('save-prefix', 'advent-saves')
        if not os.path.exists(prefix):
            os.mkdir(prefix)

        return os.path.join(prefix, ctxhash + '.' + name)
예제 #5
0
def validateMail(s):
    l = s.split('@')
    
    if not len(l) == 2:
        #more than one or no '@'char
        return False
    
    userName = l[0]
    l = l[1].split('.')
    
    if not len(l) == 2:
        #more than one or no'.'char
        return False
    
    webName = l[0]
    extension = l[1]
    
    #test end first
    if len(extension) > 3 or len(extension) == 0 :
        return False
    
    if len(webName) == 0:
        return False
    
    for c in webName:
        if not isalnum(c):
            return False
    
    if len(userName) == 0:
        return False
    
    for c in userName:
        if not (isalnum(c) or c == '-' or c == '_'):
            return False
        
    #all validations are done
    return True
예제 #6
0
def guess_id(alb):
    """Tries to construct a valid id from an album's information."""

    from curses.ascii import isalnum

    s = '%s-%s' % (strxtra.idify(alb.artist.encode('iso-8859-1')),
                   strxtra.idify(alb.title.encode('iso-8859-1')))
    ss = ''
    for c in s:
        if isalnum(c) or c == '-':
            ss += c
        else:
            ss += '_'
    s = ss
    return ss
def precompute():
    """
    precomputes anagrams with /usr/share/dict/words as wordbase
    """
    starttime = time.time()
    file = open(englishWordFile, "r")
    # load all english words
    for line in file:
        line = line[:-1]
        line = line.lower()
        sortL = "".join(char for char in sorted(line))
        put = line + ":" + sortL
        englishWords.add(put)
    print("Loaded words %d" % len(englishWords))
    buckets = [set() for x in xrange(25)]
    for word in englishWords:
        buckets[len(word) / 2].add(word)
    for i in xrange(maxLength):
        print("%d: %d" % (i, len(buckets[i])))
    count = 0
    for word in englishWords:
        if count % 1000 == 0:
            print("Done for %d words" % count)
            print("Took %s seconds" % (time.time() - starttime))
        count += 1
        sortedWord = word.split(":")[1]
        if not isalnum(sortedWord[0]):
            continue
        if sortedWord not in hashMap:
            hashMap[sortedWord] = set()
            for word2 in buckets[len(sortedWord)]:
                sortedWord2 = word2.split(":")[1]
                if sortedWord == sortedWord2:
                    hashMap[sortedWord].add(word2.split(":")[0])
    anag = open(anagramsFile, "w")
    anag.truncate()
    count = 0
    for anagram, listOfAnagrams in hashMap.items():
        if count % 100 == 0:
            print("%d lists written" % (count))
            anag.flush()
        count += 1
        anag.write("%s:%s\n" % (anagram, listOfAnagrams))
    anag.close()
    print("Precomputation done")
예제 #8
0
파일: utils.py 프로젝트: RJVB/xxdiff4qt5
def idify(s, strip=True, preserve_chars=[]):
    """
    Removes non-alphanumeric characters from a string, morphing them into
    underscores.
    """
    ss = ''
    preserve_list = ['\n'] + preserve_chars
    for c in s:
        if isalnum(c):
            ss += c
        elif c in preserve_list:
            ss += c
        else:
            ss += '_'
    if strip:
        b, e = 0, len(ss)
        while b < len(ss) and ss[b] == '_': b += 1
        while e > 1 and ss[e-1] == '_': e -= 1
        ss = ss[b:e]
    return ss
예제 #9
0
def search_abilities(stdscr):
    my, mx = stdscr.getmaxyx()
    curses.noecho()
    #Get curses to do key conversion
    stdscr.keypad(True)

    search_input = ""
    selected = 0
    search_results = search(search_input)
    render_static(stdscr, search_input, search_results, selected)
    while True:
        character = stdscr.getkey()

        if len(character) > 1:
            if "KEY_BACKSPACE" == character:
                search_input = search_input[:-1]
                search_results = search(search_input)
                selected = 0
            elif "KEY_DOWN" == character and selected < len(search_results) - 1:
                selected += 1
            elif "KEY_UP" == character and selected > 0:
                selected -= 1
            else:
                if len(search_results) > 0:
                    render_result(stdscr, search_results[selected])
                else:
                    curses.beep()
        elif character == "\n":
            if len(search_results) > 0:
                render_result(stdscr, search_results[selected])
            else:
                curses.beep()
        elif ascii.isalnum(character) or ascii.isspace(character):
            search_input += character
            search_results = search(search_input)
            selected = 0
        render_static(stdscr, search_input, search_results, selected)
예제 #10
0
def IsAlphanum(code):
    return IsGBKAlphanum(code) or ascii.isalnum(code)
예제 #11
0
def IsASCIIAlphanum(code):
    return ascii.isalnum(code)
예제 #12
0
 def __check_alphanumer(self, data):
     for letter in data:
         if isalnum(letter):
             return True
     return False
예제 #13
0
    def getchar(self, character) -> None:
        """Process character"""
        if character == -1:
            return
        if self.is_bool:
            if character == 32:  # space
                self.toggle_state()
                self.get_focus()
                return
            try:
                if chr(character) in "XxYy1":
                    self.set_state(True)
                    self.get_focus()
                    return
                if chr(character) in "0nN":
                    self.set_state(False)
                    self.get_focus()
                    return
            except ValueError:
                pass
        else:
            if curses.keyname(character) == b"^V":
                clip = self.get_clipboard()
                if clip and len(self.textfield) < self.max_length:
                    self.textfield = (
                        f"{self.textfield[:self.cursor_position]}"
                        f"{clip}"
                        f"{self.textfield[self.cursor_position:]}")
                    self.cursor_position += len(clip)
                    if len(self.textfield) > self.max_length:
                        self.textfield = self.textfield[:self.max_length]
                    if self.cursor_position > self.max_length:
                        self.cursor_position = self.max_length

            if character == curses.KEY_LEFT:
                self.cursor_position -= 1
                self.cursor_position = max(self.cursor_position, 0)
            if character == curses.KEY_RIGHT:
                self.cursor_position += 1
                if self.cursor_position > len(self.textfield):
                    self.cursor_position = len(self.textfield)
            if character == curses.KEY_BACKSPACE:
                if self.cursor_position > 0:
                    self.textfield = (
                        self.textfield[:self.cursor_position - 1] +
                        self.textfield[self.cursor_position:])
                    self.cursor_position -= 1
                    self.cursor_position = max(self.cursor_position, 0)
            if character == curses.KEY_DC:
                self.textfield = (self.textfield[:self.cursor_position] +
                                  self.textfield[self.cursor_position + 1:])
            if isalnum(character) or character == ord("."):
                if len(self.textfield) < self.max_length:
                    self.textfield = (
                        f"{self.textfield[:self.cursor_position]}"
                        f"{chr(character)}"
                        f"{self.textfield[self.cursor_position:]}")
                    if not self.allow_lowercase:
                        self.textfield = self.textfield.upper()
                    self.cursor_position += 1
            self.screen.addstr(self.position_y, self.position_x,
                               " " * self.max_length, self.attribute)
            self.screen.addstr(self.position_y, self.position_x,
                               self.textfield, self.attribute)
            self._movecursor()
예제 #14
0
def client(stdscr, input=""):
    global server_ip
    prev_input = ""
    redraw = None
    selected_result = None

    stdscr.clear()
    draw_menu(stdscr, "Connect to agent")
    if connection:
        connected(stdscr)
        stdscr.refresh()
    else:
        centered(stdscr, HEIGHT // 2, "Fill in the IP of the agent/server:")
        addstr(stdscr, HEIGHT // 2 + 2, WIDTH // 2 - 8, "_" * 15, curses.A_DIM)
        addstr(stdscr, HEIGHT // 2 + 2, WIDTH // 2 - 8, input[:15])
        curses.curs_set(2)
        stdscr.refresh()

        stdscr.nodelay(True)

        while True:
            if input != prev_input or (redraw and (redraw is True or redraw.is_set())):
                if input != prev_input:
                    prev_input = input
                else:
                    redraw = None

                stdscr.clear()
                draw_menu(stdscr, "Connect to agent")
                centered(stdscr, HEIGHT // 2, "Fill in the IP of the agent/server:")

                addstr(stdscr, HEIGHT // 2 + 2, WIDTH // 2 - 8, "_" * 15, curses.A_DIM)
                server_ip = input
                addstr(stdscr, HEIGHT // 2 + 2, WIDTH // 2 - 8, server_ip[:15])

                stdscr.refresh()

            ev = stdscr.getch()
            if ev == SP:
                selected_result = None
                input += " "
            elif ev == curses.KEY_RESIZE:
                set_dimensions(stdscr)
            elif ev in (BS, DEL, curses.KEY_BACKSPACE):
                if selected_result:
                    redraw = True
                    selected_result = None
                else:
                    input = input[:-1]
            # On ESC go back to home menu
            elif ev == ESC:
                stdscr.nodelay(False)
                curses.curs_set(0)
                return home(stdscr)
            # On Enter connect to server socket with given IP
            elif ev == ord("\n"):
                stdscr.nodelay(False)
                curses.curs_set(0)
                connect(stdscr, server_ip)
            elif isalnum(ev) or ev in (ord(","), ord("."), ord("-")):
                selected_result = None
                input += chr(ev)

            time.sleep(0.01)