symfony2 translating errormessages - validation

I want to translate errormessages inside validation.yml.
If I have a normal "NotBlank" rule, it works like following:
- NotBlank: { message: not.blank.firstname }
But what if there are some further rules like:
- NotBlank: { message: not.blank.username }
- Length:
min: 7
max: 50
minMessage: "Your Username must be at least {{ limit }} characters length"
This works, but how should I handle the minMessage? Also for the reason that I want to give USers some hints about the min Length of the input.

You can do something like this:
- NotBlank: { message: not.blank.username }
- Length:
min: 7
max: 50
minMessage: 'username.minLength'
maxMessage: 'username.maxLength'
Your validators.LANG.yml:
username:
minLength: "Your Username must be at least 7 characters length"
maxLength: "Your Username must be at least 50 characters length"

Related

elastalert sends multiple email alerts instead of sending an aggregated email

Instead of sending one alert, ElastAlert sends email for each document which mapped. Below is my rule file. It works but I want alerts in one email. Please help any suggestion will be appreciated.
skynet.yaml: |-
---
name: skynet
type: frequency
limit_execution: "0/10 * * * *"
index: wpng-httpd-perf-*
num_events: 1
top_count_keys: ["Host_Id", "Host_Group"]
timeframe:
minutes: 15
filter:
- query:
query_string:
query: "Host_Group.keyword:ZOOKEEPER_ZK1_QA"
alert:
- "email"
email_format: html
aggregation:
minutes: 15
aggregation_key: 'Host_Id'
email:
- "johndoe#skynet.com"
from_addr: "sam#skynet.com"
alert_subject: "PLOT1 at {0}."
alert_subject_args:
- "#timestamp"
alert_text: "Hi Team,<br><br/> {0} ERROR event(s) detected in last 15 minutes <br/><br>Hosts where errors are detected :</br> Host_Id is {1} <br></br><br></br> <br>Here are a few of those :</br><br> messages {2} </br><br> </br><br/><br>bye.</br><br></br><br>Thanks <br></br> "
alert_text_type: alert_text_only
alert_text_args:
- num_matches
- Host_Id
- message
- top_count_keys
Below code worked for me.
PLOTTHREE.yaml: |-
---
name: PLOTTHREE
type: frequency
limit_execution: "0/15 * * * *"
index: home-*
num_events: 1
aggregation:
minutes: 10
include:
- Host_Group
- Host_Id
timeframe:
minutes: 15
filter:
- query:
query_string:
query: "Host_Group.keyword:fatal"
alert:
- "email"
email:
- "john#doe.com"
from_addr: "yyy#doe.com"
alert_subject: "PLOTTHREE - ERROR detected in Kafka Zookeeper logs of host group fatal at {0}."
alert_subject_args:
- "#timestamp"
alert_text: "Hello Team, ERROR event(s) detected in last 15 minutes. Hosts where errors are detected in {0}. Here is the num events {1} . "
alert_text_type: alert_text_only
alert_text_args:
- Host_Id
- num_matches

Converting Language Detection Score of CLD2 to CLD3 Accuracy

My cld2 language detection model (langID) returns for the input sentence to classify the following values
{ reliable: true,
textBytes: 181,
languages:
[ { name: 'ITALIAN', code: 'it', percent: 61, score: 774 },
{ name: 'ENGLISH', code: 'en', percent: 38, score: 1573 } ],
chunks:
[ { name: 'ITALIAN', code: 'it', offset: 0, bytes: 116 },
{ name: 'ENGLISH', code: 'en', offset: 116, bytes: 71 } ] }
where the textBytes represents the size of the input text, percent the distribution of the code in the sentence, while the score is an indicator of the quality of the detection (the smaller it is the best it is).
That said, in the brand new CLD3 neural network, the result of the classification is just the accuracy (so a probability value between 0 and 1) so like
println(ld.getCode(0))
println(ld.getScore(0))
en
0.99
I would like to figure out how to convert CLD2 score to probabilities values in order to compare the results to the new CLD3 model.

How to find an expression in a text file and process all lines until the next occurrence of the expression and repeat until end of the file

