text
stringlengths
0
828
3780,"def template(self, resources):
"""""" Get the template from: YAML, hierarchy, or class """"""
template_name = self.acquire(resources, 'template')
if template_name:
return template_name
else:
# We're putting an exception for ""resource"", the built-in
# rtype/directive. We want it to work out-of-the-box without
# requiring an _templates/resource.html in the docs project.
# Instead, use the page.html the ships with Sphinx.
if self.rtype == 'resource':
return 'page'
else:
return self.rtype"
3781,"def find_prop_item(self, prop_name, prop_key, prop_value):
"""""" Look for a list prop with an item where key == value """"""
# Image props are a sequence of dicts. We often need one of them.
# Where one of the items has a dict key matching a value, and if
# nothing matches, return None
prop = getattr(self.props, prop_name, None)
if prop:
return next(
(p for p in prop
if getattr(p, prop_key) == prop_value),
None
)
return None"
3782,"def default() :
"""""" Get default shaman instance by ""data/trained.json""
""""""
if Shaman._default_instance is not None :
return Shaman._default_instance
with open((os.path.dirname(__file__) or '.') + '/data/trained.json') as file :
tset = json.loads(file.read())
Shaman._default_instance = Shaman(tset)
return Shaman._default_instance"
3783,"def detect(self, code) :
"""""" Detect language with code
""""""
keywords = KeywordFetcher.fetch( code )
probabilities = {}
for keyword in keywords :
if keyword not in self.trained_set['keywords'] :
continue
data = self.trained_set['keywords'][keyword]
p_avg = sum(data.values()) / len(data) # Average probability of all languages
for language, probability in data.items() :
# By Naïve Bayes Classification
p = probability / p_avg
probabilities[ language ] = probabilities.get(language, 0) + math.log(1 + p)
for pattern, data in self.trained_set['patterns'].items() :
matcher = PatternMatcher(pattern)
p0 = matcher.getratio(code)
for language, p_avg in data.items() :
if language not in probabilities :
continue
p = 1 - abs(p_avg - p0)
probabilities[ language ] *= p
# Convert `log` operated probability to percentile
sum_val = 0
for language, p in probabilities.items() :
sum_val += math.pow(math.e / 2, p)
for language, p in probabilities.items() :
probabilities[language] = math.pow(math.e / 2, p) / sum_val * 100
return sorted(probabilities.items(), key=lambda a: a[1], reverse=True)"
3784,"def fetch(code) :
"""""" Fetch keywords by Code
""""""
ret = {}
code = KeywordFetcher._remove_strings(code)
result = KeywordFetcher.prog.findall(code)
for keyword in result :
if len(keyword) <= 1: continue # Ignore single-length word
if keyword.isdigit(): continue # Ignore number
if keyword[0] == '-' or keyword[0] == '*' : keyword = keyword[1:] # Remove first char if string is starting by '-' or '*' (Pointer or Negative numbers)
if keyword[-1] == '-' or keyword[-1] == '*' : keyword = keyword[0:-1] # Remove last char if string is finished by '-' or '*'
if len(keyword) <= 1: continue