Java Stanford NLP: Part of Speech labels? - java

The Stanford NLP, demo'd here, gives an output like this:
Colorless/JJ green/JJ ideas/NNS sleep/VBP furiously/RB ./.
What do the Part of Speech tags mean? I am unable to find an official list. Is it Stanford's own system, or are they using universal tags? (What is JJ, for instance?)
Also, when I am iterating through the sentences, looking for nouns, for instance, I end up doing something like checking to see if the tag .contains('N'). This feels pretty weak. Is there a better way to programmatically search for a certain part of speech?

The Penn Treebank Project. Look at the Part-of-speech tagging ps.
JJ is adjective. NNS is noun, plural. VBP is verb present tense. RB is adverb.
That's for english. For chinese, it's the Penn Chinese Treebank. And for german it's the NEGRA corpus.
CC Coordinating conjunction
CD Cardinal number
DT Determiner
EX Existential there
FW Foreign word
IN Preposition or subordinating conjunction
JJ Adjective
JJR Adjective, comparative
JJS Adjective, superlative
LS List item marker
MD Modal
NN Noun, singular or mass
NNS Noun, plural
NNP Proper noun, singular
NNPS Proper noun, plural
PDT Predeterminer
POS Possessive ending
PRP Personal pronoun
PRP$ Possessive pronoun
RB Adverb
RBR Adverb, comparative
RBS Adverb, superlative
RP Particle
SYM Symbol
TO to
UH Interjection
VB Verb, base form
VBD Verb, past tense
VBG Verb, gerund or present participle
VBN Verb, past participle
VBP Verb, non­3rd person singular present
VBZ Verb, 3rd person singular present
WDT Wh­determiner
WP Wh­pronoun
WP$ Possessive wh­pronoun
WRB Wh­adverb

Explanation of each tag from the documentation :
CC: conjunction, coordinating
& 'n and both but either et for less minus neither nor or plus so
therefore times v. versus vs. whether yet
CD: numeral, cardinal
mid-1890 nine-thirty forty-two one-tenth ten million 0.5 one forty-
seven 1987 twenty '79 zero two 78-degrees eighty-four IX '60s .025
fifteen 271,124 dozen quintillion DM2,000 ...
DT: determiner
all an another any both del each either every half la many much nary
neither no some such that the them these this those
EX: existential there
there
FW: foreign word
gemeinschaft hund ich jeux habeas Haementeria Herr K'ang-si vous
lutihaw alai je jour objets salutaris fille quibusdam pas trop Monte
terram fiche oui corporis ...
IN: preposition or conjunction, subordinating
astride among uppon whether out inside pro despite on by throughout
below within for towards near behind atop around if like until below
next into if beside ...
JJ: adjective or numeral, ordinal
third ill-mannered pre-war regrettable oiled calamitous first separable
ectoplasmic battery-powered participatory fourth still-to-be-named
multilingual multi-disciplinary ...
JJR: adjective, comparative
bleaker braver breezier briefer brighter brisker broader bumper busier
calmer cheaper choosier cleaner clearer closer colder commoner costlier
cozier creamier crunchier cuter ...
JJS: adjective, superlative
calmest cheapest choicest classiest cleanest clearest closest commonest
corniest costliest crassest creepiest crudest cutest darkest deadliest
dearest deepest densest dinkiest ...
LS: list item marker
A A. B B. C C. D E F First G H I J K One SP-44001 SP-44002 SP-44005
SP-44007 Second Third Three Two * a b c d first five four one six three
two
MD: modal auxiliary
can cannot could couldn't dare may might must need ought shall should
shouldn't will would
NN: noun, common, singular or mass
common-carrier cabbage knuckle-duster Casino afghan shed thermostat
investment slide humour falloff slick wind hyena override subhumanity
machinist ...
NNS: noun, common, plural
undergraduates scotches bric-a-brac products bodyguards facets coasts
divestitures storehouses designs clubs fragrances averages
subjectivists apprehensions muses factory-jobs ...
NNP: noun, proper, singular
Motown Venneboerger Czestochwa Ranzer Conchita Trumplane Christos
Oceanside Escobar Kreisler Sawyer Cougar Yvette Ervin ODI Darryl CTCA
Shannon A.K.C. Meltex Liverpool ...
NNPS: noun, proper, plural
Americans Americas Amharas Amityvilles Amusements Anarcho-Syndicalists
Andalusians Andes Andruses Angels Animals Anthony Antilles Antiques
Apache Apaches Apocrypha ...
PDT: pre-determiner
all both half many quite such sure this
POS: genitive marker
' 's
PRP: pronoun, personal
hers herself him himself hisself it itself me myself one oneself ours
ourselves ownself self she thee theirs them themselves they thou thy us
PRP$: pronoun, possessive
her his mine my our ours their thy your
RB: adverb
occasionally unabatingly maddeningly adventurously professedly
stirringly prominently technologically magisterially predominately
swiftly fiscally pitilessly ...
RBR: adverb, comparative
further gloomier grander graver greater grimmer harder harsher
healthier heavier higher however larger later leaner lengthier less-
perfectly lesser lonelier longer louder lower more ...
RBS: adverb, superlative
best biggest bluntest earliest farthest first furthest hardest
heartiest highest largest least less most nearest second tightest worst
RP: particle
aboard about across along apart around aside at away back before behind
by crop down ever fast for forth from go high i.e. in into just later
low more off on open out over per pie raising start teeth that through
under unto up up-pp upon whole with you
SYM: symbol
% & ' '' ''. ) ). * + ,. < = > # A[fj] U.S U.S.S.R * ** ***
TO: "to" as preposition or infinitive marker
to
UH: interjection
Goodbye Goody Gosh Wow Jeepers Jee-sus Hubba Hey Kee-reist Oops amen
huh howdy uh dammit whammo shucks heck anyways whodunnit honey golly
man baby diddle hush sonuvabitch ...
VB: verb, base form
ask assemble assess assign assume atone attention avoid bake balkanize
bank begin behold believe bend benefit bevel beware bless boil bomb
boost brace break bring broil brush build ...
VBD: verb, past tense
dipped pleaded swiped regummed soaked tidied convened halted registered
cushioned exacted snubbed strode aimed adopted belied figgered
speculated wore appreciated contemplated ...
VBG: verb, present participle or gerund
telegraphing stirring focusing angering judging stalling lactating
hankerin' alleging veering capping approaching traveling besieging
encrypting interrupting erasing wincing ...
VBN: verb, past participle
multihulled dilapidated aerosolized chaired languished panelized used
experimented flourished imitated reunifed factored condensed sheared
unsettled primed dubbed desired ...
VBP: verb, present tense, not 3rd person singular
predominate wrap resort sue twist spill cure lengthen brush terminate
appear tend stray glisten obtain comprise detest tease attract
emphasize mold postpone sever return wag ...
VBZ: verb, present tense, 3rd person singular
bases reconstructs marks mixes displeases seals carps weaves snatches
slumps stretches authorizes smolders pictures emerges stockpiles
seduces fizzes uses bolsters slaps speaks pleads ...
WDT: WH-determiner
that what whatever which whichever
WP: WH-pronoun
that what whatever whatsoever which who whom whosoever
WP$: WH-pronoun, possessive
whose
WRB: Wh-adverb
how however whence whenever where whereby whereever wherein whereof why

