Beispiel #1
0
    def from_file(cls, filepath, synonyms_filepaths, stemmer, debug=False):
        """
            filepath: string or list
                single filepath or list of filepaths.
        """
        assert (isinstance(filepath, unicode) or isinstance(filepath, list))
        assert (isinstance(stemmer, Stemmer))

        if isinstance(synonyms_filepaths, unicode):
            synonyms = SynonymsCollection.from_file(synonyms_filepaths,
                                                    stemmer=stemmer,
                                                    debug=debug)
        elif isinstance(synonyms_filepaths, list):
            synonyms = SynonymsCollection.from_files(synonyms_filepaths,
                                                     stemmer=stemmer,
                                                     debug=debug)
        else:
            raise Exception("Unsupported type for 'synonyms_filepaths'")

        opinions = []
        filepaths = []
        if (isinstance(filepath, unicode)):
            filepaths.append(filepath)
        elif (isinstance(filepath, unicode)):
            filepaths = filepath

        for fp in filepaths:
            with io.open(fp, "r", encoding='utf-8') as f:
                for i, line in enumerate(f.readlines()):

                    if line == '\n':
                        continue

                    args = line.strip().split(',')

                    if len(args) < 3:
                        print "should be at least 3 arguments: {}, '{}'".format(
                            i, line.encode('utf-8'))
                        continue

                    entity_left = args[0].strip()
                    entity_right = args[1].strip()
                    sentiment = Label.from_str(args[2].strip())

                    o = Opinion(entity_left, entity_right, sentiment, stemmer)
                    opinions.append(o)

        return cls(opinions, synonyms, stemmer, debug)
    def from_file(cls, filepath, synonyms):
        assert (isinstance(synonyms, SynonymsCollection))

        opinions = []
        with io.open(filepath, "r", encoding='utf-8') as f:
            for i, line in enumerate(f.readlines()):

                if line == '\n':
                    continue

                args = line.strip().split(',')
                assert (len(args) >= 3)

                entity_left = args[0].strip()
                entity_right = args[1].strip()
                sentiment = Label.from_str(args[2].strip())

                o = Opinion(entity_left, entity_right, sentiment)
                opinions.append(o)

        return cls(opinions, synonyms)
Beispiel #3
0
    def from_file(cls, filepath):
        """ Read the vectors from *.vectors.txt file
        """
        vectors = {}
        with io.open(filepath, 'r', encoding='utf-8') as f:
            for line in f.readlines():
                args = line.split(',')

                opinion_value_left = args[0].strip()
                opinion_value_right = args[1].strip()
                label = Label.from_str(args[len(args) - 1].strip())
                vector = np.array(
                    [float(args[i]) for i in range(2,
                                                   len(args) - 1)])

                key = cls.___create_key(opinion_value_left,
                                        opinion_value_right)

                vectors[key] = OpinionVector(opinion_value_left,
                                             opinion_value_right, vector,
                                             label)

        return cls(vectors)
 def __frame_polarity_from_args(args):
     return FramePolarity(src=args[0],
                          dest=args[1],
                          label=Label.from_str(args[2]),
                          prob=args[3])