Retrieve definition for parenthesized abbreviation, based on letter count
Is there a DSLR/mirorless camera with minimal options like a classic, simple SLR?
How do free-speech protections in the United States apply in public to corporate misrepresentations?
How to trick the reader into thinking they're following a redshirt instead of the protagonist?
Has there been a multiethnic Star Trek character?
A map of non-pathological topology?
Scientist couple raises alien baby
Proving that a Russian cryptographic standard is too structured
A word that means "blending into a community too much"
Why not invest in precious metals?
Should I refuse being named as co-author of a bad quality paper?
Is it expected that a reader will skip parts of what you write?
Is it safe to change the harddrive power feature so that it never turns off?
How can I end combat quickly when the outcome is inevitable?
Longest bridge/tunnel that can be cycled over/through?
Does Assassinate grant two attacks?
Origin of "boor"
60s or 70s novel about Empire of Man making 1st contact with 1st discovered alien race
New bike, tubeless tire will not inflate
Should I put programming books I wrote a few years ago on my resume?
Can a human be transformed into a Mind Flayer?
Excel division by 0 error when trying to average results of formulas
How can I deal with uncomfortable silence from my partner?
Why am I getting a strange double quote (“) in Open Office instead of the ordinary one (")?
How to communicate to my GM that not being allowed to use stealth isn't fun for me?
Retrieve definition for parenthesized abbreviation, based on letter count
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I need to retrieve the definition of an acronym based on the number of letters enclosed in parentheses. For the data I'm dealing with, the number of letters in parentheses corresponds to the number of words to retrieve. I know this isn't a reliable method for getting abbreviations, but in my case it will be. For example:
String = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'
Desired output: family health history (FHH), nurse practitioner (NP)
I know how to extract parentheses from a string, but after that I am stuck. Any help is appreciated.
import re
a = 'Although family health history (FHH) is commonly accepted as an
important risk factor for common, chronic diseases, it is rarely considered
by a nurse practitioner (NP).'
x2 = re.findall('((.*?))', a)
for x in x2:
length = len(x)
print(x, length)
python regex text text-parsing abbreviation
add a comment |
I need to retrieve the definition of an acronym based on the number of letters enclosed in parentheses. For the data I'm dealing with, the number of letters in parentheses corresponds to the number of words to retrieve. I know this isn't a reliable method for getting abbreviations, but in my case it will be. For example:
String = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'
Desired output: family health history (FHH), nurse practitioner (NP)
I know how to extract parentheses from a string, but after that I am stuck. Any help is appreciated.
import re
a = 'Although family health history (FHH) is commonly accepted as an
important risk factor for common, chronic diseases, it is rarely considered
by a nurse practitioner (NP).'
x2 = re.findall('((.*?))', a)
for x in x2:
length = len(x)
print(x, length)
python regex text text-parsing abbreviation
I think you will need to write some parsing logic here, in addition to maybe using regex.
– Tim Biegeleisen
Jun 2 at 2:55
I know I can run a loop and do a Len(string) to get the number of letters, but I guess it's after that point I'm lost. Like if it's 3 letters, how to capture the previous 3 words.
– tenebris silentio
Jun 2 at 2:59
1
You should use"""instead of'for multiline string
– Keatinge
Jun 2 at 3:00
add a comment |
I need to retrieve the definition of an acronym based on the number of letters enclosed in parentheses. For the data I'm dealing with, the number of letters in parentheses corresponds to the number of words to retrieve. I know this isn't a reliable method for getting abbreviations, but in my case it will be. For example:
String = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'
Desired output: family health history (FHH), nurse practitioner (NP)
I know how to extract parentheses from a string, but after that I am stuck. Any help is appreciated.
import re
a = 'Although family health history (FHH) is commonly accepted as an
important risk factor for common, chronic diseases, it is rarely considered
by a nurse practitioner (NP).'
x2 = re.findall('((.*?))', a)
for x in x2:
length = len(x)
print(x, length)
python regex text text-parsing abbreviation
I need to retrieve the definition of an acronym based on the number of letters enclosed in parentheses. For the data I'm dealing with, the number of letters in parentheses corresponds to the number of words to retrieve. I know this isn't a reliable method for getting abbreviations, but in my case it will be. For example:
String = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'
Desired output: family health history (FHH), nurse practitioner (NP)
I know how to extract parentheses from a string, but after that I am stuck. Any help is appreciated.
import re
a = 'Although family health history (FHH) is commonly accepted as an
important risk factor for common, chronic diseases, it is rarely considered
by a nurse practitioner (NP).'
x2 = re.findall('((.*?))', a)
for x in x2:
length = len(x)
print(x, length)
python regex text text-parsing abbreviation
python regex text text-parsing abbreviation
edited Jun 2 at 5:54
cdlane
21.4k31245
21.4k31245
asked Jun 2 at 2:45
tenebris silentiotenebris silentio
969
969
I think you will need to write some parsing logic here, in addition to maybe using regex.
– Tim Biegeleisen
Jun 2 at 2:55
I know I can run a loop and do a Len(string) to get the number of letters, but I guess it's after that point I'm lost. Like if it's 3 letters, how to capture the previous 3 words.
– tenebris silentio
Jun 2 at 2:59
1
You should use"""instead of'for multiline string
– Keatinge
Jun 2 at 3:00
add a comment |
I think you will need to write some parsing logic here, in addition to maybe using regex.
– Tim Biegeleisen
Jun 2 at 2:55
I know I can run a loop and do a Len(string) to get the number of letters, but I guess it's after that point I'm lost. Like if it's 3 letters, how to capture the previous 3 words.
– tenebris silentio
Jun 2 at 2:59
1
You should use"""instead of'for multiline string
– Keatinge
Jun 2 at 3:00
I think you will need to write some parsing logic here, in addition to maybe using regex.
– Tim Biegeleisen
Jun 2 at 2:55
I think you will need to write some parsing logic here, in addition to maybe using regex.
– Tim Biegeleisen
Jun 2 at 2:55
I know I can run a loop and do a Len(string) to get the number of letters, but I guess it's after that point I'm lost. Like if it's 3 letters, how to capture the previous 3 words.
– tenebris silentio
Jun 2 at 2:59
I know I can run a loop and do a Len(string) to get the number of letters, but I guess it's after that point I'm lost. Like if it's 3 letters, how to capture the previous 3 words.
– tenebris silentio
Jun 2 at 2:59
1
1
You should use
""" instead of ' for multiline string– Keatinge
Jun 2 at 3:00
You should use
""" instead of ' for multiline string– Keatinge
Jun 2 at 3:00
add a comment |
5 Answers
5
active
oldest
votes
Use the regex match to find the position of the start of the match. Then use python string indexing to get the substring leading up to the start of the match. Split the substring by words, and get the last n words. Where n is the length of the abbreviation.
import re
s = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'
for match in re.finditer(r"((.*?))", s):
start_index = match.start()
abbr = match.group(1)
size = len(abbr)
words = s[:start_index].split()[-size:]
definition = " ".join(words)
print(abbr, definition)
This prints:
FHH family health history
NP nurse practitioner
Man, what a life saver. That makes sense. Thanks so much .
– tenebris silentio
Jun 2 at 3:09
You can addoutput = ""to the top of the code, andoutput += definition + ", (" + abbr + ")"to the end of the loop to get your desired output.
– MarsNebulaSoup
Jun 2 at 3:12
I would suggest to match only capital letters:re.finditer(r"(([A-Z]*?))", s)
– igrinis
Jun 2 at 13:24
add a comment |
An idea, to use a recursive pattern with PyPI regex module.
b[A-Za-z]+s+(?R)?(?[A-Z](?=[A-Z]*)))?
See this pcre demo at regex101
b[A-Za-z]+s+matches a word boundary, one or more alpha, one or more white space(?R)?recursive part: optionally paste the pattern from start(?need to make the parenthesis optional for recursion to fit in)?[A-Z](?=[A-Z]*)match one upper alpha if followed by closing)with any A-Z in between
- Does not check if the first word letter actually match the letter at position in the abbreviation.
- Does not check for an opening parenthesis in front of the abbreviation. To check, add a variable length lookbehind. Change
[A-Z](?=[A-Z]*))to(?<=([A-Z]*)[A-Z](?=[A-Z]*)).
add a comment |
does this solve your problem?
a = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'
splitstr=a.replace('.','').split(' ')
output=''
for i,word in enumerate(splitstr):
if '(' in word:
w=word.replace('(','').replace(')','').replace('.','')
for n in range(len(w)+1):
output=splitstr[i-n]+' '+output
print(output)
actually, Keatinge beat me to it
add a comment |
Using re with list-comprehension
x_lst = [ str(len(i[1:-1])) for i in re.findall('((.*?))', a) ]
[re.search( r'(S+s+)' + i + '(.' + i + ')', a).group(0) for i in x_lst]
#['family health history (FHH)', 'nurse practitioner (NP)']
add a comment |
This solution isn't particularly clever, it simpy searches for the acronyms and then builds up a pattern to extract the words ahead of each one:
import re
string = "Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP)."
definitions = []
for acronym in re.findall(r'(([A-Z]+?))', string):
length = len(acronym)
match = re.search(r'(?:w+W+)' + str(length) + r'(' + acronym + r')', string)
definitions.append(match.group(0))
print(", ".join(definitions))
OUTPUT
> python3 test.py
family health history (FHH), nurse practitioner (NP)
>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56411861%2fretrieve-definition-for-parenthesized-abbreviation-based-on-letter-count%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use the regex match to find the position of the start of the match. Then use python string indexing to get the substring leading up to the start of the match. Split the substring by words, and get the last n words. Where n is the length of the abbreviation.
import re
s = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'
for match in re.finditer(r"((.*?))", s):
start_index = match.start()
abbr = match.group(1)
size = len(abbr)
words = s[:start_index].split()[-size:]
definition = " ".join(words)
print(abbr, definition)
This prints:
FHH family health history
NP nurse practitioner
Man, what a life saver. That makes sense. Thanks so much .
– tenebris silentio
Jun 2 at 3:09
You can addoutput = ""to the top of the code, andoutput += definition + ", (" + abbr + ")"to the end of the loop to get your desired output.
– MarsNebulaSoup
Jun 2 at 3:12
I would suggest to match only capital letters:re.finditer(r"(([A-Z]*?))", s)
– igrinis
Jun 2 at 13:24
add a comment |
Use the regex match to find the position of the start of the match. Then use python string indexing to get the substring leading up to the start of the match. Split the substring by words, and get the last n words. Where n is the length of the abbreviation.
import re
s = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'
for match in re.finditer(r"((.*?))", s):
start_index = match.start()
abbr = match.group(1)
size = len(abbr)
words = s[:start_index].split()[-size:]
definition = " ".join(words)
print(abbr, definition)
This prints:
FHH family health history
NP nurse practitioner
Man, what a life saver. That makes sense. Thanks so much .
– tenebris silentio
Jun 2 at 3:09
You can addoutput = ""to the top of the code, andoutput += definition + ", (" + abbr + ")"to the end of the loop to get your desired output.
– MarsNebulaSoup
Jun 2 at 3:12
I would suggest to match only capital letters:re.finditer(r"(([A-Z]*?))", s)
– igrinis
Jun 2 at 13:24
add a comment |
Use the regex match to find the position of the start of the match. Then use python string indexing to get the substring leading up to the start of the match. Split the substring by words, and get the last n words. Where n is the length of the abbreviation.
import re
s = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'
for match in re.finditer(r"((.*?))", s):
start_index = match.start()
abbr = match.group(1)
size = len(abbr)
words = s[:start_index].split()[-size:]
definition = " ".join(words)
print(abbr, definition)
This prints:
FHH family health history
NP nurse practitioner
Use the regex match to find the position of the start of the match. Then use python string indexing to get the substring leading up to the start of the match. Split the substring by words, and get the last n words. Where n is the length of the abbreviation.
import re
s = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'
for match in re.finditer(r"((.*?))", s):
start_index = match.start()
abbr = match.group(1)
size = len(abbr)
words = s[:start_index].split()[-size:]
definition = " ".join(words)
print(abbr, definition)
This prints:
FHH family health history
NP nurse practitioner
edited Jun 2 at 3:16
answered Jun 2 at 3:07
KeatingeKeatinge
3,27041532
3,27041532
Man, what a life saver. That makes sense. Thanks so much .
– tenebris silentio
Jun 2 at 3:09
You can addoutput = ""to the top of the code, andoutput += definition + ", (" + abbr + ")"to the end of the loop to get your desired output.
– MarsNebulaSoup
Jun 2 at 3:12
I would suggest to match only capital letters:re.finditer(r"(([A-Z]*?))", s)
– igrinis
Jun 2 at 13:24
add a comment |
Man, what a life saver. That makes sense. Thanks so much .
– tenebris silentio
Jun 2 at 3:09
You can addoutput = ""to the top of the code, andoutput += definition + ", (" + abbr + ")"to the end of the loop to get your desired output.
– MarsNebulaSoup
Jun 2 at 3:12
I would suggest to match only capital letters:re.finditer(r"(([A-Z]*?))", s)
– igrinis
Jun 2 at 13:24
Man, what a life saver. That makes sense. Thanks so much .
– tenebris silentio
Jun 2 at 3:09
Man, what a life saver. That makes sense. Thanks so much .
– tenebris silentio
Jun 2 at 3:09
You can add
output = "" to the top of the code, and output += definition + ", (" + abbr + ")" to the end of the loop to get your desired output.– MarsNebulaSoup
Jun 2 at 3:12
You can add
output = "" to the top of the code, and output += definition + ", (" + abbr + ")" to the end of the loop to get your desired output.– MarsNebulaSoup
Jun 2 at 3:12
I would suggest to match only capital letters:
re.finditer(r"(([A-Z]*?))", s)– igrinis
Jun 2 at 13:24
I would suggest to match only capital letters:
re.finditer(r"(([A-Z]*?))", s)– igrinis
Jun 2 at 13:24
add a comment |
An idea, to use a recursive pattern with PyPI regex module.
b[A-Za-z]+s+(?R)?(?[A-Z](?=[A-Z]*)))?
See this pcre demo at regex101
b[A-Za-z]+s+matches a word boundary, one or more alpha, one or more white space(?R)?recursive part: optionally paste the pattern from start(?need to make the parenthesis optional for recursion to fit in)?[A-Z](?=[A-Z]*)match one upper alpha if followed by closing)with any A-Z in between
- Does not check if the first word letter actually match the letter at position in the abbreviation.
- Does not check for an opening parenthesis in front of the abbreviation. To check, add a variable length lookbehind. Change
[A-Z](?=[A-Z]*))to(?<=([A-Z]*)[A-Z](?=[A-Z]*)).
add a comment |
An idea, to use a recursive pattern with PyPI regex module.
b[A-Za-z]+s+(?R)?(?[A-Z](?=[A-Z]*)))?
See this pcre demo at regex101
b[A-Za-z]+s+matches a word boundary, one or more alpha, one or more white space(?R)?recursive part: optionally paste the pattern from start(?need to make the parenthesis optional for recursion to fit in)?[A-Z](?=[A-Z]*)match one upper alpha if followed by closing)with any A-Z in between
- Does not check if the first word letter actually match the letter at position in the abbreviation.
- Does not check for an opening parenthesis in front of the abbreviation. To check, add a variable length lookbehind. Change
[A-Z](?=[A-Z]*))to(?<=([A-Z]*)[A-Z](?=[A-Z]*)).
add a comment |
An idea, to use a recursive pattern with PyPI regex module.
b[A-Za-z]+s+(?R)?(?[A-Z](?=[A-Z]*)))?
See this pcre demo at regex101
b[A-Za-z]+s+matches a word boundary, one or more alpha, one or more white space(?R)?recursive part: optionally paste the pattern from start(?need to make the parenthesis optional for recursion to fit in)?[A-Z](?=[A-Z]*)match one upper alpha if followed by closing)with any A-Z in between
- Does not check if the first word letter actually match the letter at position in the abbreviation.
- Does not check for an opening parenthesis in front of the abbreviation. To check, add a variable length lookbehind. Change
[A-Z](?=[A-Z]*))to(?<=([A-Z]*)[A-Z](?=[A-Z]*)).
An idea, to use a recursive pattern with PyPI regex module.
b[A-Za-z]+s+(?R)?(?[A-Z](?=[A-Z]*)))?
See this pcre demo at regex101
b[A-Za-z]+s+matches a word boundary, one or more alpha, one or more white space(?R)?recursive part: optionally paste the pattern from start(?need to make the parenthesis optional for recursion to fit in)?[A-Z](?=[A-Z]*)match one upper alpha if followed by closing)with any A-Z in between
- Does not check if the first word letter actually match the letter at position in the abbreviation.
- Does not check for an opening parenthesis in front of the abbreviation. To check, add a variable length lookbehind. Change
[A-Z](?=[A-Z]*))to(?<=([A-Z]*)[A-Z](?=[A-Z]*)).
answered Jun 2 at 10:42
bobble bubblebobble bubble
7,12011529
7,12011529
add a comment |
add a comment |
does this solve your problem?
a = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'
splitstr=a.replace('.','').split(' ')
output=''
for i,word in enumerate(splitstr):
if '(' in word:
w=word.replace('(','').replace(')','').replace('.','')
for n in range(len(w)+1):
output=splitstr[i-n]+' '+output
print(output)
actually, Keatinge beat me to it
add a comment |
does this solve your problem?
a = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'
splitstr=a.replace('.','').split(' ')
output=''
for i,word in enumerate(splitstr):
if '(' in word:
w=word.replace('(','').replace(')','').replace('.','')
for n in range(len(w)+1):
output=splitstr[i-n]+' '+output
print(output)
actually, Keatinge beat me to it
add a comment |
does this solve your problem?
a = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'
splitstr=a.replace('.','').split(' ')
output=''
for i,word in enumerate(splitstr):
if '(' in word:
w=word.replace('(','').replace(')','').replace('.','')
for n in range(len(w)+1):
output=splitstr[i-n]+' '+output
print(output)
actually, Keatinge beat me to it
does this solve your problem?
a = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'
splitstr=a.replace('.','').split(' ')
output=''
for i,word in enumerate(splitstr):
if '(' in word:
w=word.replace('(','').replace(')','').replace('.','')
for n in range(len(w)+1):
output=splitstr[i-n]+' '+output
print(output)
actually, Keatinge beat me to it
answered Jun 2 at 3:09
3NiGMa3NiGMa
559
559
add a comment |
add a comment |
Using re with list-comprehension
x_lst = [ str(len(i[1:-1])) for i in re.findall('((.*?))', a) ]
[re.search( r'(S+s+)' + i + '(.' + i + ')', a).group(0) for i in x_lst]
#['family health history (FHH)', 'nurse practitioner (NP)']
add a comment |
Using re with list-comprehension
x_lst = [ str(len(i[1:-1])) for i in re.findall('((.*?))', a) ]
[re.search( r'(S+s+)' + i + '(.' + i + ')', a).group(0) for i in x_lst]
#['family health history (FHH)', 'nurse practitioner (NP)']
add a comment |
Using re with list-comprehension
x_lst = [ str(len(i[1:-1])) for i in re.findall('((.*?))', a) ]
[re.search( r'(S+s+)' + i + '(.' + i + ')', a).group(0) for i in x_lst]
#['family health history (FHH)', 'nurse practitioner (NP)']
Using re with list-comprehension
x_lst = [ str(len(i[1:-1])) for i in re.findall('((.*?))', a) ]
[re.search( r'(S+s+)' + i + '(.' + i + ')', a).group(0) for i in x_lst]
#['family health history (FHH)', 'nurse practitioner (NP)']
edited Jun 2 at 3:25
answered Jun 2 at 3:17
TranshumanTranshuman
2,9761412
2,9761412
add a comment |
add a comment |
This solution isn't particularly clever, it simpy searches for the acronyms and then builds up a pattern to extract the words ahead of each one:
import re
string = "Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP)."
definitions = []
for acronym in re.findall(r'(([A-Z]+?))', string):
length = len(acronym)
match = re.search(r'(?:w+W+)' + str(length) + r'(' + acronym + r')', string)
definitions.append(match.group(0))
print(", ".join(definitions))
OUTPUT
> python3 test.py
family health history (FHH), nurse practitioner (NP)
>
add a comment |
This solution isn't particularly clever, it simpy searches for the acronyms and then builds up a pattern to extract the words ahead of each one:
import re
string = "Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP)."
definitions = []
for acronym in re.findall(r'(([A-Z]+?))', string):
length = len(acronym)
match = re.search(r'(?:w+W+)' + str(length) + r'(' + acronym + r')', string)
definitions.append(match.group(0))
print(", ".join(definitions))
OUTPUT
> python3 test.py
family health history (FHH), nurse practitioner (NP)
>
add a comment |
This solution isn't particularly clever, it simpy searches for the acronyms and then builds up a pattern to extract the words ahead of each one:
import re
string = "Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP)."
definitions = []
for acronym in re.findall(r'(([A-Z]+?))', string):
length = len(acronym)
match = re.search(r'(?:w+W+)' + str(length) + r'(' + acronym + r')', string)
definitions.append(match.group(0))
print(", ".join(definitions))
OUTPUT
> python3 test.py
family health history (FHH), nurse practitioner (NP)
>
This solution isn't particularly clever, it simpy searches for the acronyms and then builds up a pattern to extract the words ahead of each one:
import re
string = "Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP)."
definitions = []
for acronym in re.findall(r'(([A-Z]+?))', string):
length = len(acronym)
match = re.search(r'(?:w+W+)' + str(length) + r'(' + acronym + r')', string)
definitions.append(match.group(0))
print(", ".join(definitions))
OUTPUT
> python3 test.py
family health history (FHH), nurse practitioner (NP)
>
edited Jun 2 at 3:29
answered Jun 2 at 3:22
cdlanecdlane
21.4k31245
21.4k31245
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56411861%2fretrieve-definition-for-parenthesized-abbreviation-based-on-letter-count%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I think you will need to write some parsing logic here, in addition to maybe using regex.
– Tim Biegeleisen
Jun 2 at 2:55
I know I can run a loop and do a Len(string) to get the number of letters, but I guess it's after that point I'm lost. Like if it's 3 letters, how to capture the previous 3 words.
– tenebris silentio
Jun 2 at 2:59
1
You should use
"""instead of'for multiline string– Keatinge
Jun 2 at 3:00