The accepted answer above is missing the following information:
There are also 9 punctuation tags defined (which are not listed in some references, see here). These are:
#
$
'' (used for all forms of closing quote)
( (used for all forms of opening parenthesis)
) (used for all forms of closing parenthesis)
,
. (used for all sentence-ending punctuation)
: (used for colons, semicolons and ellipses)
`` (used for all forms of opening quote)

Here is a more complete list of tags for the Penn Treebank (posted here for the sake of completness):
http://www.surdeanu.info/mihai/teaching/ista555-fall13/readings/PennTreebankConstituents.html
It also includes tags for clause and phrase levels.
Clause Level
- S
- SBAR
- SBARQ
- SINV
- SQ
Phrase Level
- ADJP
- ADVP
- CONJP
- FRAG
- INTJ
- LST
- NAC
- NP
- NX
- PP
- PRN
- PRT
- QP
- RRC
- UCP
- VP
- WHADJP
- WHAVP
- WHNP
- WHPP
- X
(descriptions in the link)

Codified:
/**
* Represents the English parts-of-speech, encoded using the
* de facto <a href="http://www.cis.upenn.edu/~treebank/">Penn Treebank
* Project</a> standard.
*
* #see Penn Treebank Specification
*/
public enum PartOfSpeech {
ADJECTIVE( "JJ" ),
ADJECTIVE_COMPARATIVE( ADJECTIVE + "R" ),
ADJECTIVE_SUPERLATIVE( ADJECTIVE + "S" ),
/* This category includes most words that end in -ly as well as degree
* words like quite, too and very, posthead modi ers like enough and
* indeed (as in good enough, very well indeed), and negative markers like
* not, n't and never.
*/
ADVERB( "RB" ),
/* Adverbs with the comparative ending -er but without a strictly comparative
* meaning, like <i>later</i> in <i>We can always come by later</i>, should
* simply be tagged as RB.
*/
ADVERB_COMPARATIVE( ADVERB + "R" ),
ADVERB_SUPERLATIVE( ADVERB + "S" ),
/* This category includes how, where, why, etc.
*/
ADVERB_WH( "W" + ADVERB ),
/* This category includes and, but, nor, or, yet (as in Y et it's cheap,
* cheap yet good), as well as the mathematical operators plus, minus, less,
* times (in the sense of "multiplied by") and over (in the sense of "divided
* by"), when they are spelled out. <i>For</i> in the sense of "because" is
* a coordinating conjunction (CC) rather than a subordinating conjunction.
*/
CONJUNCTION_COORDINATING( "CC" ),
CONJUNCTION_SUBORDINATING( "IN" ),
CARDINAL_NUMBER( "CD" ),
DETERMINER( "DT" ),
/* This category includes which, as well as that when it is used as a
* relative pronoun.
*/
DETERMINER_WH( "W" + DETERMINER ),
EXISTENTIAL_THERE( "EX" ),
FOREIGN_WORD( "FW" ),
LIST_ITEM_MARKER( "LS" ),
NOUN( "NN" ),
NOUN_PLURAL( NOUN + "S" ),
NOUN_PROPER_SINGULAR( NOUN + "P" ),
NOUN_PROPER_PLURAL( NOUN + "PS" ),
PREDETERMINER( "PDT" ),
POSSESSIVE_ENDING( "POS" ),
PRONOUN_PERSONAL( "PRP" ),
PRONOUN_POSSESSIVE( "PRP$" ),
/* This category includes the wh-word whose.
*/
PRONOUN_POSSESSIVE_WH( "WP$" ),
/* This category includes what, who and whom.
*/
PRONOUN_WH( "WP" ),
PARTICLE( "RP" ),
/* This tag should be used for mathematical, scientific and technical symbols
* or expressions that aren't English words. It should not used for any and
* all technical expressions. For instance, the names of chemicals, units of
* measurements (including abbreviations thereof) and the like should be
* tagged as nouns.
*/
SYMBOL( "SYM" ),
TO( "TO" ),
/* This category includes my (as in M y, what a gorgeous day), oh, please,
* see (as in See, it's like this), uh, well and yes, among others.
*/
INTERJECTION( "UH" ),
VERB( "VB" ),
VERB_PAST_TENSE( VERB + "D" ),
VERB_PARTICIPLE_PRESENT( VERB + "G" ),
VERB_PARTICIPLE_PAST( VERB + "N" ),
VERB_SINGULAR_PRESENT_NONTHIRD_PERSON( VERB + "P" ),
VERB_SINGULAR_PRESENT_THIRD_PERSON( VERB + "Z" ),
/* This category includes all verbs that don't take an -s ending in the
* third person singular present: can, could, (dare), may, might, must,
* ought, shall, should, will, would.
*/
VERB_MODAL( "MD" ),
/* Stanford.
*/
SENTENCE_TERMINATOR( "." );
private final String tag;
private PartOfSpeech( String tag ) {
this.tag = tag;
}
/**
* Returns the encoding for this part-of-speech.
*
* #return A string representing a Penn Treebank encoding for an English
* part-of-speech.
*/
public String toString() {
return getTag();
}
protected String getTag() {
return this.tag;
}
public static PartOfSpeech get( String value ) {
for( PartOfSpeech v : values() ) {
if( value.equals( v.getTag() ) ) {
return v;
}
}
throw new IllegalArgumentException( "Unknown part of speech: '" + value + "'." );
}
}

