318 lines
14 KiB
Go
318 lines
14 KiB
Go
package need
|
||
|
||
import (
|
||
"fmt"
|
||
"regexp"
|
||
"sort"
|
||
"strings"
|
||
"unicode"
|
||
"unicode/utf8"
|
||
|
||
"lincodex/backend/internal/domain"
|
||
)
|
||
|
||
const needRulesVersion = "need-rules.2026-10.1"
|
||
|
||
var (
|
||
needSectionPattern = regexp.MustCompile(`(?i)(обязательно|обязательные навыки|требуемые навыки|требования|желательно|желательные навыки|предпочтительно|технологии|стек|required skills|requirements|preferred skills|technology stack)\s*:?\s*`)
|
||
needTaskPattern = regexp.MustCompile(`(?i)(задачи|обязанности|tasks|responsibilities)\s*:`)
|
||
needNegationPattern = regexp.MustCompile(`(?i)(?:без|without|no|not(?:\s+required)?|не(?:\s+(?:требуется|нужен|нужна|нужно|обязательно|обязателен|обязательна|обязательны|требуемый|требуемая))?)\s*[:,\-]?\s*$`)
|
||
needDomainPatterns = []domainRule{
|
||
{pattern: regexp.MustCompile(`(?i)(в команду платежей)`), value: "платежи"},
|
||
{pattern: regexp.MustCompile(`(?i)(в команду выплат)`), value: "выплаты"},
|
||
{pattern: regexp.MustCompile(`(?i)(payments team)`), value: "payments"},
|
||
{pattern: regexp.MustCompile(`(?i)(logistics team)`), value: "logistics"},
|
||
}
|
||
needSpecializationPatterns = []domainRule{
|
||
{pattern: regexp.MustCompile(`(?i)(?:^|[^\p{L}\p{N}_])((?:ищем\s+)?(?:backend[ -](?:developer|engineer)|back-end[ -](?:developer|engineer)|бэкенд[- ]разработчик\p{L}*|бэкэнд[- ]разработчик\p{L}*|серверн\p{L}*\s+разработчик\p{L}*))(?:$|[^\p{L}\p{N}_])`), value: string(domain.SpecializationBackend)},
|
||
{pattern: regexp.MustCompile(`(?i)(?:^|[^\p{L}\p{N}_])((?:ищем\s+)?(?:frontend[ -](?:developer|engineer)|front-end[ -](?:developer|engineer)|фронтенд[- ]разработчик\p{L}*|фронтэнд[- ]разработчик\p{L}*))(?:$|[^\p{L}\p{N}_])`), value: string(domain.SpecializationFrontend)},
|
||
{pattern: regexp.MustCompile(`(?i)(?:^|[^\p{L}\p{N}_])((?:ищем\s+)?(?:mobile[ -](?:developer|engineer)|мобильн\p{L}*\s+разработчик\p{L}*))(?:$|[^\p{L}\p{N}_])`), value: string(domain.SpecializationMobile)},
|
||
{pattern: regexp.MustCompile(`(?i)(?:^|[^\p{L}\p{N}_])(network engineer|сетев\p{L}* инженер\p{L}*)(?:$|[^\p{L}\p{N}_])`), value: string(domain.SpecializationNetworkEngineer)},
|
||
{pattern: regexp.MustCompile(`(?i)(?:^|[^\p{L}\p{N}_])(devops|sre)(?:$|[^\p{L}\p{N}_])`), value: string(domain.SpecializationDevopsSRE)},
|
||
{pattern: regexp.MustCompile(`(?i)(?:^|[^\p{L}\p{N}_])(data analyst|аналитик данных)(?:$|[^\p{L}\p{N}_])`), value: string(domain.SpecializationDataAnalyst)},
|
||
{pattern: regexp.MustCompile(`(?i)(?:^|[^\p{L}\p{N}_])(data scientist|machine learning|ml engineer|специалист\p{L}* машинн\p{L}* обучен\p{L}*)(?:$|[^\p{L}\p{N}_])`), value: string(domain.SpecializationDSML)},
|
||
{pattern: regexp.MustCompile(`(?i)(?:^|[^\p{L}\p{N}_])(information security|информационн\p{L}* безопасност\p{L}*)(?:$|[^\p{L}\p{N}_])`), value: string(domain.SpecializationInformationSecurity)},
|
||
{pattern: regexp.MustCompile(`(?i)(?:^|[^\p{L}\p{N}_])(qa engineer|тестировщик\p{L}*)(?:$|[^\p{L}\p{N}_])`), value: string(domain.SpecializationQA)},
|
||
}
|
||
needGradePatterns = []domainRule{
|
||
{pattern: regexp.MustCompile(`(?i)(?:^|[^\p{L}\p{N}_])(intern|internship|стаж[её]р\p{L}*)(?:$|[^\p{L}\p{N}_])`), value: string(domain.GradeIntern)},
|
||
{pattern: regexp.MustCompile(`(?i)(?:^|[^\p{L}\p{N}_])(junior|jun|джуниор\p{L}*)(?:$|[^\p{L}\p{N}_])`), value: string(domain.GradeJunior)},
|
||
{pattern: regexp.MustCompile(`(?i)(?:^|[^\p{L}\p{N}_])(middle|mid|мидл\p{L}*)(?:$|[^\p{L}\p{N}_])`), value: string(domain.GradeMiddle)},
|
||
{pattern: regexp.MustCompile(`(?i)(?:^|[^\p{L}\p{N}_])(senior|sen|сеньор\p{L}*)(?:$|[^\p{L}\p{N}_])`), value: string(domain.GradeSenior)},
|
||
{pattern: regexp.MustCompile(`(?i)(младш(?:ий|его|ему))\s+(?:backend|back-end|frontend|front-end|разработчик|инженер)`), value: string(domain.GradeJunior)},
|
||
{pattern: regexp.MustCompile(`(?i)(средн(?:ий|его|ему))\s+(?:backend|back-end|frontend|front-end|разработчик|инженер)`), value: string(domain.GradeMiddle)},
|
||
{pattern: regexp.MustCompile(`(?i)(старш(?:ий|его|ему))\s+(?:backend|back-end|frontend|front-end|разработчик|инженер)`), value: string(domain.GradeSenior)},
|
||
{pattern: regexp.MustCompile(`(?i)(?:уров(?:ень|ня|не)|грейд(?:а|е)?|позици(?:я|и))\s+(младш(?:ий|его|ему))`), value: string(domain.GradeJunior)},
|
||
{pattern: regexp.MustCompile(`(?i)(?:уров(?:ень|ня|не)|грейд(?:а|е)?|позици(?:я|и))\s+(средн(?:ий|его|ему))`), value: string(domain.GradeMiddle)},
|
||
{pattern: regexp.MustCompile(`(?i)(?:уров(?:ень|ня|не)|грейд(?:а|е)?|позици(?:я|и))\s+(старш(?:ий|его|ему))`), value: string(domain.GradeSenior)},
|
||
}
|
||
needWorkFormatRules = []formatRule{
|
||
{pattern: regexp.MustCompile(`(?i)(удал[её]нн\p{L}*|дистанционн\p{L}*|remote)`), value: domain.NeedWorkFormatRemote},
|
||
{pattern: regexp.MustCompile(`(?i)(офис\p{L}*|office)`), value: domain.NeedWorkFormatOffice},
|
||
{pattern: regexp.MustCompile(`(?i)(гибрид\p{L}*|hybrid)`), value: domain.NeedWorkFormatHybrid},
|
||
}
|
||
needSkillRules = []skillRule{
|
||
{pattern: regexp.MustCompile(`(?i)(?:^|[^\p{L}\p{N}_])(postgresql)(?:$|[^\p{L}\p{N}_])`), name: "PostgreSQL", taxonomy: "postgresql"},
|
||
{pattern: regexp.MustCompile(`(?i)(?:^|[^\p{L}\p{N}_])(kafka)(?:$|[^\p{L}\p{N}_])`), name: "Kafka", taxonomy: "kafka"},
|
||
{pattern: regexp.MustCompile(`(?i)(?:^|[^\p{L}\p{N}_])(temporal)(?:$|[^\p{L}\p{N}_])`), name: "Temporal"},
|
||
{pattern: regexp.MustCompile(`(?i)(?:^|[^\p{L}\p{N}_])(docker)(?:$|[^\p{L}\p{N}_])`), name: "Docker", taxonomy: "docker"},
|
||
{pattern: regexp.MustCompile(`(?i)(?:^|[^\p{L}\p{N}_])(go)(?:$|[^\p{L}\p{N}_])`), name: "Go", taxonomy: "go"},
|
||
}
|
||
)
|
||
|
||
type domainRule struct {
|
||
pattern *regexp.Regexp
|
||
value string
|
||
}
|
||
|
||
type formatRule struct {
|
||
pattern *regexp.Regexp
|
||
value domain.NeedWorkFormat
|
||
}
|
||
|
||
type skillRule struct {
|
||
pattern *regexp.Regexp
|
||
name string
|
||
taxonomy string
|
||
}
|
||
|
||
type needTextSection struct {
|
||
start int
|
||
end int
|
||
quote string
|
||
want bool
|
||
}
|
||
|
||
type skillOccurrence struct {
|
||
start int
|
||
name string
|
||
taxonomy string
|
||
quote string
|
||
section needTextSection
|
||
}
|
||
|
||
const maxParsedEvidenceRunes = 300
|
||
|
||
func parseSourceText(source string, version int64) domain.NeedRequirements {
|
||
requirements := domain.EmptyNeedRequirements()
|
||
requirements.Specialization = parseNeedScalar(source, version, needSpecializationPatterns, func(value string) domain.SpecializationCode {
|
||
return domain.SpecializationCode(value)
|
||
})
|
||
requirements.Grade = parseNeedScalar(source, version, needGradePatterns, func(value string) domain.GradeCode {
|
||
return domain.GradeCode(value)
|
||
})
|
||
requirements.Domain = parseNeedScalar(source, version, needDomainPatterns, func(value string) string {
|
||
return value
|
||
})
|
||
requirements.WorkFormats = parseWorkFormats(source, version)
|
||
requirements.RequiredSkills, requirements.DesiredSkills = parseSkills(source, version)
|
||
requirements.Tasks = parseTasks(source, version)
|
||
return requirements
|
||
}
|
||
|
||
func parseNeedScalar[T any](source string, version int64, rules []domainRule, convert func(string) T) domain.NeedValue[T] {
|
||
type candidate struct {
|
||
start int
|
||
value T
|
||
quote string
|
||
}
|
||
found := make([]candidate, 0, len(rules))
|
||
values := make(map[string]struct{}, len(rules))
|
||
for _, rule := range rules {
|
||
for _, indices := range rule.pattern.FindAllStringSubmatchIndex(source, -1) {
|
||
if len(indices) < 4 || hasNeedNegation(source, indices[2]) {
|
||
continue
|
||
}
|
||
value := convert(rule.value)
|
||
key := fmt.Sprint(value)
|
||
values[key] = struct{}{}
|
||
found = append(found, candidate{
|
||
start: indices[2], value: value,
|
||
quote: strings.TrimSpace(source[indices[2]:indices[3]]),
|
||
})
|
||
}
|
||
}
|
||
if len(values) != 1 || len(found) == 0 {
|
||
return unknownNeedValue[T]()
|
||
}
|
||
sort.Slice(found, func(i, j int) bool { return found[i].start < found[j].start })
|
||
value := found[0].value
|
||
return domain.NeedValue[T]{Status: domain.NeedValueExtracted, Value: &value, Source: extractedNeedSource(found[0].quote, version)}
|
||
}
|
||
|
||
func parseWorkFormats(source string, version int64) domain.NeedValue[[]domain.NeedWorkFormat] {
|
||
type foundFormat struct {
|
||
start int
|
||
end int
|
||
format domain.NeedWorkFormat
|
||
}
|
||
found := make([]foundFormat, 0, len(needWorkFormatRules))
|
||
seen := make(map[domain.NeedWorkFormat]struct{}, len(needWorkFormatRules))
|
||
for _, rule := range needWorkFormatRules {
|
||
for _, indices := range rule.pattern.FindAllStringSubmatchIndex(source, -1) {
|
||
if len(indices) < 4 || hasNeedNegation(source, indices[2]) {
|
||
continue
|
||
}
|
||
if _, ok := seen[rule.value]; ok {
|
||
continue
|
||
}
|
||
seen[rule.value] = struct{}{}
|
||
found = append(found, foundFormat{start: indices[2], end: indices[3], format: rule.value})
|
||
}
|
||
}
|
||
if len(found) == 0 {
|
||
return domain.NeedValue[[]domain.NeedWorkFormat]{Status: domain.NeedValueUnknown}
|
||
}
|
||
sort.Slice(found, func(i, j int) bool { return found[i].start < found[j].start })
|
||
formats := make([]domain.NeedWorkFormat, 0, len(found))
|
||
for _, item := range found {
|
||
formats = append(formats, item.format)
|
||
}
|
||
quote := strings.TrimSpace(source[found[0].start:found[len(found)-1].end])
|
||
if utf8.RuneCountInString(quote) > maxParsedEvidenceRunes {
|
||
return domain.NeedValue[[]domain.NeedWorkFormat]{Status: domain.NeedValueUnknown}
|
||
}
|
||
value := formats
|
||
return domain.NeedValue[[]domain.NeedWorkFormat]{
|
||
Status: domain.NeedValueExtracted,
|
||
Value: &value,
|
||
Source: extractedNeedSource(quote, version),
|
||
}
|
||
}
|
||
|
||
func parseSkills(source string, version int64) ([]domain.NeedSkill, []domain.NeedSkill) {
|
||
sections := skillSections(source)
|
||
occurrences := make([]skillOccurrence, 0, len(needSkillRules))
|
||
for _, section := range sections {
|
||
segment := source[section.start:section.end]
|
||
for _, rule := range needSkillRules {
|
||
for _, indices := range rule.pattern.FindAllStringSubmatchIndex(segment, -1) {
|
||
if len(indices) < 4 || hasNeedNegation(source, section.start+indices[2]) {
|
||
continue
|
||
}
|
||
start := section.start + indices[2]
|
||
occurrences = append(occurrences, skillOccurrence{
|
||
start: start, name: rule.name, taxonomy: rule.taxonomy,
|
||
quote: source[section.start+indices[2] : section.start+indices[3]], section: section,
|
||
})
|
||
}
|
||
}
|
||
}
|
||
sort.Slice(occurrences, func(i, j int) bool { return occurrences[i].start < occurrences[j].start })
|
||
seen := make(map[string]struct{}, len(occurrences))
|
||
required := make([]domain.NeedSkill, 0, len(occurrences))
|
||
desired := make([]domain.NeedSkill, 0, len(occurrences))
|
||
for _, occurrence := range occurrences {
|
||
key := strings.ToLower(occurrence.name)
|
||
if _, ok := seen[key]; ok {
|
||
continue
|
||
}
|
||
seen[key] = struct{}{}
|
||
var taxonomy *string
|
||
if occurrence.taxonomy != "" {
|
||
value := occurrence.taxonomy
|
||
taxonomy = &value
|
||
}
|
||
item := domain.NeedSkill{
|
||
Name: occurrence.name, TaxonomyCode: taxonomy, Status: domain.NeedItemExtracted,
|
||
Source: *extractedNeedSource(skillQuote(occurrence), version),
|
||
}
|
||
if occurrence.section.want {
|
||
desired = append(desired, item)
|
||
} else {
|
||
required = append(required, item)
|
||
}
|
||
}
|
||
return required, desired
|
||
}
|
||
|
||
func skillQuote(occurrence skillOccurrence) string {
|
||
if occurrence.section.quote != "" && utf8.RuneCountInString(occurrence.section.quote) <= maxParsedEvidenceRunes {
|
||
return occurrence.section.quote
|
||
}
|
||
return occurrence.quote
|
||
}
|
||
|
||
func skillSections(source string) []needTextSection {
|
||
matches := needSectionPattern.FindAllStringSubmatchIndex(source, -1)
|
||
if len(matches) == 0 {
|
||
return []needTextSection{{start: 0, end: len(source), quote: ""}}
|
||
}
|
||
sections := make([]needTextSection, 0, len(matches))
|
||
for index, match := range matches {
|
||
end := len(source)
|
||
if index+1 < len(matches) {
|
||
end = matches[index+1][0]
|
||
}
|
||
if sentenceEnd := strings.IndexAny(source[match[1]:end], ".!?\n"); sentenceEnd >= 0 {
|
||
end = match[1] + sentenceEnd
|
||
}
|
||
segment := strings.TrimSpace(source[match[0]:end])
|
||
if segment == "" {
|
||
continue
|
||
}
|
||
want := isDesiredSection(source[match[2]:match[3]])
|
||
sections = append(sections, needTextSection{start: match[0], end: end, quote: segment, want: want})
|
||
}
|
||
if len(sections) == 0 {
|
||
return []needTextSection{{start: 0, end: len(source), quote: ""}}
|
||
}
|
||
return sections
|
||
}
|
||
|
||
func isDesiredSection(label string) bool {
|
||
label = strings.ToLower(label)
|
||
return strings.Contains(label, "желат") || strings.Contains(label, "предпочт") || strings.Contains(label, "preferred")
|
||
}
|
||
|
||
func hasNeedNegation(source string, start int) bool {
|
||
if start < 0 || start > len(source) {
|
||
return false
|
||
}
|
||
prefix := source[:start]
|
||
if boundary := strings.LastIndexAny(prefix, ".!?;,\n"); boundary >= 0 {
|
||
prefix = prefix[boundary+1:]
|
||
}
|
||
return needNegationPattern.MatchString(prefix)
|
||
}
|
||
|
||
func parseTasks(source string, version int64) []domain.NeedTask {
|
||
indices := needTaskPattern.FindStringSubmatchIndex(source)
|
||
if len(indices) < 4 {
|
||
return []domain.NeedTask{}
|
||
}
|
||
start := indices[1]
|
||
end := len(source)
|
||
if sentenceEnd := strings.IndexAny(source[start:], ".!?\n"); sentenceEnd >= 0 {
|
||
end = start + sentenceEnd
|
||
}
|
||
text := strings.TrimSpace(source[start:end])
|
||
text = strings.Trim(text, " :;,-\t\r")
|
||
quote := strings.TrimSpace(source[indices[0]:end])
|
||
if text == "" || utf8.RuneCountInString(text) > maxParsedEvidenceRunes || utf8.RuneCountInString(quote) > maxParsedEvidenceRunes {
|
||
return []domain.NeedTask{}
|
||
}
|
||
value := []rune(text)
|
||
value[0] = unicode.ToUpper(value[0])
|
||
text = string(value)
|
||
return []domain.NeedTask{{
|
||
Text: text, Status: domain.NeedItemExtracted,
|
||
Source: *extractedNeedSource(quote, version),
|
||
}}
|
||
}
|
||
|
||
func extractedNeedSource(quote string, version int64) *domain.NeedValueSource {
|
||
versionLabel := needRulesVersion
|
||
var quoteValue *string
|
||
if quote != "" {
|
||
quoteValue = "e
|
||
}
|
||
extractedVersion := version
|
||
return &domain.NeedValueSource{
|
||
Origin: domain.NeedValueExtractedFromText,
|
||
Quote: quoteValue,
|
||
ExtractedInVersion: &extractedVersion,
|
||
ExtractionVersion: &versionLabel,
|
||
}
|
||
}
|