# Generated by Snowball.Generator — do not edit by hand. defmodule Text.Stemmer.Stemmers.Swedish do @moduledoc """ Snowball stemmer for swedish. Generated from the canonical Snowball algorithm source. """ alias Snowball.Runtime alias Snowball.Grouping # Groupings @g_ost_ending Grouping.from_string("iklnprtuv") @g_s_ending Grouping.from_string("bcdfghjklmnoprtvy") @g_v Grouping.from_string("aeiouyäåö") # Among tables @a_0 [ {"ig", -1, 1, nil}, {"lig", 0, 1, nil}, {"els", -1, 1, nil}, {"fullt", -1, 3, nil}, {"öst", -1, 2, nil} ] @a_1 [ {"dd", -1, -1, nil}, {"gd", -1, -1, nil}, {"nn", -1, -1, nil}, {"dt", -1, -1, nil}, {"gt", -1, -1, nil}, {"kt", -1, -1, nil}, {"tt", -1, -1, nil} ] @a_2 [ {"a", -1, 1, nil}, {"arna", 0, 1, nil}, {"erna", 0, 1, nil}, {"heterna", 2, 1, nil}, {"orna", 0, 1, nil}, {"ad", -1, 1, nil}, {"e", -1, 1, nil}, {"ade", 6, 1, nil}, {"ande", 6, 1, nil}, {"arne", 6, 1, nil}, {"are", 6, 1, nil}, {"aste", 6, 1, nil}, {"en", -1, 1, nil}, {"anden", 12, 1, nil}, {"aren", 12, 1, nil}, {"heten", 12, 1, nil}, {"ern", -1, 1, nil}, {"ar", -1, 1, nil}, {"er", -1, 1, nil}, {"heter", 18, 1, nil}, {"or", -1, 1, nil}, {"s", -1, 2, nil}, {"as", 21, 1, nil}, {"arnas", 22, 1, nil}, {"ernas", 22, 1, nil}, {"ornas", 22, 1, nil}, {"es", 21, 1, nil}, {"ades", 26, 1, nil}, {"andes", 26, 1, nil}, {"ens", 21, 1, nil}, {"arens", 29, 1, nil}, {"hetens", 29, 1, nil}, {"erns", 21, 1, nil}, {"at", -1, 1, nil}, {"et", -1, 3, nil}, {"andet", 34, 1, nil}, {"het", 34, 1, nil}, {"ast", -1, 1, nil} ] @a_3 [ {"fab", -1, -1, nil}, {"h", -1, -1, nil}, {"pak", -1, -1, nil}, {"rak", -1, -1, nil}, {"stak", -1, -1, nil}, {"kom", -1, -1, nil}, {"iet", -1, -1, nil}, {"cit", -1, -1, nil}, {"dit", -1, -1, nil}, {"alit", -1, -1, nil}, {"ilit", -1, -1, nil}, {"mit", -1, -1, nil}, {"nit", -1, -1, nil}, {"pit", -1, -1, nil}, {"rit", -1, -1, nil}, {"sit", -1, -1, nil}, {"tit", -1, -1, nil}, {"uit", -1, -1, nil}, {"ivit", -1, -1, nil}, {"kvit", -1, -1, nil}, {"xit", -1, -1, nil} ] @doc """ Stem a word. ### Arguments * `word` is a UTF-8 binary. ### Returns * The stemmed UTF-8 binary. """ @spec stem(binary()) :: binary() def stem(word) when is_binary(word) do state = Runtime.new(word) |> init_vars() state = run_stem(state) Runtime.assign_to(state) end defp init_vars(state), do: %{state | vars: %{p1: 0, x: 0}} defp run_stem(%Runtime{} = state) do {_, state} = r_stem(state) state end # Snowball runtime helpers. defp snowball_do_f(state, fun) do saved_c = state.cursor {_, s} = fun.(state) %{s | cursor: saved_c} end defp snowball_do_b(state, fun) do rel = state.limit - state.cursor {_, s} = fun.(state) %{s | cursor: s.limit - rel} end defp snowball_test_f(state, fun) do saved_c = state.cursor case fun.(state) do {:ok, s} -> {:ok, %{s | cursor: saved_c}} {:fail, s} -> {:fail, %{s | cursor: saved_c}} end end defp snowball_test_b(state, fun) do rel = state.limit - state.cursor case fun.(state) do {:ok, s} -> {:ok, %{s | cursor: s.limit - rel}} {:fail, s} -> {:fail, %{s | cursor: s.limit - rel}} end end defp snowball_try(state, fun) do rel = state.limit - state.cursor case fun.(state) do {:ok, s} -> s {:fail, s} -> %{s | cursor: s.limit - rel} end end defp snowball_or(state, fun1, fun2) do rel = state.limit - state.cursor case fun1.(state) do {:ok, s} -> {:ok, s} {:fail, s} -> fun2.(%{s | cursor: s.limit - rel}) end end defp lift(state, :fail), do: {:fail, state} defp lift(_state, %Runtime{} = s), do: {:ok, s} defp next_codepoint(%Runtime{cursor: c, limit: lim, current: cur} = state) do case Runtime.codepoint_at(cur, c, lim) do {_cp, size} -> {:ok, %{state | cursor: c + size}} :error -> {:fail, state} end end defp next_codepoint_b(%Runtime{cursor: c, limit_backward: lb, current: cur} = state) do case Runtime.codepoint_before(cur, c, lb) do {_cp, size} -> {:ok, %{state | cursor: c - size}} :error -> {:fail, state} end end defp r_other_suffix(%Runtime{} = state) do (fn -> old_limit = state.limit_backward case (fn state -> (fn -> target = state.vars[:p1] if target < state.limit_backward or target > state.limit do {:fail, state} else {:ok, %{state | cursor: target}} end end).() end).(state) do {:fail, _} -> {:fail, state} {:ok, limit_state} -> state = %{state | ket: state.cursor, limit_backward: limit_state.cursor} case Runtime.find_among_b(state, @a_0) do :fail -> {:fail, %{state | limit_backward: old_limit}} {%Runtime{} = s, result} -> state = %{s | bra: s.cursor, limit_backward: old_limit} case result do 1 -> {:ok, Runtime.slice_del(state)} 2 -> case (fn state -> lift(state, Runtime.in_grouping_b(state, @g_ost_ending)) end).(state) do {:ok, state} -> {:ok, Runtime.slice_from(state, "ös")} r -> r end 3 -> {:ok, Runtime.slice_from(state, "full")} _ -> {:ok, state} end end end end).() end defp r_consonant_pair(%Runtime{} = state) do (fn -> case (fn state -> (fn -> target = state.vars[:p1] if target < state.limit_backward or target > state.limit do {:fail, state} else {:ok, %{state | cursor: target}} end end).() end).(state) do {:fail, _} -> {:fail, state} {:ok, limit_state} -> old_lb = state.limit_backward state = %{state | limit_backward: limit_state.cursor} case (fn state -> case (fn state -> snowball_test_b(state, fn state -> (fn -> case Runtime.find_among_b(state, @a_1) do :fail -> {:fail, state} {%Runtime{} = s, _} -> {:ok, s} end end).() end) end).(state) do {:ok, state} -> case (fn state -> {:ok, %{state | ket: state.cursor}} end).(state) do {:ok, state} -> case (fn state -> next_codepoint_b(state) end).(state) do {:ok, state} -> case (fn state -> {:ok, %{state | bra: state.cursor}} end).(state) do {:ok, state} -> {:ok, Runtime.slice_del(state)} end r -> r end end r -> r end end).(state) do {:ok, s} -> {:ok, %{s | limit_backward: old_lb}} {:fail, s} -> {:fail, %{s | limit_backward: old_lb}} end end end).() end defp r_main_suffix(%Runtime{} = state) do (fn -> old_limit = state.limit_backward case (fn state -> (fn -> target = state.vars[:p1] if target < state.limit_backward or target > state.limit do {:fail, state} else {:ok, %{state | cursor: target}} end end).() end).(state) do {:fail, _} -> {:fail, state} {:ok, limit_state} -> state = %{state | ket: state.cursor, limit_backward: limit_state.cursor} case Runtime.find_among_b(state, @a_2) do :fail -> {:fail, %{state | limit_backward: old_limit}} {%Runtime{} = s, result} -> state = %{s | bra: s.cursor, limit_backward: old_limit} case result do 1 -> {:ok, Runtime.slice_del(state)} 2 -> case (fn state -> snowball_or(state, fn state -> case (fn state -> lift(state, Runtime.eq_s_b(state, "et")) end).(state) do {:ok, state} -> case (fn state -> r_et_condition(state) end).(state) do {:ok, state} -> {:ok, %{state | bra: state.cursor}} r -> r end r -> r end end, fn state -> lift(state, Runtime.in_grouping_b(state, @g_s_ending)) end) end).(state) do {:ok, state} -> {:ok, Runtime.slice_del(state)} r -> r end 3 -> case (fn state -> r_et_condition(state) end).(state) do {:ok, state} -> {:ok, Runtime.slice_del(state)} r -> r end _ -> {:ok, state} end end end end).() end defp r_et_condition(%Runtime{} = state) do case (fn state -> snowball_test_b(state, fn state -> case (fn state -> lift(state, Runtime.out_grouping_b(state, @g_v)) end).(state) do {:ok, state} -> case (fn state -> lift(state, Runtime.in_grouping_b(state, @g_v)) end).(state) do {:ok, state} -> (fn state -> saved_c = state.cursor case (fn state -> if state.cursor <= state.limit_backward, do: {:ok, state}, else: {:fail, state} end).(state) do {:ok, s} -> {:fail, %{s | cursor: saved_c}} {:fail, s} -> {:ok, %{s | cursor: saved_c}} end end).(state) r -> r end r -> r end end) end).(state) do {:ok, state} -> (fn state -> saved_c = state.cursor case (fn state -> (fn -> case Runtime.find_among_b(state, @a_3) do :fail -> {:fail, state} {%Runtime{} = s, _} -> {:ok, s} end end).() end).(state) do {:ok, s} -> {:fail, %{s | cursor: saved_c}} {:fail, s} -> {:ok, %{s | cursor: saved_c}} end end).(state) r -> r end end defp r_mark_regions(%Runtime{} = state) do case (fn state -> {:ok, put_in(state.vars[:p1], state.limit)} end).(state) do {:ok, state} -> case (fn state -> snowball_test_f(state, fn state -> case (fn state -> (fn -> n = 3 Enum.reduce_while(1..max(n, 0)//1, {:ok, state}, fn _, {:ok, s} -> case next_codepoint(s) do {:ok, s2} -> {:cont, {:ok, s2}} {:fail, _} -> {:halt, {:fail, state}} end end) end).() end).(state) do {:ok, state} -> {:ok, put_in(state.vars[:x], state.cursor)} r -> r end end) end).(state) do {:ok, state} -> case (fn state -> case Runtime.go_out_grouping(state, @g_v) do :fail -> {:fail, state} %Runtime{} = s -> next_codepoint(s) end end).(state) do {:ok, state} -> case (fn state -> case Runtime.go_in_grouping(state, @g_v) do :fail -> {:fail, state} %Runtime{} = s -> next_codepoint(s) end end).(state) do {:ok, state} -> case (fn state -> {:ok, put_in(state.vars[:p1], state.cursor)} end).(state) do {:ok, state} -> {:ok, snowball_try(state, fn state -> case (fn state -> if state.vars[:p1] < state.vars[:x], do: {:ok, state}, else: {:fail, state} end).(state) do {:ok, state} -> {:ok, put_in(state.vars[:p1], state.vars[:x])} r -> r end end)} end r -> r end r -> r end r -> r end end end defp r_stem(%Runtime{} = state) do case (fn state -> {:ok, snowball_do_f(state, fn state -> r_mark_regions(state) end)} end).(state) do {:ok, state} -> (fn state -> old_cursor = state.cursor old_lb = state.limit_backward state = %{state | cursor: state.limit, limit_backward: old_cursor} {tag, s} = (fn state -> case (fn state -> {:ok, snowball_do_b(state, fn state -> r_main_suffix(state) end)} end).(state) do {:ok, state} -> case (fn state -> {:ok, snowball_do_b(state, fn state -> r_consonant_pair(state) end)} end).(state) do {:ok, state} -> {:ok, snowball_do_b(state, fn state -> r_other_suffix(state) end)} end end end).(state) {tag, %{s | cursor: old_cursor, limit_backward: old_lb}} end).(state) end end end