I am providing the whole list here and also giving reference link
1. CC Coordinating conjunction
2. CD Cardinal number
3. DT Determiner
4. EX Existential there
5. FW Foreign word
6. IN Preposition or subordinating conjunction
7. JJ Adjective
8. JJR Adjective, comparative
9. JJS Adjective, superlative
10. LS List item marker
11. MD Modal
12. NN Noun, singular or mass
13. NNS Noun, plural
14. NNP Proper noun, singular
15. NNPS Proper noun, plural
16. PDT Predeterminer
17. POS Possessive ending
18. PRP Personal pronoun
19. PRP$ Possessive pronoun
20. RB Adverb
21. RBR Adverb, comparative
22. RBS Adverb, superlative
23. RP Particle
24. SYM Symbol
25. TO to
26. UH Interjection
27. VB Verb, base form
28. VBD Verb, past tense
29. VBG Verb, gerund or present participle
30. VBN Verb, past participle
31. VBP Verb, non-3rd person singular present
32. VBZ Verb, 3rd person singular present
33. WDT Wh-determiner
34. WP Wh-pronoun
35. WP$ Possessive wh-pronoun
36. WRB Wh-adverb
You can find out the whole list of Parts of Speech tags here.

Regarding your second question of finding particular POS (e.g., Noun) tagged word/chunk, here is the sample code you can follow.
public static void main(String[] args) {
Properties properties = new Properties();
properties.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse");
StanfordCoreNLP pipeline = new StanfordCoreNLP(properties);
String input = "Colorless green ideas sleep furiously.";
Annotation annotation = pipeline.process(input);
List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
List<String> output = new ArrayList<>();
String regex = "([{pos:/NN|NNS|NNP/}])"; //Noun
for (CoreMap sentence : sentences) {
List<CoreLabel> tokens = sentence.get(CoreAnnotations.TokensAnnotation.class);
TokenSequencePattern pattern = TokenSequencePattern.compile(regex);
TokenSequenceMatcher matcher = pattern.getMatcher(tokens);
while (matcher.find()) {
output.add(matcher.group());
}
}
System.out.println("Input: "+input);
System.out.println("Output: "+output);
}
The output is:
Input: Colorless green ideas sleep furiously.
Output: [ideas]

They seem to be Brown Corpus tags.

