コード例 #1
0
ファイル: __init__.py プロジェクト: RobertShayne/VNR-Extras
    def __init__(self,
                 lang=None,
                 text=None,
                 tokenize=None,
                 chunkers=None,
                 filters=None):
        """Constructor for the SpellChecker class.

        SpellChecker objects can be created in two ways, depending on
        the nature of the first argument.  If it is a string, it
        specifies a language tag from which a dictionary is created.
        Otherwise, it must be an enchant Dict object to be used.
        
        Optional keyword arguments are:
            
            * text:  to set the text to be checked at creation time
            * tokenize:  a custom tokenization function to use
            * chunkers:  a list of chunkers to apply during tokenization
            * filters:  a list of filters to apply during tokenization
        
        If <tokenize> is not given and the first argument is a Dict,
        its 'tag' attribute must be a language tag so that a tokenization
        function can be created automatically.  If this attribute is missing
        the user's default language will be used.
        """
        if lang is None:
            lang = get_default_language()
        if isinstance(lang, basestring):
            dict = enchant.Dict(lang)
        else:
            dict = lang
            try:
                lang = dict.tag
            except AttributeError:
                lang = get_default_language()
        if lang is None:
            raise DefaultLanguageNotFoundError
        self.lang = lang
        self.dict = dict
        if tokenize is None:
            try:
                tokenize = get_tokenizer(lang, chunkers, filters)
            except TokenizerNotFoundError:
                # Fall back to default tokenization if no match for 'lang'
                tokenize = get_tokenizer(None, chunkers, filters)
        self._tokenize = tokenize

        self.word = None
        self.wordpos = None
        self._ignore_words = {}
        self._replace_words = {}
        # Default to the empty string as the text to be checked
        self._text = array.array('u')
        self._use_tostring = False
        self._tokens = iter([])

        if text is not None:
            self.set_text(text)
コード例 #2
0
ファイル: __init__.py プロジェクト: CatCookie/DomainSearch
    def __init__(self,lang=None,text=None,tokenize=None,chunkers=None,filters=None):
        """Constructor for the SpellChecker class.

        SpellChecker objects can be created in two ways, depending on
        the nature of the first argument.  If it is a string, it
        specifies a language tag from which a dictionary is created.
        Otherwise, it must be an enchant Dict object to be used.
        
        Optional keyword arguments are:
            
            * text:  to set the text to be checked at creation time
            * tokenize:  a custom tokenization function to use
            * chunkers:  a list of chunkers to apply during tokenization
            * filters:  a list of filters to apply during tokenization
        
        If <tokenize> is not given and the first argument is a Dict,
        its 'tag' attribute must be a language tag so that a tokenization
        function can be created automatically.  If this attribute is missing
        the user's default language will be used.
        """
        if lang is None:
            lang = get_default_language()
        if isinstance(lang,basestring):
            dict = enchant.Dict(lang)
        else:
            dict = lang
            try:
                lang = dict.tag
            except AttributeError:
                lang = get_default_language()
        if lang is None:
            raise DefaultLanguageNotFoundError
        self.lang = lang
        self.dict = dict
        if tokenize is None:
            try:
                tokenize = get_tokenizer(lang,chunkers,filters)
            except TokenizerNotFoundError:
                # Fall back to default tokenization if no match for 'lang'
                tokenize = get_tokenizer(None,chunkers,filters)
        self._tokenize = tokenize
        
        self.word = None
        self.wordpos = None
        self._ignore_words = {}
        self._replace_words = {}
        # Default to the empty string as the text to be checked
        self._text = array.array('u')
        self._use_tostring = False
        self._tokens = iter([])
        
        if text is not None:
            self.set_text(text)
コード例 #3
0
ファイル: __init__.py プロジェクト: FlickDUB/pyenchant
    def __init__(self, tag=None, broker=None):
        """Dict object constructor.

        A dictionary belongs to a specific language, identified by the
        string <tag>.  If the tag is not given or is None, an attempt to
        determine the language currently in use is made using the 'locale'
        module.  If the current language cannot be determined, Error is raised.

        If <tag> is instead given the value of False, a 'dead' Dict object
        is created without any reference to a language.  This is typically
        only useful within PyEnchant itself.  Any other non-string value
        for <tag> raises Error.

        Each dictionary must also have an associated Broker object which
        obtains the dictionary information from the underlying system. This
        may be specified using <broker>.  If not given, the default broker
        is used.
        """
        # Initialise misc object attributes to None
        self.provider = None
        # If no tag was given, use the default language
        if tag is None:
            tag = get_default_language()
            if tag is None:
                err = "No tag specified and default language could not "
                err = err + "be determined."
                raise Error(err)
        self.tag = tag
        # If no broker was given, use the default broker
        if broker is None:
            broker = _broker
        self._broker = broker
        # Now let the superclass initialise the C-library object
        super().__init__()
