2
$\begingroup$

For example, consider

string = "this is a sentence."

The best way I can think about is DictionaryWordQ. However, "this a is sentence." can't be found in this way. "is sentence this a." "is sentence a this."ares not sentences. because they are not grammatically correct.

I'm doing a cryptography analysis for some text messages. I want to write code to tell some combinations whether it's true or not.

Anyone has a better idea?

$\endgroup$
8
  • 1
    $\begingroup$ TextStructure might be helpful here. It doesn't not tell you what's wrong with the second example, but can be used to show that one has a verb and one does not. $\endgroup$ Nov 10, 2020 at 12:43
  • 1
    $\begingroup$ I think both examples are sentences -- voting to close as opinion based. (But I like the answer of @bobthechemist .) $\endgroup$ Nov 10, 2020 at 12:53
  • 2
    $\begingroup$ @AntonAntonov Well, there are lots of machine learning methods these days which handle fuzzy problems like this just fine. Defining what is or isn't a sentence is going to be impossible. But writing a problem that classifies strings reasonably, and usually returns True for obviously correct sentences and False for obvious gibberish is doable. What it would or could do in the middle (non-obvious cases) and whether it could be useful for any real application are different questions. $\endgroup$
    – Szabolcs
    Nov 10, 2020 at 13:42
  • 2
    $\begingroup$ I guess this is more or less the job of engineers at grammarly.com :-) $\endgroup$
    – Szabolcs
    Nov 10, 2020 at 13:43
  • 1
    $\begingroup$ (cont.) For example, I would say that "this, a sentence." is a valid sentence, but using TextStructure -- as shown by @bobthechemist -- gives False for that sentence. So, a narrowing definition is needed. $\endgroup$ Nov 10, 2020 at 13:49

1 Answer 1

10
$\begingroup$

Mathematica has the experimental (as of v12) function TextStructure which provides some guidance with this question, but things get tricky.

s1 = "This is a sentence.";
s2 = "This a is sentence.";
s3 = "This a sentence.";
sentenceQ[s_String]:=StringContainsQ[
    TextStructure[s,"ConstituentStrings"],"Sentence"];

sentenceQ yields True for s1 and s2 and False for the last example. Not having a verb phrase is grounds for TextStructure to not declare that the string is a sentence.

Clearly, s2 is grammatically incorrect. Less clear, is whether or not a grammatically incorrect phrase is still considered a sentence.

If we take one definition of a sentence as containing a subject and a predicate, then s2 is a sentence with a subject This a and a predicate is sentence. Granted, the subject and predicate are nonsensical. Should Mathematica know that? At this point, I think the question belongs at a different StackExchange website.

I decided to stop my search for grammatical truth once I learned that Mathematica and Yoda agree:

enter image description here

I do not believe that current built-in functions of Mathematica provide the tools necessary to address the OPs problem, which I interpret to be identifying grammar errors in a sentence. The reason can be summarized by comparing the output of TextStructure on "The baseball team are established." and "The baseball team is established." Both yield the same parts of speech, notably, "is" and "are" are both labeled correctly as verbs. However, in order for grammar checking to pick up this error, the two verbs must be tagged differently. A thesis has been written on creating an open-source grammar checker which describes parts-of-speech tagging in more detail. Perhaps if this level of detail is incorporated into future versions of the linguistics functions in Mathematica, this problem will become more tractable.

$\endgroup$
4
  • $\begingroup$ But OP asked about "this a is sentence", for which this returns True. Perhaps this "a" is a sentence but that "a" is not a sentence. Would make more sense if we used a1 and a2 instead of s1 and s2. This illustrates why this is going to be difficult to do reliably :-) $\endgroup$
    – Szabolcs
    Nov 10, 2020 at 13:40
  • 2
    $\begingroup$ @Szabolcs yes, I noticed that after posting and instead of updating my answer have been jumping down the rabbit hole of web-searching: "what is a sentence?". $\endgroup$ Nov 10, 2020 at 13:43
  • $\begingroup$ Of course that was not a real criticism, and I already upvoted :-) $\endgroup$
    – Szabolcs
    Nov 10, 2020 at 13:44
  • $\begingroup$ Your code is not working. StringContainsQ::strse: String or list of strings expected at position 1 in StringContainsQ[TextSTructure[This a is sentence.,ConstituentStrings],Sentence]. $\endgroup$
    – kile
    Nov 11, 2020 at 1:03

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