Stanford CoreNLP Tags for Other Languages : French, Spanish, German ...
I see you use the parser for English language, which is the default model.
You may use the parser for other languages (French, Spanish, German ...) and, be aware, both tokenizers and part of speech taggers are different for each language. If you want to do that, you must download the specific model for the language (using a builder like Maven for example) and then set the model you want to use.
Here you have more information about that.
Here you are lists of tags for different languages :
Stanford CoreNLP POS Tags for Spanish
Stanford CoreNLP POS Tagger for German uses the Stuttgart-Tübingen Tag Set (STTS)
Stanford CoreNLP POS tagger for French uses the following tags:
TAGS FOR FRENCH:
Part of Speech Tags for French
A (adjective)
Adv (adverb)
CC (coordinating conjunction)
Cl (weak clitic pronoun)
CS (subordinating conjunction)
D (determiner)
ET (foreign word)
I (interjection)
NC (common noun)
NP (proper noun)
P (preposition)
PREF (prefix)
PRO (strong pronoun)
V (verb)
PONCT (punctuation mark)
Phrasal Categories Tags for French:
AP (adjectival phrases)
AdP (adverbial phrases)
COORD (coordinated phrases)
NP (noun phrases)
PP (prepositional phrases)
VN (verbal nucleus)
VPinf (infinitive clauses)
VPpart (nonfinite clauses)
SENT (sentences)
Sint, Srel, Ssub (finite clauses)
Syntactic Functions for French:
SUJ (subject)
OBJ (direct object)
ATS (predicative complement of a subject)
ATO (predicative complement of a direct object)
MOD (modifier or adjunct)
A-OBJ (indirect complement introduced by à)
DE-OBJ (indirect complement introduced by de)
P-OBJ (indirect complement introduced by another preposition)

In spacy it was very fast i think, in just a low-end notebook it will run like this :
import spacy
import time
start = time.time()
with open('d:/dictionary/e-store.txt') as f:
input = f.read()
word = 0
result = []
nlp = spacy.load("en_core_web_sm")
doc = nlp(input)
for token in doc:
if token.pos_ == "NOUN":
result.append(token.text)
word += 1
elapsed = time.time() - start
print("From", word, "words, there is", len(result), "NOUN found in", elapsed, "seconds")
The Output in several trial :
From 3547 words, there is 913 NOUN found in 7.768507719039917 seconds
From 3547 words, there is 913 NOUN found in 7.408619403839111 seconds
From 3547 words, there is 913 NOUN found in 7.431427955627441 seconds
So, I think you don't need to worry about the looping for each POS tag check :)
More improvement I got when disabled certain pipeline :
nlp = spacy.load("en_core_web_sm", disable = 'ner')
So, The result is faster :
From 3547 words, there is 913 NOUN found in 6.212834596633911 seconds
From 3547 words, there is 913 NOUN found in 6.257707595825195 seconds
From 3547 words, there is 913 NOUN found in 6.371225833892822 seconds

Related

How to conduct nested named entity recognition in OpenNLP?

I am currently working on a java web server project, that requires the use of Natural Language processing, specifically Named Entity Recognition (NER).
I was using OpenNLP for java, since it was easy to add custom training data. It works perfectly.
However, I need to also be able to extract entites inside of entities (Nested named entity recognition). I tried doing this in OpenNLP, but I got parsing errors. So my guess is that OpenNLP sadly does not support nested entities.
Here is an example of what I need to parse:
Remind me to [START:reminder] give some presents to [START:contact] John [END] and [START:contact] Charlie [END][END].
If this cannot be achieved with OpenNLP, is there any other Java NLP Library that could do this. If there are no Java libraries at all, are there any NLP libraries in any other language that can do this?
Please help. Thanks!
The short answer is:
This cannot be achieved using openNLP NER which is suitable only for continuous entities because it use a BIO tagging scheme.
I don't know any library in any language capable of do this.
I think you are extending too much the concept of entity, which is habitually associated with persons, places, organizations, gene names etc.
But not with the identification of complex structures within text.
For that purpose you need to think in a more elaborated solution, taking into account the grammatical structure of the sentence, which can be obtained using a parser like the one in OpenNLP, and maybe combine this with the output of the NER process.
For the purpose of Name Entity Recognition (Java based) I use the following:
Apache UIMA
ClearTK
https://github.com/merishav/cleartk-tutorials
You can train models for your use case, I have already trained for NER for person, places, date of birth, profession.
ClearTK gives you a wrapper on MalletCRFClassifier.
Use this python source code (Python 3) https://gist.github.com/ttpro1995/cd8c60cfc72416a02713bb93dff9ae6f
It's create multiple un-nest version of nest data for you.
For input sentence below ( input data must be tokenized first, so there are space between and thing around it)
Remind me to <START:reminder> give some presents to <START:contact> John <END> and <START:contact> Charlie <END> <END> .
It output multiple sentence with different nest level.
Remind me to give some presents to John and Charlie .
Remind me to <START:reminder> give some presents to John and Charlie <END> .
Remind me to give some presents to <START:contact> John <END> and <START:contact> Charlie <END> .
Full source code here for quick copy-paste
import sys
END_TAG = 0
START_TAG = 1
NOT_TAG = -1
def detect_tag(in_token):
"""
detect tag in token
:param in_token:
:return:
"""
if "<START:" in in_token:
return START_TAG
elif "<END>" == in_token:
return END_TAG
return NOT_TAG
def remove_nest_tag(in_str):
"""
với <START:ORGANIZATION> Sở Cảnh sát Phòng cháy , chữa cháy ( PCCC ) và cứu nạn , cứu hộ <START:LOCATION> Hà Nội <END> <END>
:param in_str:
:return:
"""
state = 0
taglist = []
tag_dict = dict()
sentence_token = in_str.split()
## detect token tag
max_nest = 0
for index, token in enumerate(sentence_token):
# print(token + str(detect_tag(token)))
tag = detect_tag(token)
if tag > 0:
state += 1
if max_nest < state:
max_nest = state
token_info = (index, state, token)
taglist.append(token_info)
tag_dict[index] = token_info
elif tag == 0:
token_info = (index, state, token)
taglist.append(token_info)
tag_dict[index] = token_info
state -= 1
generate_sentences = []
for state in range(max_nest+1):
generate_sentence_token = []
for index, token in enumerate(sentence_token):
if detect_tag(token) >= 0: # is a tag
token_info = tag_dict[index]
if token_info[1] == state:
generate_sentence_token.append(token)
elif detect_tag(token) == -1 : # not a tag
generate_sentence_token.append(token)
sentence = ' '.join(generate_sentence_token)
generate_sentences.append(sentence)
return generate_sentences
# generate sentence
print(taglist)
def test():
tstr2 = "Remind me to <START:reminder> give some presents to <START:contact> John <END> and <START:contact> Charlie <END> <END> ."
result = remove_nest_tag(tstr2)
print("-----")
for sentence in result:
print(sentence)
if __name__ == "__main__":
"""
un-nest dataset for opennlp name
"""
# test()
# test()
if len(sys.argv) > 1:
inpath = sys.argv[1]
infile = open(inpath, 'r')
outfile = open(inpath+".out", 'w')
for line in infile:
sentences = remove_nest_tag(line)
for sentence in sentences:
outfile.write(sentence+"\n")
outfile.close()
else:
print("usage: python unnest_data.py input.txt")

