Ejemplo n.º 1
0
    def run(self, input):
        language = self.options['language']
        lexicon_name = self.options['lexicon']
        delimiter = str(self.options['delimiter'])
        lex = get_or_create(SentimentLexicon,
                            language=language,
                            label=lexicon_name)

        if not self.options['noheader']:
            input.readline()

        for lemma, pos, value in csv.reader(input, delimiter=delimiter):
            pos = pos_map.get(pos.upper(), pos.upper())
            if pos is None: continue
            lemma = lemma.decode('latin-1')

            sent = sent_map.get(value.lower(), 0)
            intense = intense_map.get(value.lower(), 0)

            print lemma, pos, sent, intense

            if sent or intense:

                l = get_or_create(Lemma,
                                  language=language,
                                  pos=pos,
                                  lemma=lemma)

                SentimentLemma.objects.create(lexicon=lex,
                                              lemma=l,
                                              sentiment=sent,
                                              intensifier=intense)
Ejemplo n.º 2
0
    def run(self, input):
	language = self.options['language']
	lexicon_name = self.options['lexicon']
	delimiter = str(self.options['delimiter'])
	lex = get_or_create(SentimentLexicon, language=language, label=lexicon_name)

	if not self.options['noheader']:
	    input.readline()
	
	for lemma, pos, value in csv.reader(input, delimiter=delimiter):
	    pos = pos_map.get(pos.upper(), pos.upper())
	    if pos is None: continue
	    lemma = lemma.decode('latin-1')

	    sent = sent_map.get(value.lower(), 0)
	    intense = intense_map.get(value.lower(), 0)

	    print lemma, pos, sent, intense
	    
	    if sent or intense:
	    
		l = get_or_create(Lemma, language=language,
				  pos=pos, lemma=lemma)

		SentimentLemma.objects.create(lexicon=lex, lemma=l,
					      sentiment=sent, intensifier=intense)
Ejemplo n.º 3
0
 def test_get_or_create(self):
     """Test the get or create operation"""
     from amcat.models.medium import Medium
     name = "dsafdsafdsafDSA_amcat_test_medium"
     Medium.objects.filter(name=name).delete()
     self.assertRaises(Medium.DoesNotExist, Medium.objects.get, name=name)
     m = get_or_create(Medium, name=name)
     self.assertEqual(m.name, name)
     m2 = get_or_create(Medium, name=name)
     self.assertEqual(m, m2)
Ejemplo n.º 4
0
 def test_get_or_create(self):
     """Test the get or create operation"""
     from amcat.models.medium import Medium
     name = "dsafdsafdsafDSA_amcat_test_medium"
     Medium.objects.filter(name=name).delete()
     self.assertRaises(Medium.DoesNotExist, Medium.objects.get, name=name)
     m = get_or_create(Medium, name=name)
     self.assertEqual(m.name, name)
     m2 = get_or_create(Medium, name=name)
     self.assertEqual(m, m2)
Ejemplo n.º 5
0
 def setUp(self):
     self.codes = [
         # (label, value, parent)
         ("root", "root r", None),
         ("code1", "Code One", "root"),
         (u'\u5728\u8377\u5170\u98ce\u8f66', u'\xda\xd1\xcd\xa2\xd3\xd0\xc9 \xde\xc9X\xde', "root"),
         ("codes", "Code Collection", "root"),
         ("one", "eins OR een", "codes"),
         ("two", "zwei AND deux OR twee", "codes"),
         ("three", "drei OR drie AND trois", "two")
     ]
     self.l_lang = djangotoolkit.get_or_create(Language, label='l_lang')
     self.r_lang = djangotoolkit.get_or_create(Language, label='r_lang')
     self.codebook = self._get_test_codebook(self.codes, self.l_lang, self.r_lang)
Ejemplo n.º 6
0
 def setUp(self):
     self.codes = [
         # (label, value, parent)
         ("root", "root r", None),
         ("code1", "Code One", "root"),
         (u'\u5728\u8377\u5170\u98ce\u8f66',
          u'\xda\xd1\xcd\xa2\xd3\xd0\xc9 \xde\xc9X\xde', "root"),
         ("codes", "Code Collection", "root"),
         ("one", "eins OR een", "codes"),
         ("two", "zwei AND deux OR twee", "codes"),
         ("three", "drei OR drie AND trois", "two")
     ]
     self.l_lang = djangotoolkit.get_or_create(Language, label='l_lang')
     self.r_lang = djangotoolkit.get_or_create(Language, label='r_lang')
     self.codebook = self._get_test_codebook(self.codes, self.l_lang,
                                             self.r_lang)
