1
$\begingroup$

I have the following problem.

I want to get a snippet from a text that contains a number, which I am interested in:

Sentence:

{"My phone number is 1234567 and my social security number is 98765433."}

where I know that I want the following snippets for {1234567, 98765433}:

{"phone number 1234567","social security number 98765433}

(optimally, "is" would not be included)

Unfortunately, I have no clue how to tackle this problem... Could anyone help me with this?

Thank you very much!

$\endgroup$
1

1 Answer 1

5
$\begingroup$

Define

text="My phone number is 1234567 and my social security number is 98765433.";

A possible one-liner for this:

StringRiffle[DeleteStopwords[#]]&/@Fold[TextCases,text,{"Clause","NounPhrase"}]

{" phone number 1234567"," social security number 98765433"}

There are many ways to go around this, depending on your text corpus structure and generality of the solution you need. To get you started, consider the following articles in docs:

Using TextCases you can get very flexible results, but you need to read these doc articles above to build a general application that suits your purpose. Here are some simple starters. You can see TextStructure to understand elements you want from grammatical point of view:

TextStructure[text]

enter image description here

Looks Noun Phrase is relevant and you can try to get it cleaning up a bit after:

cases=DeleteStopwords[TextCases[text, "NounPhrase"]]

{" phone number","1234567"," social security number","98765433"}

Now for formatting also many choices:

TextGrid[Partition[cases, 2]]

enter image description here

or what you wanted exactly:

StringRiffle /@ Partition[cases, 2]

{"phone number 1234567","social security number 98765433"}

But a more elegant solution is given at the top with TextCases addressing also Clause.

$\endgroup$
2
  • $\begingroup$ Thank you very much, Vitaliy! It helped me a lot! $\endgroup$
    – Coffee_09
    Apr 25, 2019 at 3:57
  • $\begingroup$ @Coffee_09 you are welcome. I see you are a newcomer, you should be voting on answers and excepting them as shown in TOUR $\endgroup$ Apr 25, 2019 at 8:33

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.