I have a text file:
Some comment on the 1st line of the file.
processing date: 31.8.2016
amount: -1.23
currency: EUR
balance: 1234.56
payer reference: /VS123456/SS0011223344/KS1212
type of the transaction: Some type of the transaction 1
additional info: Amount: 1.23 EUR 29.08.2016 Place: 123456789XY
processing date: 30.8.2016
amount: -2.23
currency: EUR
balance: 12345.56
payer reference: /VS123456/SS0011223344/KS1212
type of the transaction: Some type of the transaction 2
additional info: Amount: 2.23 EUR 28.08.2016 Place: 123456789XY
processing date: 29.8.2016
amount: -3.23
currency: EUR
balance: 123456.56
payer reference: /VS123456/SS0011223344/KS1212
type of the transaction: Some type of the transaction 2
additional info: Amount: 2.23 EUR 27.08.2016 Place: 123456789XY
I need to process the file so I will have the values on the right side, 31.8.2016, -1.23, EUR, 1234.56, etc., stored in a MySQL database.
I only achieved returning either 1 occurrence of the line which contains a particular string or all the lines using find or find_all, but this is not sufficient as I somehow need to identify the block starting with "processing date:" and ending with "additional info:" and process the values there, then process next block, and next, until the end of the file.
Any hints how to achieve this?
I'd start with this:
File.foreach('data.txt', "\n\n") do |li|
next unless li[/^processing/]
puts "'#{li.strip}'"
end
If "data.txt" contains your content, foreach will read the file and return paragraphs, not lines, of text in li. Once you have those you can manipulate them as you need. This is very fast and efficient and doesn't have the scalability problems readlines or any read-based I/O could have.
This is the output:
'processing date: 31.8.2016
amount: -1.23
currency: EUR
balance: 1234.56
payer reference: /VS123456/SS0011223344/KS1212
type of the transaction: Some type of the transaction 1
additional info: Amount: 1.23 EUR 29.08.2016 Place: 123456789XY'
'processing date: 30.8.2016
amount: -2.23
currency: EUR
balance: 12345.56
payer reference: /VS123456/SS0011223344/KS1212
type of the transaction: Some type of the transaction 2
additional info: Amount: 2.23 EUR 28.08.2016 Place: 123456789XY'
'processing date: 29.8.2016
amount: -3.23
currency: EUR
balance: 123456.56
payer reference: /VS123456/SS0011223344/KS1212
type of the transaction: Some type of the transaction 2
additional info: Amount: 2.23 EUR 27.08.2016 Place: 123456789XY'
You can see by the wrapping ' that the file is being read in chunks or paragraphs delineated by "\n\n" then each chunk is stripped to remove trailing blanks.
See the foreach documentation for more information.
split(':', 2) is your friend:
'processing date: 31.8.2016'.split(':', 2) # => ["processing date", " 31.8.2016"]
'amount: -1.23'.split(':', 2) # => ["amount", " -1.23"]
'currency: EUR'.split(':', 2) # => ["currency", " EUR"]
'balance: 1234.56'.split(':', 2) # => ["balance", " 1234.56"]
'payer reference: /VS123456/SS0011223344/KS1212'.split(':', 2) # => ["payer reference", " /VS123456/SS0011223344/KS1212"]
'type of the transaction: Some type of the transaction 1'.split(':', 2) # => ["type of the transaction", " Some type of the transaction 1"]
'additional info: Amount: 1.23 EUR 29.08.2016 Place: 123456789XY'.split(':', 2) # => ["additional info", " Amount: 1.23 EUR 29.08.2016 Place: 123456789XY"]
From that you can do:
text = 'processing date: 31.8.2016
amount: -1.23
currency: EUR
balance: 1234.56
payer reference: /VS123456/SS0011223344/KS1212
type of the transaction: Some type of the transaction 1
additional info: Amount: 1.23 EUR 29.08.2016 Place: 123456789XY'
text.lines.map{ |li| li.split(':', 2).map(&:strip) }.to_h
# => {"processing date"=>"31.8.2016", "amount"=>"-1.23", "currency"=>"EUR", "balance"=>"1234.56", "payer reference"=>"/VS123456/SS0011223344/KS1212", "type of the transaction"=>"Some type of the transaction 1", "additional info"=>"Amount: 1.23 EUR 29.08.2016 Place: 123456789XY"}
There are a number of ways to continue parsing the information into more usable data but that's for you to figure out.

