Skip to content

Instantly share code, notes, and snippets.

# Cookbook Name:: erlang R13B04
# Recipe:: default
# Author:: Ward Bekker <[email protected]>
#
# Copyright 2008-2009, Joe Williams
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
@wardbekker
wardbekker / .erlang
Created August 8, 2011 06:41
Autoloading of modules
%% autoload all modules that are in the code:path() and reside in the user directory
%% put in ~/.erlang
[code:ensure_loaded(list_to_atom(filename:rootname(filename:basename(F))))
|| P <- lists:filter(fun(Path) -> string:str(Path, os:getenv("USER")) > 0 end, code:get_path()), F <- filelib:wildcard(P ++ "/*.beam")].
@wardbekker
wardbekker / stackoverflow_importer_ser.erl
Created September 8, 2011 14:53
Import XML file using Erlsom SAX parser
handle_call({import, FilePath}, _From, State) ->
{ok, Handle} = file:open(FilePath, [read, raw, binary]),
Position = 0,
Chunk = 1024,
CState = {Handle, Position, Chunk},
{ok, _Result, _TrailingBytes} =
erlsom:parse_sax(
<<>>,
undefined,
fun(Event, _Acc) -> sax_event(Event) end,
@wardbekker
wardbekker / stackoverflow_import_ser.erl
Created September 8, 2011 14:58
Handle the sax_event when a startElement is found with the name "row"
sax_event({startElement, [] , "row", [], Attributes}) ->
{_,_,_,_,StringId} = lists:keyfind( "Id", 2, Attributes ),
Id = erlang:list_to_integer(StringId),
AttributesDict = lists:foldl(
fun( {attribute, AttributeName,_,_,AttributeValue}, AccIn )->
dict:store(AttributeName, AttributeValue, AccIn)
end,
dict:new(),
Attributes),
CompleteAttributesDict = case dict:is_key("Title", AttributesDict) of
@wardbekker
wardbekker / tokenize.erl
Created September 8, 2011 15:11
Tokenize test
Eshell V5.8.1 (abort with ^G)
1> string:tokens("this is a test, thank you!", " ,!").
["this","is","a","test","thank","you"]
@wardbekker
wardbekker / stackoverflow_import_ser.erl
Created September 9, 2011 06:31
Add Attribute Tokens
add_attribute_tokens(Id, AttributeName, AttributeValue) ->
add_facet( Id, AttributeName, AttributeValue ),
Tokens = string:tokens(string:to_lower(AttributeValue), " ,.:;-"),
TokensAndPositions = lists:zip( Tokens, lists:seq( 1, length(Tokens))),
lists:foreach(
fun({Token, Position}) ->
keyword_ser:add_keyword_position( Token, Id, Position)
end,
TokensAndPositions
).
add_facet(Id, "Tags", AttributeValue) ->
{match, Captured} = re:run(AttributeValue, "<(.*?)>", [global, {capture, all_but_first, list}]),
lists:foreach( fun(Match) -> facet_ser:add_facet_value( "Tags", Match, Id) end, Captured);
add_facet(Id, "CreationDate", AttributeValue) ->
[ Date | _Time ] = string:tokens(AttributeValue, "T"),
facet_ser:add_facet_value("CreationDate", Date, Id);
add_facet(_,_,_) ->
ignore.
add_keyword_position(Keyword, DocCatId, Position) ->
Server = get_keyword_server( Keyword ),
gen_server:cast(Server, {add, DocCatId, Position}).
get_keyword_server(Keyword) ->
ServerName = keyword_server_name(Keyword),
ServerProcessName = list_to_atom(ServerName),
case whereis(ServerProcessName) of
undefined ->
{ok, Pid} = supervisor:start_child(keyword_sup, [ServerProcessName]);
Pid ->
Pid
end,
Pid.
keyword_server_name(Keyword) ->
integer_to_list(
erlang:phash2(
porter:stem(
string:to_lower(Keyword)
)
)
) ++ "_keyword_server".