Ejemplo n.º 7
0
def get_article(e):
    headline = get_headline(e)
    body = get_body(e)
    medium, date, page = get_meta(e)
    section = get_section(e)
    medium = get_or_create(Medium, name=medium)

    return Article(headline=headline, text=body, date=date, pagenr=page, section=section, medium=medium)
Ejemplo n.º 8
0
def get_or_create_medium(medium_name):
    """
    Finds a medium object or creates a new one if not found
    @type medium_name: unicode
    @return: a Medium object (or None if medium_name was None)
    """
    if medium_name is None: return None
    return get_or_create(Medium, name=medium_name)
Ejemplo n.º 9
0
    def _validate_form(self):
        # If affiliation is given as a string, get or create the affiliation
        aff = self.bound_form.data['affiliation']
        if isinstance(aff, basestring):
            self.bound_form.data['affiliation'] = get_or_create(Affiliation, name=aff).id
        role = self.bound_form.data['role']
        if isinstance(role, basestring):
            self.bound_form.data['role'] = Role.objects.get(label=role, projectlevel=False).id

        super(AddUser, self)._validate_form()
Ejemplo n.º 10
0
def get_or_create_articleset(name, project):
    """
    Finds an articleset based on its name. If it does not exists, it creates
    one and returns that one instead.

    @type name: unicode
    @param name: name attribute of ArticleSet
    @type project: project.Project
    @param project: project attribute of ArticleSet
    @return: ArticleSet object or None if name is None
    """
    return get_or_create(ArticleSet, name=name,
                         project=project) if name else None
Ejemplo n.º 11
0
def get_article(e):
    headline = get_headline(e)
    body = get_body(e)
    medium, date, page = get_meta(e)
    section = get_section(e)
    medium = get_or_create(Medium, name=medium)

    return Article(headline=headline,
                   text=body,
                   date=date,
                   pagenr=page,
                   section=section,
                   medium=medium)
Ejemplo n.º 12
0
    def body_to_article(self, headline, byline, text, date, source, meta):
        """
        Create an Article-object based on given parameters. It raises an
        error (Medium.DoesNotExist) when the given source does not have
        an entry in the database.

        @param headline: headline of new Article-object
        @type headline: unicode / str

        @param byline: byline for new Article
        @type byline: NoneType, unicode, str

        @param text: text for new Article
        @type text: unicode / str

        @param date: date(time) for new Article
        @type date: datetime.date, datetime.datetime

        @param source: medium-label for new Article
        @type source: unicode / str

        @param meta: object containing all sorts of meta-information, most of
                     it suitable for metastring. However, some information
                     (author, length) will be extracted.
        @type meta: dictionary

        @return Article-object

        """
        log.debug(
            "Creating article object for {headline!r}".format(**locals()))

        art = Article(headline=headline, byline=byline, text=text, date=date)

        art.medium = get_or_create(Medium, name=source)

        # Author / Section
        meta = meta.copy()
        art.author = meta.pop('author', None)
        art.section = meta.pop('section', None)
        if 'length' in meta:
            art.length = int(meta.pop('length').split()[0])
        else:
            art.length = art.text.count(" ")
        art.metastring = str(meta)

        art.project = self.options['project']

        return art
Ejemplo n.º 13
0
def get_test_language(**kargs):
    from amcat.models.language import Language
    from amcat.tools import djangotoolkit
    return djangotoolkit.get_or_create(Language, label='en')
Ejemplo n.º 14
0
def get_test_role(**kargs):
    from amcat.models import Role
    from amcat.tools import djangotoolkit
    return djangotoolkit.get_or_create(Role, label='admin', projectlevel=False)
Ejemplo n.º 15
0
def get_test_language(**kargs):
    from amcat.models.language import Language
    from amcat.tools import djangotoolkit
    return djangotoolkit.get_or_create(Language, label='en')
Ejemplo n.º 16
0
def get_test_role(**kargs):
    from amcat.models import Role
    from amcat.tools import djangotoolkit
    return djangotoolkit.get_or_create(Role, label='admin', projectlevel=False)