示例#1
0
 def setUp(self):
     self.sentence_repository = SentenceRepository("test.txt")
     if len(self.sentence_repository.get_all()) == 0:
         self.sentence_repository.add_sentence(
             Sentence(['test', 'abc', 'defg']))
         self.sentence_repository.add_sentence(
             Sentence(['Test', '123', 'hgfb']))
示例#2
0
    def read_file(self):
        """
        Reads all the sentences from a txt file, each sentence on a line, words separated by spaces
        :return: -
        :exception: IOError, EOFError
        """
        f = None

        try:
            f = open(self.__filename, 'r')
            line = f.readline()

            while line != '':
                line = line.strip('\n').split(' ')
                sentence = Sentence(line[:])
                self.add_sentence(sentence)
                line = f.readline()

        except IOError as ie:
            return ie
        except EOFError as ee:
            return ee

        finally:
            if f:
                f.close()
示例#3
0
 def add_sentence(self, sentence_string):
     """
     Receives a string, converts it into a Sentence object, validates it and then adds it to the repository
     :param sentence_string: The string representation of the sentence
     :return: The sentence
     :exceptions: ValueError if the sentence is not valid or if the sentence is already stored
     """
     sent = sentence_string.split(' ')
     sentence = Sentence(sent)
     self.__sentence_validator.validate(sentence)
     self.__sentence_repository.add_sentence(sentence)
     return sentence
示例#4
0
    def _loadFromFile(self):
        '''
        Loads the sentences from the file.
        '''

        file = open('input.txt', 'r')
        text = file.readline()
        while text != '':
            s = Sentence(text.strip())
            self._objects.append(s)
            text = file.readline()
        file.close()
 def testSentence(self):
     s = Sentence('Lalala')
     assert s.getScore() == 6
     
     s = Sentence('This is a sentence')
     assert s.getScore() == 15
     
     assert s.sentence == [['T','h','i','s'],['i','s'],['a'],['s','e','n','t','e','n','c','e']]
示例#6
0
    def groupSentence(self):
        """Divide words for terms and then for sentences"""
        sentenceStart = True
        currentSentence = None
        for x in self.resListSplitWithDots:
            if sentenceStart:
                currentSentence = Sentence()
                sentenceStart = False
            if x[-1] in self.end_of_sentence_characters:
                x = x[:-1]
                sentenceStart = True
            while len(x) >= 1 and x[0] in self.special_characters:
                x = x[1:]
            while len(x) >= 1 and x[-1] in self.special_characters:
                x = x[:-1]
            currentTerm = Term(x, currentSentence)
            currentSentence.termList.append(currentTerm)
            self.termList.append(currentTerm)

            # filling wordTermDict
            if self.wordTermDict.get(currentTerm.word) is None:
                self.wordTermDict[currentTerm.word] = []
            self.wordTermDict[currentTerm.word].append(currentTerm)
示例#7
0
 def test_printSentence(self):
     self.assertEqual(
         self.__sentence_controller.get_hidden_characters_form_of_sentence(
             Sentence(['Diana', 'este', 'frumoasa']), 'Ditre'),
         'Di___ e_te _r______ ')
示例#8
0
 def test_getCharsInSentence(self):
     self.assertEqual(
         self.__sentence_controller.get_characters_in_sentence(
             Sentence(['nu', 'mai', 'vreau', 'sa', 'scriu', 'teste'])),
         'numaivresct')