def post(self): content = self.request.get('content') for line in content.split('\n'): spelling, meaning = line.split(',') word = Word() word.spelling = spelling word.meaning = meaning word.put() self.redirect('/words')
def get(self): i = 0 for wordlist in wordlists: wl = WordList(name="wordlist%02d" % i, owner=users.get_current_user()) wl.put() for spelling, meaning in wordlist: w = Word(spelling=spelling, meaning=meaning) w.put() m = WordMember(wordlist=wl, word=w) m.put() i += 1 self.redirect('/words')
def translate(self, value): value = value.strip().lower() word = Word.query.filter_by(word=value).first() if word: word.power += 1 db.session.commit() return {'success': True, 'model': word} response = self.request(value) if response.status_code != 200: return {'success': False, 'message': response.json()['message']} word = Word() word.word = value word.definitions = response.json()['definitions'] db.session.add(word) db.session.commit() return {'success': True, 'model': word}
def add_words(self, sentence, parsed_sentence, raw_text): """Given a Sentence and its parsed text, and find the PoS, lemmas, and space_befores of each word in the sentence, and add them to the Sentence object. """ words = dict() position = 0 space = re.compile(r'\s') cr = re.compile(r'[\n\r]') for word_data in parsed_sentence["words"]: surface = word_data[0] part_of_speech = word_data[1]["PartOfSpeech"] try: lemma = word_data[1]["Lemma"].lower() except AttributeError as err: # this word wasn't recognized as a word by the parser, # it's probably a weird character or something lemma = "*" * (int(word_data[1]["CharacterOffsetEnd"]) - int(word_data[1]["CharacterOffsetBegin"])) surface = "*" * (int(word_data[1]["CharacterOffsetEnd"]) - int(word_data[1]["CharacterOffsetBegin"])) space_before = "" try: prevChar = raw_text[int(word_data[1]["CharacterOffsetBegin"]) - 1] if space.match(prevChar): if cr.match(prevChar): space_before = "\n" else: space_before = " " except IndexError: pass key = (surface.lower(), part_of_speech, lemma) if key in words: word = words[key] else: try: word = Word.query.filter_by(lemma=lemma, surface=surface.lower(), part_of_speech=part_of_speech).one() except MultipleResultsFound: project_logger.warning("Duplicate records found for: %s", str(key)) except NoResultFound: word = Word(lemma=lemma, surface=surface.lower(), part_of_speech=part_of_speech) word.save(False) words[key] = word sentence.add_word( word=word, position=position, space_before=space_before, surface=surface, project=self.project, force=False ) position += 1 db.session.commit()
def add_words(self, sentence, parsed_sentence, raw_text): """Given a Sentence and its parsed text, and find the PoS, lemmas, and space_befores of each word in the sentence, and add them to the Sentence object. """ words = dict() position = 0 space = re.compile(r'\s') cr = re.compile(r'[\n\r]') for word_data in parsed_sentence["words"]: surface = word_data[0] part_of_speech = word_data[1]["PartOfSpeech"] try: lemma = word_data[1]["Lemma"].lower() except AttributeError as err: # this word wasn't recognized as a word by the parser, # it's probably a weird character or something lemma = "*" * (int(word_data[1]["CharacterOffsetEnd"]) - int(word_data[1]["CharacterOffsetBegin"])) surface = "*" * (int(word_data[1]["CharacterOffsetEnd"]) - int(word_data[1]["CharacterOffsetBegin"])) space_before = "" try: prevChar = raw_text[int(word_data[1]["CharacterOffsetBegin"]) - 1] if space.match(prevChar): if cr.match(prevChar): space_before = "\n" else: space_before = " " except IndexError: pass key = (surface.lower(), part_of_speech, lemma) if key in words: word = words[key] else: try: word = Word.query.filter_by( lemma=lemma, surface=surface.lower(), part_of_speech=part_of_speech).one() except MultipleResultsFound: project_logger.warning("Duplicate records found for: %s", str(key)) except NoResultFound: word = Word(lemma=lemma, surface=surface.lower(), part_of_speech=part_of_speech) word.save(False) words[key] = word sentence.add_word(word=word, position=position, space_before=space_before, surface=surface, project=self.project, force=False) position += 1 db.session.commit()
"definition": "the quantity contained in a bottle", "partOfSpeech": "noun" }, { "definition": "a vessel fitted with a flexible teat and filled with milk or formula; used as a substitute for breast feeding infants and very young children", "partOfSpeech": "noun" }, { "definition": "a glass or plastic vessel used for storing drinks or other liquids; typically cylindrical without handles and with a narrow neck that can be plugged or capped", "partOfSpeech": "noun" }, { "definition": "put into bottles", "partOfSpeech": "verb" }, { "definition": "store (liquids or gases) in bottles", "partOfSpeech": "verb" }] word_one = Word() word_one.word = "flask" word_one.definitions = definition_one word_two = Word() word_two.word = "bottle" word_two.definitions = definition_two db.session.add(word_one) db.session.add(word_two) db.session.commit()