How to extract sub-strings for a collection of text?

I extracted text from pdf document. .. I want to extract some particular fields in it using java..
The portion of text ..
US00RE44697E (i9) United States (12) Reissued Patent (10)
Patent Number: RE44,697 E Jones et al. (45) Date of
ReissuedPatent: Jan. 7, 2014 (54) ENCRYPTIONPROCESSORWITH SHARED
MEMORY INTERCONNECT (75) Inventors: David E.Jones, Ottawa
(CA); Cormac M.O'Connell, Carp (CA) (73) Assignee: Mosaid
Technologies Incorporated, Ottawa, Ontario (CA) (21)
Appl.No.: 13/603,137 (22) Filed: Sep. 4, 2012 Related U.S.
Patent Documents Reissue of: (64) Patent No.: Issued:
Appl. No.: Filed: 6,088,800 Jul. 11, 2000
09/032,029 Feb. 27, 1998 (51) Int.CI. G06F 21/00
(2013.01) (52) U.S. CI. USPC .............713/189; 713/190;
713/193; 380/28; 380/33; 380/52 (58) Field of Classification
Search None
Now my mission is to extract fields form it and give to strings.. that is
the text (10) Patent Number: RE44,697 E will be extracted as String pat_no= " RE44,697 E"
the text (54) ENCRYPTIONPROCESSORWITH SHARED
MEMORY INTERCONNECT will be extracted as String title= "ENCRYPTIONPROCESSORWITH SHARED
MEMORY INTERCONNECT"
the extremely irregular text block
(64) Patent No.: Issued: Appl. No.: Filed:
6,088,800 Jul. 11, 2000 09/032,029 Feb. 27, 1998
have to be extracted as
String pat_no_org = "6,088,800";
String issued = "jul.11,2000"
String filed = "feb 27 ,1998"
......
like this..
My Works
First i used the string.split , string.substring , string,indexof and even apache string utils , but none helped.. Because the text are scattered , above methods doesn't helped.. I also tried regular expressions ,but since I very weak in it I can't program .
Please tell me how to achieve my objective using java ?
With regex, I would split it in 3 parts:
1.) (10) Patent Number the regex could look like this:
\(10\)\s*Patent Number:\s*([\w,]+)
as a java string:
"\\(10\\)\\s*Patent Number:\\s*([\\w,]+)"
The matches for the first parenthesized group will be in [1].
\s is a shorthand for [ \t\r\n\f] any kind of white-space.
\w is a shorthand for [A-Za-z0-9_] word-characters, together with , in a character class.
Some characters have special meanings in regex. They have to be escaped with a backslash.
2.) (54) ENCRYPT...
A pattern could look like:
(?s)\(54\)\s*(.*?)\s*(?=\(\d|$\))
as a java string:
"(?s)\\(54\\)\\s*(.*?)\\s*(?=\\(\\d|$\\))"
(?s) The s modifier equals Pattern.DOTALL where the dot matches new-lines too.
(?=\(\d|$\)) a lookahead is used, to match (.*?) lazy any amount of any characters until another ( followed by a digit | or string-end $ (anchor for end) is seen.
3.) For the other desired 3 parts I would try to reflect formatting of the input with the pattern. This requires, that all data is constructed compatible. A pattern could look like this:
(?s)\(64\).*?Filed:\s*([\d,]+)\s*(\w+\.\s*\d+,\s*\d+)\s*\n[\d+][^\n]+\n\s*(\w+\.\s*\d+,\s*\d+)
as a java string:
"(?s)\\(64\\).*?Filed:\\s*([\\d,]+)\\s*(\\w+\\.\\s*\\d+,\\s*\\d+)\\s*\\n[\\d+][^\\n]+\\n\\s*(\\w+\\.\\s*\\d+,\\s*\\d+)"
\n matches a newline.
Matches will be in [1] e.g. 6,088,800, [2] e.g. Jul. 11, 2000 and [3] e.g. Feb. 27, 1998.
For getting started with regex, this is too much information at once :)

