13

A string is given as an input (e.g. "What is your name?"). The input always contains a question which I want to extract. But the problem that I am trying to solve is that the input is always with unneeded input.

So the input could be (but not limited to) the following:

1- "eo000 ATATAT EG\n\nWhat is your name?\nkgda dasflkjasn" 2- "What is your\nlastname and email?\ndasf?lkjas" 3- "askjdmk.\nGiven your skills\nhow would you rate yourself?\nand your name? dasf?"

(Notice that at the third input, the question starts with the word "Given" and end with "yourself?")

The above input examples are generated by the pytesseract OCR library of scanning an image and converting it into text

I only want to extract the question from the garbage input and nothing else.

I tried to use find('?', 1) function of the re library to get index of last part of the question (assuming for now that the first question mark is always the end of the question and not part of the input that I don't want). But I can't figure out how to get the index of the first letter of the question. I tried to loop in reverse and get the first spotted \n in the input, but the question doesn't always have \n before the first letter of the question.

def extractQuestion(q):
    index_end_q = q.find('?', 1)
    index_first_letter_of_q = 0 # TODO
    question = '\n ' . join(q[index_first_letter_of_q :index_end_q ])

3
  • I think more examples may help determine if there is any invariant property about the start of the question to hook into. Commented Jul 6, 2019 at 9:45
  • 2
    I think this TODO is the TODO of humanity right now because you'll need to make your program understand human language in order to properly solve this, and this task remains largely unsolved now.
    – ForceBru
    Commented Jul 6, 2019 at 9:45
  • 2
    You said the input could be, but not limited to, the following. Well, what is it limited to? Maybe you can tell us where you're getting these inputs and we may be able to provide a solution that navigates around having messy inputs in the first place.
    – Paul M.
    Commented Jul 6, 2019 at 9:55

2 Answers 2

11

A way to find the question's first word index would be to search for the first word that has an actual meaning (you're interested in English words I suppose). A way to do that would be using pyenchant:

#!/usr/bin/env python

import enchant

GLOSSARY = enchant.Dict("en_US")

def isWord(word):
    return True if GLOSSARY.check(word) else False

sentences = [
"eo000 ATATAT EG\n\nWhat is your name?\nkgda dasflkjasn",
"What is your\nlastname and email?\ndasf?lkjas",
"\nGiven your skills\nhow would you rate yourself?\nand your name? dasf?"]

for sentence in sentences:
    for i,w in enumerate(sentence.split()):
        if isWord(w):
            print('index: {} => {}'.format(i, w))
            break

The above piece of code gives as a result:

index: 3 => What
index: 0 => What
index: 0 => Given
4
  • Yes this would be a great solution for the problem, but what if the input BEFORE the question is also a valid english word? I updated the question btw
    – LinkCoder
    Commented Jul 6, 2019 at 9:49
  • @LinkCoder Then the problem is much more complicated from the one you initially described. Then maybe NLTK be of some help to help you recognize "logical" sentences, but as far as I know that's not an easy problem to solve (and I think it hasn't been solved by the time I posted this answer).
    – game0ver
    Commented Jul 6, 2019 at 9:53
  • Alright then assuming the input before question is garbage, then how could I find the index of the first letter of the question of the whole input because then I can slice it.
    – LinkCoder
    Commented Jul 6, 2019 at 9:59
  • @LinkCoder that's easy, you can use the python built-in find() function.
    – game0ver
    Commented Jul 6, 2019 at 10:04
6

You could try a regular expression like \b[A-Z][a-z][^?]+\?, meaning:

  • The start of a word \b with an upper case letter [A-Z] followed by a lower case letter [a-z],
  • then a sequence of non-questionmark-characters [^?]+,
  • followed by a literal question mark \?.

This can still have some false positives or misses, e.g. if a question actually starts with an acronym, or if there is a name in the middle of the question, but for you examples it works quite well.

>>> tests = ["eo000 ATATAT EG\n\nWhat is your name?\nkgda dasflkjasn",
             "What is your\nlastname and email?\ndasf?lkjas",
             "\nGiven your skills\nhow would you rate yourself?\nand your name? dasf?"]

>>> import re
>>> p = r"\b[A-Z][a-z][^?]+\?"
>>> [re.search(p, t).group() for t in tests]
['What is your name?',
 'What is your\nlastname and email?',
 'Given your skills\nhow would you rate yourself?']

If that's one blob of text, you can use findall instead of search:

>>> text = "\n".join(tests)
>>> re.findall(p, text)
['What is your name?',
 'What is your\nlastname and email?',
 'Given your skills\nhow would you rate yourself?']

Actually, this also seems to work reasonably well for questions with names in them:

>>> t = "asdGARBAGEasd\nHow did you like St. Petersburg? more stuff with ?" 
>>> re.search(p, t).group()
'How did you like St. Petersburg?'
3
  • @LinkCoder Wouldn't that be exactly what I did in the lower code with text?
    – tobias_k
    Commented Jul 6, 2019 at 10:59
  • Yes I figured it out. I just used re.search(regex_pattern, input, flags=re.S).group().replace("\n", " ") so thanks for that. Btw is there a regex that doesn't miss names but does the same thing as what you did above?
    – LinkCoder
    Commented Jul 6, 2019 at 11:04
  • Actually, I think it should work okay if there are names in the question, as long as the start of the question is the first word in the sentence starting with an upper-case letter.
    – tobias_k
    Commented Jul 6, 2019 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.