How to resolve the algorithm Rosetta Code/Find bare lang tags step by step in the Erlang programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Rosetta Code/Find bare lang tags step by step in the Erlang programming language

Table of Contents

Problem Statement

Find all     tags without a language specified in the text of a page.
Display counts by language section: should display something like

Allow multiple files to be read.   Summarize all results by language:

Use the   Media Wiki API   to test actual RC tasks.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Rosetta Code/Find bare lang tags step by step in the Erlang programming language

Source code in the erlang programming language

-module( find_bare_lang_tags ).

-export( [task/0] ).

task() ->
	{ok, Binary} = file:read_file( "priv/find_bare_lang_tags_1" ),
	Lines = string:tokens( erlang:binary_to_list(Binary), "\n" ),
	{_Lang, Dict} = lists:foldl( fun count_empty_lang/2, {"no language", dict:new()}, Lines ),
	Count_langs = [{dict:fetch(X, Dict), X} || X <- dict:fetch_keys(Dict)],
	io:fwrite( "~p bare language tags.~n", [lists:sum([X || {X, _Y} <- Count_langs])] ),
	[io:fwrite( "~p in ~p~n", [X, Y] ) || {X, Y} <- Count_langs].



count_empty_lang( Line, {Lang, Dict} ) ->
	Empty_lang = string:str( Line, "<lang>" ),
	New_dict = dict_update_counter( Empty_lang, Lang, Dict ),
	New_lang = new_lang( string:str( Line,"==header" ), Line, Lang ),
	{New_lang, New_dict}.

dict_update_counter( 0, _Lang, Dict ) -> Dict;
dict_update_counter( _Start, Lang, Dict ) -> dict:update_counter( Lang, 1, Dict ).

new_lang( 0, _Line, Lang ) -> Lang;
new_lang( _Start, Line, _Lang ) ->
	Start = string:str( Line, "|" ),
	Stop = string:rstr( Line, "}}==" ),
	string:sub_string( Line, Start+1, Stop-1 ).


  

You may also check:How to resolve the algorithm Repeat a string step by step in the Sparkling programming language
You may also check:How to resolve the algorithm Read entire file step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Permutations/Derangements step by step in the zkl programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the min programming language
You may also check:How to resolve the algorithm Read a file line by line step by step in the R programming language