Sparql query with java

A have an RDF file and I want to make a select SPARQL query that will select descriptions of a specific author. I tried a query like below but with no success. I am new to SPARQL so if anyone can help I would appreciate it…
MY TRY:
String query = "PREFIX schema: <"+Constants.SCHEMA+"> \n" +
"SELECT ?description \n" +
"WHERE { \n" +
"?review a schema:Review; \n" +
"schema:author \"Judson C.\"; \n" +
"}";
The RDF file looks like this:
<rdf:Description rdf:nodeID="A3">
<schema:ratingValue>5.0</schema:ratingValue>
<rdf:type rdf:resource="http://schema.org/Rating" />
</rdf:Description>
<rdf:Description rdf:nodeID="A4">
< schema:reviewRating rdf:nodeID="A5"/>
<schema:description>OMG Love this place!! But glad I don't have to
stand in line for an hour like back in the day to get my fix!</schema:description>
<schema:datePublished>2013-05-18</schema:datePublished>
<schema:author>Judson C.</schema:author>
<rdf:type rdf:resource="http://schema.org/Review" />
</rdf:Description>
<rdf:Description rdf:nodeID="A6">
<schema:reviewRating rdf:nodeID="A7" />
<schema:description>If you're ever wondering of where to stuff your
face with a good ol' sandwich, you'll definitely have to pop in at
Ike's Place. Not only are the different sandwiches uniquely named,
they're also uniquely flavor profiled. A wonderful experience
indeed!Sandwiches I've personally ordered are the Love Triangle,
Nacho girl, and the Al Bundy. For all you people out there who loved
the tv show, Married with Children, I think you could appreciate the
sentiment of having a sammie named after the 'No Ma'am enthusiast. I
know I did. Ahhh... *tucks hand under pant waistband*All kidding
aside, these sandwiches are pretty legit. You go to the counter and
order whichever sandwich sounds appealing and they can either add
chips or a drink to your meal. Simple. They have a wide range of
sandwiches that are made to please any meat lover, vegetarian, or
vegan palate alike. The huge variety is an A+ in my book. They can
even individualize and customize your sandwich to your liking. This
means that even the pickiest of eaters are welcome!So if you're
interested in a fun place to enjoy a good sandwich, most definitely
give 'em a try!</schema:description>
<schema:datePublished>2013-04-30</schema:datePublished>
<schema:author>Ann S.</schema:author>
<rdf:type rdf:resource="http://schema.org/Review" />
</rdf:Description>
Short Answer
The short answer is that the query isn't syntactically well formed, and that it doesn't return the things you're looking for. The query needs to terminate its triple pattern with a full stop (.), not a semicolon (;). The fixed query should look something like this:
PREFIX schema: <http://schema.org/>
SELECT ?description
WHERE {
?review a schema:Review ;
schema:author "Judson C." .
}
However, this SELECTs the variable ?description which isn't used in the query, so it's always empty. You probably either want to SELECT ?review or else add to the pattern schema:description ?description.
Long Answer
A bit more detail that shows that this works follows. The RDF you provided isn't well formed, so it's hard to test against it. I took what you provided, and wrote it up as N3 to get the following. (RDF/XML is at the end of the answer, too.)
#prefix schema: <http://schema.org/> .
[]
a schema:Rating ;
schema:ratingValue "5.0" .
[]
a schema:Review ;
schema:reviewRating [] ;
schema:description "OMG Love this place!! But glad I don't have to stand in line for an hour like back in the day to get my fix!" ;
schema:datePublished "2013-05-18" ;
schema:author "Judson C." .
[]
a schema:Review ;
schema:reviewRating [] ;
schema:description "If you're ever wondering of where to stuff your face with a good ol' sandwich, you'll definitely have to pop in at Ike's Place. Not only are the different sandwiches uniquely named, they're also uniquely flavor profiled. A wonderful experience indeed!Sandwiches I've personally ordered are the Love Triangle, Nacho girl, and the Al Bundy. For all you people out there who loved the tv show, Married with Children, I think you could appreciate the sentiment of having a sammie named after the 'No Ma'am enthusiast. I know I did. Ahhh... *tucks hand under pant waistband*All kidding aside, these sandwiches are pretty legit. You go to the counter and order whichever sandwich sounds appealing and they can either add chips or a drink to your meal. Simple. They have a wide range of sandwiches that are made to please any meat lover, vegetarian, or vegan palate alike. The huge variety is an A+ in my book. They can even individualize and customize your sandwich to your liking. This means that even the pickiest of eaters are welcome!So if you're interested in a fun place to enjoy a good sandwich, most definitely give 'em a try!" ;
schema:datePublished "2013-04-30" ;
schema:author "Ann S." .
The query, as I mentioned above, has some syntax errors; the triple pattern should end with a ., not a ;, and should be:
PREFIX schema: <http://schema.org/>
SELECT ?description
WHERE {
?review a schema:Review ;
schema:author "Judson C." .
}
This doesn't return anything useful though, because ?description isn't used in the query. Using Jena's ARQ (and writing the query as schema.query) we output with one row (since the pattern matched), but with an unbound variable:
$ /usr/local/lib/apache-jena-2.10.0/bin/arq \
--query schema.query \
--data schema.n3
---------------
| description |
===============
| |
---------------
Here's a query that binds ?description:
PREFIX schema: <http://schema.org/>
SELECT ?description
WHERE {
?review a schema:Review ;
schema:author "Judson C." ;
schema:description ?description .
}
As expected, it gives some useful results:
$ /usr/local/lib/apache-jena-2.10.0/bin/arq \
--query schema2.query \
--data schema.n3
-------------------------------------------------------------------------------------------------------------------
| description |
===================================================================================================================
| "OMG Love this place!! But glad I don't have to stand in line for an hour like back in the day to get my fix!" |
-------------------------------------------------------------------------------------------------------------------
RDF/XML Appendix
Here's the RDF/XML serialization of the data at hand.
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:schema="http://schema.org/">
<schema:Review>
<schema:reviewRating rdf:parseType="Resource">
</schema:reviewRating>
<schema:description>If you're ever wondering of where to stuff your face with a good ol' sandwich, you'll definitely have to pop in at Ike's Place. Not only are the different sandwiches uniquely named, they're also uniquely flavor profiled. A wonderful experience indeed!Sandwiches I've personally ordered are the Love Triangle, Nacho girl, and the Al Bundy. For all you people out there who loved the tv show, Married with Children, I think you could appreciate the sentiment of having a sammie named after the 'No Ma'am enthusiast. I know I did. Ahhh... *tucks hand under pant waistband*All kidding aside, these sandwiches are pretty legit. You go to the counter and order whichever sandwich sounds appealing and they can either add chips or a drink to your meal. Simple. They have a wide range of sandwiches that are made to please any meat lover, vegetarian, or vegan palate alike. The huge variety is an A+ in my book. They can even individualize and customize your sandwich to your liking. This means that even the pickiest of eaters are welcome!So if you're interested in a fun place to enjoy a good sandwich, most definitely give 'em a try!</schema:description>
<schema:datePublished>2013-04-30</schema:datePublished>
<schema:author>Ann S.</schema:author>
</schema:Review>
<schema:Review>
<schema:reviewRating rdf:parseType="Resource">
</schema:reviewRating>
<schema:description>OMG Love this place!! But glad I don't have to stand in line for an hour like back in the day to get my fix!</schema:description>
<schema:datePublished>2013-05-18</schema:datePublished>
<schema:author>Judson C.</schema:author>
</schema:Review>
<schema:Rating>
<schema:ratingValue>5.0</schema:ratingValue>
</schema:Rating>
</rdf:RDF>
I suppose the question is really on SPARQL, not java.
Have you tried if works writing:
?review schema:author "Judson C."^^xsd:string
or:
?review schema:author ?author:
FILTER(str(?author)=="Judson C.")

