<s> import os <EOL> from os import path <EOL> import sys <EOL> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> import json <EOL> from . detector import Detector <EOL> from . lang_detect_exception import ErrorCode , LangDetectException <EOL> from . utils . lang_profile import LangProfile <EOL> class DetectorFactory ( object ) : <EOL> '''<STR_LIT>''' <EOL> seed = None <EOL> def __init__ ( self ) : <EOL> self . word_lang_prob_map = { } <EOL> self . langlist = [ ] <EOL> def load_profile ( self , profile_directory ) : <EOL> list_files = os . listdir ( profile_directory ) <EOL> if not list_files : <EOL> raise LangDetectException ( ErrorCode . NeedLoadProfileError , '<STR_LIT>' + profile_directory ) <EOL> langsize , index = len ( list_files ) , <NUM_LIT:0> <EOL> for filename in list_files : <EOL> if filename . startswith ( '<STR_LIT:.>' ) : <EOL> continue <EOL> filename = path . join ( profile_directory , filename ) <EOL> if not path . isfile ( filename ) : <EOL> continue <EOL> f = None <EOL> try : <EOL> if sys . version_info [ <NUM_LIT:0> ] < <NUM_LIT:3> : <EOL> f = open ( filename , '<STR_LIT:r>' ) <EOL> else : <EOL> f = open ( filename , '<STR_LIT:r>' , encoding = '<STR_LIT:utf-8>' ) <EOL> json_data = json . load ( f ) <EOL> profile = LangProfile ( ** json_data ) <EOL> self . add_profile ( profile , index , langsize ) <EOL> index += <NUM_LIT:1> <EOL> except IOError : <EOL> raise LangDetectException ( ErrorCode . FileLoadError , '<STR_LIT>' % filename ) <EOL> except : <EOL> raise LangDetectException ( ErrorCode . FormatError , '<STR_LIT>' % filename ) <EOL> finally : <EOL> if f : <EOL> f . close ( ) <EOL> def load_json_profile ( self , json_profiles ) : <EOL> langsize , index = len ( json_profiles ) , <NUM_LIT:0> <EOL> if langsize < <NUM_LIT:2> : <EOL> raise LangDetectException ( ErrorCode . NeedLoadProfileError , '<STR_LIT>' ) <EOL> for json_profile in json_profiles : <EOL> try : <EOL> json_data = json . loads ( json_profile ) <EOL> profile = LangProfile ( ** json_data ) <EOL> self . add_profile ( profile , index , langsize ) <EOL> index += <NUM_LIT:1> <EOL> except : <EOL> raise LangDetectException ( ErrorCode . FormatError , '<STR_LIT>' ) <EOL> def add_profile ( self , profile , index , langsize ) : <EOL> lang = profile . name <EOL> if lang in self . langlist : <EOL> raise LangDetectException ( ErrorCode . DuplicateLangError , '<STR_LIT>' ) <EOL> self . langlist . append ( lang ) <EOL> for word in profile . freq : <EOL> if word not in self . word_lang_prob_map : <EOL> self . word_lang_prob_map [ word ] = [ <NUM_LIT:0.0> ] * langsize <EOL> length = len ( word ) <EOL> if <NUM_LIT:1> <= length <= <NUM_LIT:3> : <EOL> prob = <NUM_LIT:1.0> * profile . freq . get ( word ) / profile . n_words [ length - <NUM_LIT:1> ] <EOL> self . word_lang_prob_map [ word ] [ index ] = prob <EOL> def clear ( self ) : <EOL> self . langlist = [ ] <EOL> self . word_lang_prob_map = { } <EOL> def create ( self , alpha = None ) : <EOL> '''<STR_LIT>''' <EOL> detector = self . _create_detector ( ) <EOL> if alpha is not None : <EOL> detector . set_alpha ( alpha ) <EOL> return detector <EOL> def _create_detector ( self ) : <EOL> if not self . langlist : <EOL> raise LangDetectException ( ErrorCode . NeedLoadProfileError , '<STR_LIT>' ) <EOL> return Detector ( self ) <EOL> def set_seed ( self , seed ) : <EOL> self . seed = seed <EOL> def get_lang_list ( self ) : <EOL> return list ( self . langlist ) <EOL> PROFILES_DIRECTORY = path . join ( path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> _factory = None <EOL> def init_factory ( ) : <EOL> global _factory <EOL> if _factory is None : <EOL> _factory = DetectorFactory ( ) <EOL> _factory . load_profile ( PROFILES_DIRECTORY ) <EOL> def detect ( text ) : <EOL> init_factory ( ) <EOL> detector = _factory . create ( ) <EOL> detector . append ( text ) <EOL> return detector . detect ( ) <EOL> def detect_langs ( text ) : <EOL> init_factory ( ) <EOL> detector = _factory . create ( ) <EOL> detector . append ( text ) <EOL> return detector . get_probabilities ( ) </s>