コード例 #4
0
ファイル: __init__.py プロジェクト: DKaman/pyenchant
    def __init__(self,tag=None,broker=None):
        """Dict object constructor.
        
        A dictionary belongs to a specific language, identified by the
        string <tag>.  If the tag is not given or is None, an attempt to
        determine the language currently in use is made using the 'locale'
        module.  If the current language cannot be determined, Error is raised.

        If <tag> is instead given the value of False, a 'dead' Dict object
        is created without any reference to a language.  This is typically
        only useful within PyEnchant itself.  Any other non-string value
        for <tag> raises Error.
        
        Each dictionary must also have an associated Broker object which
        obtains the dictionary information from the underlying system. This
        may be specified using <broker>.  If not given, the default broker
        is used.
        """
        # Initialise misc object attributes to None
        self.provider = None
        # If no tag was given, use the default language
        if tag is None:
            tag = get_default_language()
            if tag is None:
                err = "No tag specified and default language could not "
                err = err + "be determined."
                raise Error(err)
        self.tag = tag
        # If no broker was given, use the default broker
        if broker is None:
            broker = _broker
        self._broker = broker
        # Now let the superclass initialise the C-library object
        _EnchantObject.__init__(self)
コード例 #5
0
ファイル: test_checker.py プロジェクト: nathanial/pyenchant
def test_default_language():
    lang = get_default_language()
    if lang is None:
        with pytest.raises(DefaultLanguageNotFoundError):
            SpellChecker()
    else:
        checker = SpellChecker()
        assert checker.lang == lang
コード例 #6
0
def test_DefaultLang(en_us_dict):
    """Test behaviour of default language selection."""
    defLang = get_default_language()
    if defLang is None:
        # If no default language, shouldn't work
        with pytest.raises(Error):
            Dict()
    else:
        # If there is a default language, should use it
        # Of course, no need for the dict to actually exist
        try:
            d = Dict()
            assert d.tag == defLang
        except DictNotFoundError:
            pass
コード例 #7
0
def test_default_language():
    # Two cases: either SpellChecker() without argument works
    # and its lang is the default language, or
    # it does not and we get a DefaultLanguageNotFoundError
    caught_err = None
    try:
        checker = SpellChecker()
    except DefaultLanguageNotFoundError as e:
        caught_err = e

    if caught_err:
        # At this point, caught_err must be DefaultLanguageNotFoundError, so
        # we're done testing
        return

    assert checker.lang == get_default_language()
コード例 #8
0
    def __init__(self, lang=None, *args, **kwargs):
        """
        Initialize a new CKEnchanter object
        """

        # Make sure the desired default dictionary exists
        # if it doesn't, get the default language
        if lang is None:
            lang = get_default_language()

        # If a word list was given, add it to the default dictionary
        if kwargs.get('wl_path', None):
            master_dict = enchant.DictWithPWL(lang, kwargs.pop('wl_path'))
        else:
            master_dict = lang

        # Finish creating a pyenchant spellchecker
        SpellChecker.__init__(self, master_dict, *args, **kwargs)
コード例 #9
0
ファイル: __init__.py プロジェクト: RobertShayne/VNR-Extras
    def __init__(self, tag=None, broker=None):
        """Dict object constructor.
        
        A dictionary belongs to a specific language, identified by the
        string <tag>.  If the tag is not given or is None, an attempt to
        determine the language currently in use is made using the 'locale'
        module.  If the current language cannot be determined, Error is raised.

        If <tag> is instead given the value of False, a 'dead' Dict object
        is created without any reference to a language.  This is typically
        only useful within PyEnchant itself.  Any other non-string value
        for <tag> raises Error.
        
        Each dictionary must also have an associated Broker object which
        obtains the dictionary information from the underlying system. This
        may be specified using <broker>.  If not given, the default broker
        is used.
        """
        # Superclass initialisation
        _EnchantObject.__init__(self)
        # Initialise object attributes to None
        self._broker = None
        self.tag = None
        self.provider = None
        # Create dead object if False was given
        if tag is False:
            self._this = None
        else:
            if tag is None:
                tag = get_default_language()
                if tag is None:
                    err = "No tag specified and default language could not "
                    err = err + "be determined."
                    raise Error(err)
            # Use module-level broker if none given
            if broker is None:
                broker = _broker
            # Use the broker to get C-library pointer data
            self._switch_this(broker._request_dict_data(tag), broker)