Java String formatting solution

I have a string description of a company, which is nasty written by different users (hand-typed). Here is a example (focus on the dots, spaces, first letters etc..):
XXXX is a Global menagement consulting,Technology services and
outsourcing company, with 257000people serving clients in more than
120 countries.. combining unparalleled experience, comprehensive
capabilities across all industries and business functions,and
extensive research on the worlds most successfull companies, XXXX
collaborates with clients to help them become high-performance
businesses and governments., the company generated net revenues of
US$27.9 Billion for the fiscal year ended 31.07.2012..
Now what i want is to format the string to a bit nicer version like this:
XXXX is a global management consulting, technology services and
outsourcing company, with 257,000 people serving clients in more than
120 countries. Combining unparalleled experience, comprehensive
capabilities across all industries and business functions, and
extensive research on the world’s most successful companies, XXXX
collaborates with clients to help them become high-performance
businesses and governments. The company generated net revenues of
US$27.9 billion for the fiscal year ended Aug. 31, 2012.
My question is: Is there any library with already defined methods which could do all the spelling corrections, unneeded space removal, etc .. ?
So far, I do it be replacing stuff like " ," with ", " and toUpperCase() if the is a "///." in front etc..
desc = desc.replace(" ", " ");
desc = desc.replace("..", ".");
desc = desc.replace(" .", ".");
desc = desc.replace(" ,", ", ");
desc = desc.replace(".,", ".");
desc = desc.replace(",.", ".");
desc = desc.replace(", .", ".");
desc = desc.replace("*", "");
I'm sure there is a cleaner and better version to do this. Using regex maybe??
Any solution would be appreciated.
If I were trying to solve your problem, I would probably read the text 1 char at a time, and format it as you go. For example, in psuedocode...
while (has more chars){
char letter = readChar();
if (letter == ','){
// checking for the ',.' combination
letter = readChar();
if (readChar == '.'){
// write out a '.' only
out.print('.');
}
else {
// it wasn't the ',.' combination, so you need to output both characters, whatever they are
out.print(',');
out.print(letter);
}
}
else if (another letter you want to filter){
// etc.
}
else {
// doesn't match any of the filters, so just output the letter
out.print(letter);
}
}
Basically if you read the text 1 char at a time, you can detect any of your chosen formatting problems as you go, and correct them immediately. This provides a performance improvement, as you're only reading over the text string once (not 8 times, like you are currently doing), and allows you to add as many different/complex formatting changes as you want. The downside, however, is that you need to write the logic yourself rather than relying on in-built functions.

