Current section
12 Versions
Jump to
Current section
12 Versions
Compare versions
4
files changed
+31
additions
-26
deletions
| @@ -1,5 +1,10 @@ | |
| 1 1 | # Paasaa |
| 2 2 | |
| 3 | + [](https://travis-ci.org/minibikini/paasaa) |
| 4 | + [](https://coveralls.io/github/minibikini/paasaa?branch=master) |
| 5 | + [](https://hex.pm/packages/paasaa) |
| 6 | + [](https://hex.pm/packages/paasaa) |
| 7 | + |
| 3 8 | Language detection for Elixir |
| 4 9 | |
| 5 10 | [Api Documentation] | [Hex Package] |
| @@ -53,4 +58,4 @@ iex> Paasaa.all("Detect this!") | |
| 53 58 | [Franc]: https://github.com/wooorm/franc/ |
| 54 59 | [Titus Wormer]: http://wooorm.com/ |
| 55 60 | [mit]: LICENSE |
| 56 | - [Egor Kislitsyn]: https://github/minibikini |
| 61 | + [Egor Kislitsyn]: https://github.com/minibikini |
| @@ -1,6 +1,6 @@ | |
| 1 1 | {<<"app">>,<<"paasaa">>}. |
| 2 2 | {<<"build_tools">>,[<<"mix">>]}. |
| 3 | - {<<"description">>,<<"Provides language detection functionss">>}. |
| 3 | + {<<"description">>,<<"Natural language detection">>}. |
| 4 4 | {<<"elixir">>,<<"~> 1.3">>}. |
| 5 5 | {<<"files">>, |
| 6 6 | [<<"lib/paasaa.ex">>,<<"priv/languages.json">>,<<"priv/scripts.json">>, |
| @@ -14,4 +14,4 @@ | |
| 14 14 | {<<"name">>,<<"exjsx">>}, |
| 15 15 | {<<"optional">>,false}, |
| 16 16 | {<<"requirement">>,<<"~> 3.2">>}]]}. |
| 17 | - {<<"version">>,<<"0.1.0">>}. |
| 17 | + {<<"version">>,<<"0.1.1">>}. |
| @@ -21,7 +21,7 @@ defmodule Paasaa do | |
| 21 21 | langs = Enum.map(langs, fn {lang, trigrams} -> |
| 22 22 | trigrams = trigrams |
| 23 23 | |> String.split("|") |
| 24 | - |> Enum.with_index() |
| 24 | + |> Enum.with_index |
| 25 25 | |> Enum.into(%{}) |
| 26 26 | |
| 27 27 | {lang, trigrams} |
| @@ -33,7 +33,7 @@ defmodule Paasaa do | |
| 33 33 | |
| 34 34 | @max_difference 300 |
| 35 35 | |
| 36 | - @type result :: [{language :: Strinsg.t, score :: number}] |
| 36 | + @type result :: [{language :: String.t, score :: number}] |
| 37 37 | |
| 38 38 | @type options :: [ |
| 39 39 | min_length: integer, |
| @@ -50,7 +50,7 @@ defmodule Paasaa do | |
| 50 50 | ] |
| 51 51 | |
| 52 52 | @doc """ |
| 53 | - Detects a language. Returns a string with ISO6393 language code (i.g. "eng"). |
| 53 | + Detects a language. Returns a string with ISO6393 language code (e.g. "eng"). |
| 54 54 | |
| 55 55 | ## Parameters |
| 56 56 | |
| @@ -88,7 +88,7 @@ defmodule Paasaa do | |
| 88 88 | def detect(str, options \\ @default_options) do |
| 89 89 | str |
| 90 90 | |> all(options) |
| 91 | - |> List.first() |
| 91 | + |> List.first |
| 92 92 | |> elem(0) |
| 93 93 | end |
| 94 94 | |
| @@ -139,7 +139,7 @@ defmodule Paasaa do | |
| 139 139 | count == 0 -> und |
| 140 140 | Map.has_key?(@languages, script) -> |
| 141 141 | str |
| 142 | - |> get_clean_trigrams() |
| 142 | + |> get_clean_trigrams |
| 143 143 | |> get_distances(@languages[script], options) |
| 144 144 | |> normalize(str) |
| 145 145 | |
| @@ -158,13 +158,9 @@ defmodule Paasaa do | |
| 158 158 | |> Enum.max_by(fn {_, count} -> count end) |
| 159 159 | end |
| 160 160 | |
| 161 | - @spec get_occurrence(str :: String.t, re :: Regex.t, str_len :: number) :: number |
| 161 | + @spec get_occurrence(str :: String.t, re :: Regex.t, str_len :: non_neg_integer) :: float |
| 162 162 | defp get_occurrence(str, re, str_len) do |
| 163 | - if found = Regex.scan(re, str) do |
| 164 | - Enum.count(found) / str_len |
| 165 | - else |
| 166 | - 0 |
| 167 | - end |
| 163 | + Enum.count(Regex.scan(re, str)) / str_len |
| 168 164 | end |
| 169 165 | |
| 170 166 | @spec get_distances([String.t], Enumerable.t, options) :: result |
| @@ -203,7 +199,7 @@ defmodule Paasaa do | |
| 203 199 | @spec normalize(result, String.t) :: result |
| 204 200 | defp normalize([], _str), do: und |
| 205 201 | defp normalize(distances, str) do |
| 206 | - min = distances |> List.first() |> elem(1) |
| 202 | + min = distances |> List.first |> elem(1) |
| 207 203 | max = String.length(str) * @max_difference - min |
| 208 204 | |
| 209 205 | Enum.map distances, fn({lang, dist}) -> |
| @@ -215,12 +211,12 @@ defmodule Paasaa do | |
| 215 211 | |
| 216 212 | # trigram stuff |
| 217 213 | |
| 218 | - @spec get_clean_trigrams(String.t) :: [String.t] |
| 214 | + @spec get_clean_trigrams(String.t) :: result |
| 219 215 | defp get_clean_trigrams(str) do |
| 220 216 | str |
| 221 | - |> clean() |
| 222 | - |> pad() |
| 223 | - |> n_grams() |
| 217 | + |> clean |
| 218 | + |> pad |
| 219 | + |> n_grams |
| 224 220 | |> Enum.reduce(%{}, fn(trigram, acc) -> |
| 225 221 | count = acc[trigram] && acc[trigram] + 1 || 1 |
| 226 222 | Map.put(acc, trigram, count) |
| @@ -235,8 +231,8 @@ defmodule Paasaa do | |
| 235 231 | str |
| 236 232 | |> String.replace(expression_symbols, " ") |
| 237 233 | |> String.replace(~r/\s+/, " ") |
| 238 | - |> String.trim() |
| 239 | - |> String.downcase() |
| 234 | + |> String.trim |
| 235 | + |> String.downcase |
| 240 236 | end |
| 241 237 | |
| 242 238 | defp pad(str), do: " #{str} " |
| @@ -244,7 +240,7 @@ defmodule Paasaa do | |
| 244 240 | @spec n_grams(str :: String.t, n :: number) :: [String.t] |
| 245 241 | defp n_grams(str, n \\ 3) do |
| 246 242 | str |
| 247 | - |> String.graphemes() |
| 243 | + |> String.graphemes |
| 248 244 | |> Enum.chunk(n, 1) |
| 249 245 | |> Enum.map(&Enum.join/1) |
| 250 246 | end |
| @@ -4,11 +4,13 @@ defmodule Paasaa.Mixfile do | |
| 4 4 | def project do |
| 5 5 | [ |
| 6 6 | app: :paasaa, |
| 7 | - version: "0.1.0", |
| 7 | + version: "0.1.1", |
| 8 8 | elixir: "~> 1.3", |
| 9 9 | description: description(), |
| 10 10 | package: package(), |
| 11 | - deps: deps() |
| 11 | + deps: deps(), |
| 12 | + test_coverage: [tool: ExCoveralls], |
| 13 | + preferred_cli_env: ["coveralls": :test, "coveralls.detail": :test, "coveralls.post": :test, "coveralls.html": :test] |
| 12 14 | ] |
| 13 15 | end |
| 14 16 | |
| @@ -22,13 +24,15 @@ defmodule Paasaa.Mixfile do | |
| 22 24 | {:ex_doc, "~> 0.12", only: :dev}, |
| 23 25 | {:mix_test_watch, "~> 0.2", only: :dev}, |
| 24 26 | {:benchfella, "~> 0.3.0", only: :dev}, |
| 25 | - {:credo, "~> 0.4", only: [:dev, :test]} |
| 27 | + {:credo, "~> 0.4", only: [:dev, :test]}, |
| 28 | + {:excoveralls, "~> 0.5", only: :test}, |
| 29 | + {:dialyxir, "~> 0.3.5", only: [:dev]} |
| 26 30 | ] |
| 27 31 | end |
| 28 32 | |
| 29 33 | defp description do |
| 30 34 | """ |
| 31 | - Provides language detection functionss |
| 35 | + Natural language detection |
| 32 36 | """ |
| 33 37 | end |