Repeat node value in YAML

pagination:
limit:
default: 10
min: 0
max: 50
current: default
The current node should have the same value as the default node? (in this case, 10). Is it possible to do that with YAML?
You can use an anchor for that, which is a token starting with & inserted before the scalar/mapping/sequence you want to "re-use". You "paste" it with an alias which is the same token preceded by a *.
pagination:
limit:
default: &def 10
min: 0
max: 50
current: *def
(you can use default instead of def but you don't have to use the same string as the key whose value you put an anchor on)

What is a regex to capture the total amount from strings? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I need to parse the total amount from different files. The layout of each file is different so the lines I need to parse vary.
What should be the regex for capturing from a sting a number that falls after "Total"?
It needs to be case insensitive and should consider the closest match after "Total". There can be anything before or after the word "Total", and I need the first number that comes after it.
For example:
from string "Service charges: 10 Total: 100 Shipping: 10"
from string "Service charges: 10 Total Amount: 100 Shipping: 10"
from string "Service charges: 10 Grand Total: 100 Shipping: 10"
from string "Service charges: 10 Total Amount (Rs.): 100 Shipping: 10"
The output should be 100 in all the above cases.
If all you're really asking about is a pattern match for various strings, look at using scan and grab the numeric strings:
[
"Service charges: 10 Total: 100 Shipping: 10",
"Service charges: 10 Total Amount: 100 Shipping: 10",
"Service charges: 10 Grand Total: 100 Shipping: 10",
"Service charges: 10 Total Amount (Rs.): 100 Shipping: 10",
].map{ |s| s.scan(/\d+/)[1] }
=> ["100", "100", "100", "100"]
This assumes you want the second number in each string.
If that order is going to change, which is unlikely because it looks like you're scanning invoices, then variations on the pattern and/or scan will work. This switches it up and uses a standard regex search based on the location of "Total", some possible intervening text, followed by ":" and the total value:
[
"Service charges: 10 Total: 100 Shipping: 10",
"Service charges: 10 Total Amount: 100 Shipping: 10",
"Service charges: 10 Grand Total: 100 Shipping: 10",
"Service charges: 10 Total Amount (Rs.): 100 Shipping: 10",
].map{ |s| s[/Total.*?: (\d+)/, 1] }
=> ["100", "100", "100", "100"]
To get the integer values append to_i inside the map statement:
[
"Service charges: 10 Total: 100 Shipping: 10",
"Service charges: 10 Total Amount: 100 Shipping: 10",
"Service charges: 10 Grand Total: 100 Shipping: 10",
"Service charges: 10 Total Amount (Rs.): 100 Shipping: 10",
].map{ |s| s[/Total.*?: (\d+)/, 1].to_i }
=> [100, 100, 100, 100]
For your example strings, it's probably preferable to use case-sensitive patterns to match "Total" unless you have knowledge that you will encounter "total" in lower-case. And, in that case, you should show such an example.
I think you can do this:
/Total[^:]*:\s+([0-9]+)/i
Explanation:
Total seach for "total"
[^:]* followed by anything or nothing until a colon ":" is found
:\s+ read over the colon and any following white space (maybe take * instead of +)
([0-9]+) read the numbers into a group for later retrieval -> 100
I am not sure how to indicate case insensitivity in the environment you use, but usually this can be done with some flags like I indicated with the i
here is a fiddle as an example
# assuming you have all your files ready in an array
a = ["Service charges: 10 Total: 100 Shipping: 10", "Service charges: 10 Total Amount: 100 Shipping: 10", "Service charges: 10 Grand Total: 100 Shipping: 10", "Service charges: 10 Total Amount (Rs.): 100 Shipping: 10"]
# we find every total with the following regexp
a.map {|s| s[/total[^\d]*(?<total>\d+)/i, 'total']}
#=> ["100", "100", "100", "100"]
The regexp is /total[^\d]*(?<total>\d*)/i. It looks for the word "total" and ignores any following character, until it finds a number (which it returns in a capture group). The i option makes it case insensitive.

Resources