Java regex trying to split string up

Hi I am trying to split this string up (its quite long):
Library Catalogue Log off | Borrower record | Course Reading | Collections | A-Z E-Journal list | ILL Request | Help   Browse | Search | Results List | Previous Searches | My e-Shelf | Self-Issue | Feedback       Selected records:  View Selected  |  Save/Mail  |  Create Subset  |  Add to My e-Shelf  |        Whole set:  Select All  |  Deselect  |  Rank  |  Refine  |  Filter   Records 1 - 15 of 101005 (maximum display and sort is 2500 records)         1 Drower, E. S. (Ethel Stefana), Lady, b. 1879. Lady E.S. Drower’s scholarly correspondence : an intrepid English autodidact in Iraq / edited by 2012. BK Book University Library( 1/ 0) 2 Kowalski, Robin M. Cyberbullying : bullying in the digital age / Robin M. Kowalski, Susan P. Limber, Patricia W. Ag 2012. BK Book University Library( 1/ 0) ... 15 Ambrose, Gavin. Approach and language [electronic resource] / Gavin Ambrose, Nigel Aono-Billson. 2011. BK Book
So that I either get back:
1 Drower, E. S. (Ethel Stefana), Lady, b. 1879. Lady E.S. Drower’s scholarly correspondence : an intrepid English autodidact in Iraq / edited by 2012. BK Book University Library( 1/ 0)
// Or
1 Drower, E. S. (Ethel Stefana), Lady, b. 1879. Lady E.S. Drower’s scholarly correspondence : an intrepid English autodidact in Iraq
This is just an example and the 1 Drower, E. S. ... will not be static. While the input will be different every time (the detail between 1 and 2) the general layout of the string will always be the same.
I have:
String top = ".* (.*)";
String bottom = "\( \d/ \d\)\W*";
Pattern p = Pattern.compile(top); //+bottom
Matcher matcher = p.matcher(td); //td is the input String
String items = matcher.group();
System.out.println(items);
When I run it with top, it is meant to remove all of the headers but all I get back is No match found. bottom is my attempt to split the rest of the string.
I can post all of the input up to number 15 if it is needed. What I need is to split up the input string so that I can work with each individual of the 15 results.
Thanks for your help!
This will provide both inputs for you. It is what you wanted?
String text = "Library Catalogue Log off ..."; \\truncated text
Pattern p = Pattern.compile("((1 Drower.+Iraq).+0\\)).+2 Kowalski");
Matcher m = p.matcher(text);
if (m.find()) {
System.out.println(m.group(1));
System.out.println(m.group(2));
}
Compile and run code here.
First off you need to separate the headers from the result data. Assuming that each time there will be that block of 9 whitespaces you can use this: .*\s{9}(.*)
Next you need to parse the data into rows, this is more difficult because you have no row delimiters. The best you can do is assume that rows are delimited by: a space then one or more digits then another space.
((?<=(?:^|\s))\d+\s.*?(?=(?:$|\s\d+\s)))
If you're planning to try to parse the records into fields then don't bother unless you can change the delimiters!
A little explanation of what each bit does:
(?<=(?:^|\s)) Look behind: Make sure the characters preceding the group is either the start of the string (1st record), or a space (all other records).
\d+\s.*? Capture group: One or more digits followed by a space, then followed by text. This is the only part of the expression that shows up in the output because of the use of non-capturing groups ?: in the assertions.
(?=(?:$|\s\d+\s)) Look ahead: Make sure the characters following the group are either the end of string marker $ or a space followed by 1+ digits, followed by a space (indicating the next record).
This method is works with the fields you provided, but it will break if you have a record that contains the custom delimiter e.g. a book called "My 10 favourite things". There other ways of parsing records that are a little safer, but if that's what you want to do then it's beyond the expectations of regex...

Categories