I am using the Java Inflector library to convert singular forms to plurals, example : 2 boat => 2 boats.
However, it fails when the inputs are already plural.
1 boats => boats,
butterflies => butterflieses
Is there any other Java utility that -
1. Converts plurals to singular when necessary, example : 1 boat => boat
2. Retains plural as it is, if the plural form is required.
Thanks!
You know, you can make a simulation, first avoid to convert verbs that are already in plural, how? take the verb and convert it to singular, then convert it in plural and check if the result string is the same than the string that is in the input, if not, thats ok, know you know that is not in plural.
Related
I am using Javaslang-2.1.0-alpha and its Javaslang-match equivalent to do some object decomposition. According to this by blog post by Daniel in the "Match the Fancy way" section:
Match(person).of( Case(Person("Carl", Address($(), $())), (street, number) -> ...) )
Should retrieve values matching the two wildcard patterns inside Address into street and number but the example does not even compile. I later realized all objects must be wrapped inside atomic patterns i.e. "Carl" becomes $("Carl"). This was after reading this issue.
I followed the updated tutorial but there was no update to this example.
I updated the example to this:
Person person = new Person("Carl", new Address("Milkyway", 42));
String result2 = Match(person).of(
Case(Person($("Carl"), Address($(),$())),
(street, number) -> "Carl lives in " + street + " " + number),
Case($(), () -> "not found")
);
System.out.println(result2);
It compiles but my values are not being matched properly, judging from the console output:
Carl lives in Carl Address [street=Milkyway, number=42]
It's clear that street contains Carl and number, the entire Address object.
When I try to add a third lambda parameter to catch Carl:
Case(Person($("Carl"), Address($(),$())),
(name, street, number) -> "Carl lives in " + street + " " + number)
The code can't compile, the lambda expression gets a red underline with the following error text:
The target type of this expression must be a functional interface
There is no way of ignoring a value with $_ in the latest versions of javaslang-match. So I want to match each of the atomic patterns which would return three lambda parameters as above.
I need somebody who understands this library to explain to me how to do this object decomposition in the latest version.
Disclaimer: I'm the creator of Javaslang.
The case needs to handle (String, Address) -> {...}. The $() match arbitrary values but the handler/function receives only the first layer of the decomposed object tree. The $() are at the second layer.
Rule: All layers are matched against patterns, only the first layer is passed to the handler.
The first prototype of Match in fact handled arbitrary tree depths but methods hat to be generated under the hood for all possible combinations - max byte code size easily exceeded and compile time exponentially exploded to infinite.
The current version of Match is the only practical way in Java I see at the moment.
Update:
Please let me give a more figurative update on this topic.
We distinguish between
The object graph of the input
The pattern tree passed to the match case
The decomposed objects of the object graph
Ad 1) The Object Graph
Given an object, the object graph is spanned by traversing the properties (resp. instance variables) of that object. Notably we do not prohibit that an object contains cycles (e.g. a mutable list that contains itself).
In Javaslang there is no natural way how to decompose an object into its parts. We need a so-called pattern for that purpose.
Example of an object graph:
Person <-- root
/ \
"Carl" Address <-- 1st level
/ \
"Milkyway" 42 <-- 2nd level
Ad 2) The Pattern Tree
A pattern (instance) inherently defines how to decompose an object.
In our example the pattern types look like this (simplified generics):
Pattern2<Person, String, Address<String, Integer>>
/ \
Pattern0<String> Pattern2<Address, String, Integer>
/ \
Pattern0<String> Pattern0<Integer>
The called pattern methods return instances of the above types:
Person(...)
/ \
$("Carl") Address(...)
/ \
$() $()
Javaslang's Match API does the following:
The Match instance passes the given person object to the first Case.
The Case passes the person object to the pattern Person(...)
The Person(...) pattern checks if the given object person is of type Person.
If true then the pattern decomposes the object into its parts
(represented by a tuple) and checks if the sub-patterns $("Carl") and Address(...) match these parts (recursively repeats 3.)
If false, then Match passes the object to the next Case (see 2.)
If the pattern is atomic, i.e. it can't decompose the object any more, then equality is checked and the callers are informed all the way back to the match case.
When a match case got a pattern match then it passes the decomposed objects of the first level of the object graph to the match case handler.
Currently Java's type system does not allow us to pass matched objects of arbitrary object graph/tree levels in a typed way to the handler.
Ad 3) Decomposed Objects
We already mentioned object decomposition above in 2). In particular it is used when parts of our given objects are send down the pattern tree.
Because of the limitation of the type system we mentioned above, we separate the process of matching an object from the process of handling decomposed parts.
Java allows us to match arbitrary object graphs. We are not limited to any level here.
However, when an object successfully matched, we can only pass the decomposed objects of the first layer to the handler.
In our example these decomposed objects are name and address of the given person (and not street and number).
I know that this is not obvious to the user of the Match API.
One of the next Java versions will contain value objects and native pattern matching! However, that version of pattern matching will be limited entirely to the first level.
Javaslang allows to match arbitrary object graphs - but it has a price. The handler does receive only the first layer of decomposed objects, which might be confusing.
I hope this answered the question in an understandable way.
- Daniel
I am new to webharvest and am using it to get the article data from a website, using the following statement:
let $text := data($doc//div[#id="articleBody"])
and this is the data that I get from the above statement :
The Refine Spa (Furman's Mill) was built as a stone grist mill along the on a tributary of Capoolong Creek by Moore Furman, quartermaster general of George Washington's army
Notable people
Notable current and former residents of Pittstown include:
My question is that, is it possible to subtract a string from another
in the above example : "Notable people" from the content.
Is it possible to do this way? If its possible please let me know how. Thanks.
Is there something that I can do like this:
if (*contains*($text, 'Notable people')) then $text := *minus*($text, 'Notable people')
contains is a example function name to determine is a string is a substring of another,
and minus is a example function name to remove a substring from another
The desired output:
The Refine Spa (Furman's Mill) was built as a stone grist mill along the on a tributary of Capoolong Creek by Moore Furman, quartermaster general of George Washington's army
Notable current and former residents of Pittstown include:
From http://web-harvest.sourceforge.net/manual.php :
regexp
Searches the body for the given regular expression and optionally replaces found occurrences with specified pattern.
If body is a list of values then the regexp processor is applied to every item and final execution result is the list.
You just have to use correct regular expression a correct regexp-pattern and correct regexp-result
i am a newbie to flash builder! i am trying to create an app for IOS, on my school project. the problem that i faced is: can i select a few items value from a text input field and to implement them in a new view where i use some formulas! text input will look like this example :
aaa 23
bbb 45
ccc 56
i need the aaa and ccc that i want to use on my formulas!
about my first question! on my main view i have 2 buttons : 1 - Manual 2 - Auto . on manual view i have fields aka: aaa = ccc= ... and so on. in auto mode i was thinking of auto input method like this : to select the needed values for formulas from a text input area where the user will paste the text, for avoiding the manual input on each field.
PS:and can someone to tell me how correctly implement math simple formulas in flash builder, here i mean the syntax . like textinput1+textinput2/textinput3!?
From what I understand, you have two questions...
First, it doesn't quite make sense to me but I think you are asking if you can do math on letters??? No. But maybe I am just not understanding the question.
Second, you want to know how to do the math in ActionScript. Its fairly simple. Basic math functions are just like any other language. Take the string, convert it to a number then do your basic math:
Number(textInput1) + Number(textInput2) = result;
Using Number() will convert your string to a number so you can perform the math. Then, to display it, just convert your result back to a string:
textResultField.text = result.toString();
Or you could get fancy (AKA: harder to modify later) and do it all on one line:
textResultField.text = (Number(textInput1) + Number(textInput2)).toString();
Again, this will work for all BASIC math function (+, -, *, /). Really depends on what you are trying to do.
One of the components that I use needs to feed an XML into it. The component provider has not provided any documentation or the specs of the XML. I am trying to generate the XMLs by trial and error using the sample XMLs from the component.
This was the story. Here is my problem.
In the XML, they have used some f_key = "b3f39bb9-3f8c-453a-bdb4-2486a887e39f-0000a008:000001e8"
Java gives me this : UUID.randomUUID().toString()
which generates random strings in this format : "22572e59-f7dc-404a-9c0c-78161e3a4df7"
Any clue, what does "0000a008:000001e8" in the f_key provided by the component mean [The random string up to 5 pieces matches in both. The 6th and 7th piece are extra in the random string provided by the component]? What sort of UUID generator would be generating that? Does it look familiar?
According to this code
Regex guidRegEx = new Regex(#"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$");
guidRegEx.IsMatch("b3f39bb9-3f8c-453a-bdb4-2486a887e39f-0000a008:000001e8");
that isn't a valid guid, its a valid guid with something on the end. I am guessing they've tacked a timestamp on the end. I've seen stuff come out of timestamp appliances in the past.
But that is a best guess.
I believe that it is just some kind of key that is generated by the provider. Although I have no idea about the rules of the key generation (that is application specific) I translated hex numbers a008 1e8 to decimal view and found that the ratio between them is 83: 40968/488=83. So, probably try to create UUID and add suffix that consists of 2 numbers in hex format so that the ration of them is 83.
what type could contain a number like 2023209999 in java?
do you think that using a string type to represent a telephone number is a good idea?
Using a string is a very good idea. Remember that the point of OOP is that different types of data have different usage patterns. So let's look at a phone number patterns.
Do we add phone numbers or perform other arithmetic on them?
Do we split it based on length, match against parts of it, replace parts of it?
The answer to the first question is no. So we don't manipulate them like numbers. The answer to the second question is yes, so we manipulate them like strings.
Now, many here are advocating making phones a class by itself. There is merit in this regard, but I'm addressing the more pressing concern of how do you store the phone number, which is something you need to do no matter if a phone is a class or not. A phone class which stored its data as a number would not be a good fit.
I would write a PhoneNumber class, which uses String as an underlying storage, and adds validation / pretty formatting functionality.
I'd say a String at the least, but personally, I'd make a PhoneNumber object. It's the sort of thing that affords itself to extra methods such as:
boolean isValid();
PhoneNumberUtils.getCountry(PhoneNumber number);
PhoneNumberUtils.getState(PhoneNumber number);
...or whatever. One thing I'd be thinking out for is just letting people put in phone numbers and getting the system to learn the rest. I despise entering data that could be determined by the system. This is just my preference.
On a simpler level, just encapsulating the String in an PhoneNumber object gives your brain a handle ... in a week or so when your brain wonders "Where should this phone number method go?", you may find yourself with a quick answer.
I think that a dedicated PhoneNumber class is the way to go about it. Phone number are not just strings. First and foremost, phone numbers obey to rules, such as: they only contain digits, in the US they can contain either 7 or 10 digits. You'd need a constructor to make sure that your phone numbers are correct.
Second, a class will make it easy for you to steamline the differences between various formats. For instance, 555-4834 and 5554834 are different strings but are the same phone number.
Finally, you'd probably want methods such as: getAreaCode() or getLocalNumber() Calling such a method is much more concise and much less error prone than manipulating a String directly:
String phoneNumber pn = ....;
String localNumber = pn.length() == 7 ? pn : pn.substring(4) :
it's rather late, but I may add my 2 cents ....
I am in telecom and have made best experience to store phone numbers in structures (or objects) with character members of variable length, i.e.
struct TelephoneNumber (
InternationalPrefix VARCHAR;
AreaCode VARCHAR;
Subscriber VARCHAR;
Extension VARCHAR;)
I never store access digits (the zero's, double zeros, pluses etc.), they don't belong to to the telephone number per se but are part of what I may call "dialing rules"
struct DialingRule (
International VARCHAR;
National VARCHAR;
Local VARCHAR;)
typical values for DialingRule are "00", "0", NULL for a direct line, and "000", "00", "0" for a PBX requiring a "0 to get the line"
to "display" a number you can freely format the objects, insert hyphens, brackets or whatever you fancy
to create a dialable sequence I determine the type (international, national or local) by comparing the corresponding elements of the FROM and TO number and add the respective string from the dialing rule set as a prefix.
This all may sound like an overkill, but for international applications with strong requirements on data integrity and with strong links to hardware I didn't come up with any better. It removes ambiguities and the need for hardcoding lenghts etc. when you want to manipulate numbers. It's also easy to prefill parts of the structure from country / city lookup tables containing ISO country codes, IATA city codes and their corresponding prefixes.
Good luck
MikeD
In Germany area codes start with a 0 so a integer representation would lose that information.
Still I wouldn't recommend just using a String.
Instead use a Phonenumber class (or interface and implementations). This approach has some advantages.
If at some point you find that a String is insufficient you just have to change the Phonenumber class not every class that uses Phonenumbers.
Additionally it would allow you to seperate area code and number internally.
If you need to record leading 0 or + for international then you should use a String.
If you ain't worried about these you can just use a long. e.g.
long phoneNumber = 2023209999L; // the L is for a long constant.
It would honestly come down to what you plan to do with it. If you just want to store it to reprint it a string or maybe even a long would be fine, assuming we are dealing with US numbers.
If you want to do something more sophisticated making a Class with containing several strings, one for each component.
Basically just not enough info here to make a real decision.
Using a String Type allows you to break apart the phone number without any voodoo or casting.
For instance, if you change the format of how you accept phone numbers, you'll have to do extra work to get the phone number contatenated if it is a long instead of a string.
Another thing to consider: an int would be too small. the maximum value an int can hold is 2,147,483,647. as far as primitives are concerned, long is your best bet.
It depends on what you're doing. Let's say you want to be able to represent international numbers, local numbers, branch exchange numbers, etc. In this case, a String is a bad choice. It doesn't have any meta-information. You probably want a class to represent a phone number.
As far as what you use to represent the phone number in this class, you could use a BigInteger (to allow you to have international numbers), or more simply, you could store each portion of a phone number as a long.