Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
packages/
# See https://www.dartlang.org/guides/libraries/private-files

# Files and directories created by pub
.dart_tool/
.packages
.vscode

build/
# If you're building an application, you may want to check-in your pubspec.lock
.pubspec.lock

# Directory created by dartdoc
# If you don't generate documentation locally you can remove this line.
doc/api/

# Avoid committing generated Javascript files:
*.dart.js
*.info.json # Produced by the --dump-info flag.
*.js # When generated by dart2js. Don't specify *.js if your
# project includes source files written in JavaScript.
*.js_
*.js.deps
*.js.map
4 changes: 2 additions & 2 deletions lib/petrovich.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library petrovich;

import 'dart:io';
import 'dart:convert';
import 'dart:io';

part './src/petrovich.dart';
part './src/petrovich.dart';
55 changes: 24 additions & 31 deletions lib/src/petrovich.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
part of petrovich;

class Petrovich {

static const CASE_NOMENATIVE = -1;
static const CASE_GENITIVE = 0;
static const CASE_DATIVE = 1;
Expand All @@ -16,19 +15,17 @@ class Petrovich {
Map _rules;
int gender = GENDER_ANDROGYNOUS;

/**
* Constructor
*/
/// Constructor
///
Petrovich([this.gender = GENDER_ANDROGYNOUS, rulesPath = '../rules/rules.json']) {
File rulesFile = new File(rulesPath);
final rulesFile = File(rulesPath);

String rulesJson = rulesFile.readAsStringSync();
_rules = JSON.decode(rulesJson);
final rulesJson = rulesFile.readAsStringSync();
_rules = json.decode(rulesJson);
}

/**
* Detect gender by middle name
*/
/// Detect gender by middle name
///
static int detectGender(String middleName) {
switch (middleName.substring(middleName.length - 2)) {
case 'ич':
Expand All @@ -40,10 +37,9 @@ class Petrovich {
}
}

/**
* Get gender int by string
*/
String _getGender(String gender) {
///Get gender int by string
///
int _getGender(String gender) {
switch (gender) {
case 'male':
return GENDER_MALE;
Expand All @@ -52,25 +48,24 @@ class Petrovich {
case 'androgynous':
return GENDER_ANDROGYNOUS;
}
return GENDER_ANDROGYNOUS;
}

int _checkGender(String gender) {
bool _checkGender(String gender) {
return this.gender == _getGender(gender) || _getGender(gender) == GENDER_ANDROGYNOUS;
}

String _applyRule(List<String> mods, String name, int cs) {
int to = name.length - mods[cs].allMatches('-').length;
String result = name.substring(0, to);
result += mods[cs].replaceAll('-', '');
return result;
final to = name.length - mods[cs].allMatches('-').length;
return name.substring(0, to) + mods[cs].replaceAll('-', '');
}

_checkException(String name, int cs, String type) {
String _checkException(String name, int cs, String type) {
if (!_rules[type].keys.contains('exceptions')) {
return false;
return null;
}

String lowerName = name.toLowerCase();
final lowerName = name.toLowerCase();

for (var rule in _rules[type]['exceptions']) {
if (!_checkGender(rule['gender'])) {
Expand All @@ -86,21 +81,20 @@ class Petrovich {
}
}

return false;

return null;
}

String _findInRules(String name, int cs, String type) {
var suffixes = _rules[type]['suffixes'];
final suffixes = _rules[type]['suffixes'];

for (var rule in suffixes) {
if (!_checkGender(rule['gender'])) {
continue;
}

for (String lastChar in rule['test']) {
int from = name.length - lastChar.length;
String lastNameChar = name.substring(from, name.length);
final from = name.length - lastChar.length;
final lastNameChar = name.substring(from, name.length);

if (lastChar == lastNameChar) {
if (rule['mods'][cs] == '.') {
Expand All @@ -116,11 +110,11 @@ class Petrovich {
}

String _inflect(String name, int cs, String type) {
List<String> namesArr = name.split('-');
List<String> result = new List<String>();
final namesArr = name.split('-');
final result = <String>[];

namesArr.forEach((String arrName) {
var exception = _checkException(arrName, cs, type);
final exception = _checkException(arrName, cs, type);
if (exception is String) {
result.add(exception);
} else {
Expand Down Expand Up @@ -154,5 +148,4 @@ class Petrovich {

return _inflect(name, cs, 'firstname');
}

}
6 changes: 2 additions & 4 deletions lib/tests/test.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import 'package:unittest/unittest.dart';
import 'package:test/test.dart';
import 'package:petrovich/petrovich.dart';

main() {

test('have gender androgynous', () {
return expect(Petrovich.GENDER_ANDROGYNOUS, new Petrovich().gender);
});
Expand All @@ -26,5 +25,4 @@ main() {
test('detect androgynous gender', () {
return expect(Petrovich.GENDER_ANDROGYNOUS, Petrovich.detectGender('Блабла'));
});

}
}
19 changes: 0 additions & 19 deletions pubspec.lock

This file was deleted.

7 changes: 5 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
name: petrovich
version: 0.2.1
version: 0.2.2
author: Alexey Salnikov <me@iamsalnikov.ru>
description: A library to inflect Russian anthroponyms such as first names, last names, and middle names. Port of https://github.com/petrovich/petrovich-ruby
homepage: https://github.com/petrovich/petrovich-dart
environment:
sdk: ">=2.10.0 <3.0.0"

dev_dependencies:
unittest: '>=0.11.0+2 <0.12.0'
test: ^1.17.4