natural language toolkit opinion lexicon corpus reader c 20012023 nltk project pierpaolo pantone 24alsecondogmail com url https www nltk org for license information see license txt corpusreader for the opinion lexicon opinion lexicon information s minqing hu and bing liu 2004 department of computer science university of illinois at chicago contact bing liu liubcs uic edu https www cs uic eduliub distributed with permission related papers minqing hu and bing liu mining and summarizing customer reviews proceedings of the acm sigkdd international conference on knowledge discovery data mining kdd04 aug 2225 2004 seattle washington usa bing liu minqing hu and junsheng cheng opinion observer analyzing and comparing opinions on the web proceedings of the 14th international world wide web conference www2005 may 1014 2005 chiba japan this corpusview is used to skip the initial readme block of the corpus open self stream skip the readme block set the initial position to the current stream position reader for liu and hu opinion lexicon blank lines and readme are ignored from nltk corpus import opinionlexicon opinionlexicon words 2faced 2faces abnormal abolish the opinionlexiconcorpusreader provides shortcuts to retrieve positivenegative words opinionlexicon negative 2faced 2faces abnormal abolish note that words from words method are sorted by file id not alphabetically opinionlexicon words0 10 doctest normalizewhitespace 2faced 2faces abnormal abolish abominable abominably abominate abomination abort aborted sortedopinionlexicon words0 10 doctest normalizewhitespace 2faced 2faces a abnormal abolish abominable abominably abominate abomination abort return all words in the opinion lexicon note that these words are not sorted in alphabetical order param fileids a list or regexp specifying the ids of the files whose words have to be returned return the given files as a list of words and punctuation symbols rtype liststr return all positive words in alphabetical order return a list of positive words rtype liststr return all negative words in alphabetical order return a list of negative words rtype liststr natural language toolkit opinion lexicon corpus reader c 2001 2023 nltk project pierpaolo pantone 24alsecondo gmail com url https www nltk org for license information see license txt corpusreader for the opinion lexicon opinion lexicon information s minqing hu and bing liu 2004 department of computer science university of illinois at chicago contact bing liu liub cs uic edu https www cs uic edu liub distributed with permission related papers minqing hu and bing liu mining and summarizing customer reviews proceedings of the acm sigkdd international conference on knowledge discovery data mining kdd 04 aug 22 25 2004 seattle washington usa bing liu minqing hu and junsheng cheng opinion observer analyzing and comparing opinions on the web proceedings of the 14th international world wide web conference www 2005 may 10 14 2005 chiba japan this corpusview is used to skip the initial readme block of the corpus open self _stream skip the readme block set the initial position to the current stream position reader for liu and hu opinion lexicon blank lines and readme are ignored from nltk corpus import opinion_lexicon opinion_lexicon words 2 faced 2 faces abnormal abolish the opinionlexiconcorpusreader provides shortcuts to retrieve positive negative words opinion_lexicon negative 2 faced 2 faces abnormal abolish note that words from words method are sorted by file id not alphabetically opinion_lexicon words 0 10 doctest normalize_whitespace 2 faced 2 faces abnormal abolish abominable abominably abominate abomination abort aborted sorted opinion_lexicon words 0 10 doctest normalize_whitespace 2 faced 2 faces a abnormal abolish abominable abominably abominate abomination abort return all words in the opinion lexicon note that these words are not sorted in alphabetical order param fileids a list or regexp specifying the ids of the files whose words have to be returned return the given file s as a list of words and punctuation symbols rtype list str return all positive words in alphabetical order return a list of positive words rtype list str return all negative words in alphabetical order return a list of negative words rtype list str read 20 lines at a time
from nltk.corpus.reader import WordListCorpusReader
from nltk.corpus.reader.api import *


class IgnoreReadmeCorpusView(StreamBackedCorpusView):
    

    def __init__(self, *args, **kwargs):
        StreamBackedCorpusView.__init__(self, *args, **kwargs)
        
        self._open()
        
        read_blankline_block(self._stream)
        
        self._filepos = [self._stream.tell()]


class OpinionLexiconCorpusReader(WordListCorpusReader):
    

    CorpusView = IgnoreReadmeCorpusView

    def words(self, fileids=None):
        
        if fileids is None:
            fileids = self._fileids
        elif isinstance(fileids, str):
            fileids = [fileids]
        return concat(
            [
                self.CorpusView(path, self._read_word_block, encoding=enc)
                for (path, enc, fileid) in self.abspaths(fileids, True, True)
            ]
        )

    def positive(self):
        
        return self.words("positive-words.txt")

    def negative(self):
        
        return self.words("negative-words.txt")

    def _read_word_block(self, stream):
        words = []
        for i in range(20):  
            line = stream.readline()
            if not line:
                continue
            words.append(line.strip())
        return words