mirror of
https://gitlab.com/Oreolek/improv_forest.git
synced 2024-06-16 23:20:58 +03:00
Скрипт вычисления частоты
This commit is contained in:
parent
642fd74bcf
commit
1d1ae715b5
63
distribution.coffee
Normal file
63
distribution.coffee
Normal file
|
@ -0,0 +1,63 @@
|
|||
# Improv distribution assessment
|
||||
# Currently only for files
|
||||
|
||||
CSON = require 'cson'
|
||||
glob = require 'glob'
|
||||
|
||||
distribution = {}
|
||||
template = (phrase) ->
|
||||
if phrase == undefined or phrase == ''
|
||||
return []
|
||||
[openBracket, closeBracket] = [phrase.indexOf('['), phrase.indexOf(']')]
|
||||
if openBracket == -1
|
||||
return phrase
|
||||
if closeBracket == -1
|
||||
throw new Error("Missing close bracket in phrase: #{phrase}")
|
||||
before = phrase.slice(0, openBracket)
|
||||
after = phrase.slice(closeBracket + 1)
|
||||
directive = phrase.substring(openBracket + 1, closeBracket)
|
||||
return [directive, after]
|
||||
|
||||
parseGroup = (group) ->
|
||||
groups = []
|
||||
for phrase in group.phrases
|
||||
nexttpl = null
|
||||
templates = []
|
||||
while nexttpl != undefined
|
||||
[nexttpl, after] = template (phrase)
|
||||
if nexttpl != undefined
|
||||
templates.push(nexttpl)
|
||||
phrase = after
|
||||
for tpl in templates
|
||||
directive = tpl.slice(0, 1)
|
||||
grp = tpl.substring(1, tpl.length)
|
||||
if groups.indexOf(grp) == -1
|
||||
groups.push grp
|
||||
if directive == ':'
|
||||
distribution[grp] ?= 0
|
||||
distribution[grp] += 1
|
||||
for filename in groups
|
||||
if spec[filename]?
|
||||
for groupdata in spec[filename].groups
|
||||
parseGroup(groupdata)
|
||||
|
||||
files = glob.sync('data/*.cson')
|
||||
spec = {}
|
||||
for file in files
|
||||
data = CSON.parseCSONFile(file)
|
||||
if not data.groups?
|
||||
data.groups = []
|
||||
if data.phrases?
|
||||
data.groups.push({
|
||||
tags: [],
|
||||
phrases: data.phrases
|
||||
})
|
||||
data.phrases = null
|
||||
key = file.substr(0, file.lastIndexOf('.')) || file
|
||||
key = key.replace('data/', '')
|
||||
spec[key] = data
|
||||
|
||||
for group in spec.description.groups
|
||||
parseGroup(group)
|
||||
|
||||
console.log distribution
|
Loading…
Reference in a new issue