コード例 #1
0
ファイル: 07-exclude.py プロジェクト: DataBranner/pattern
def imperative(sentence):
    for p in ("!could|!would|!should|!to+ VB", "^VB", "^do|VB*"):
        m = match(p, sentence)
        # Exclude questions.
        if match(p, sentence) and sentence.string.endswith((".", "!")):
            return True
    return False
コード例 #2
0
ファイル: 07-exclude.py プロジェクト: Abhishek-1/temp
def imperative(sentence):
    for p in ("!could|!would|!should|!to+ VB", "^VB", "^do|VB*"):
        m = match(p, sentence)
        if match(p, sentence) and sentence.string.endswith(
            (".", "!")):  # Exclude questions.
            return True
    return False
コード例 #3
0
 def test_match_function(self):
     # Assert match() function.
     s = Sentence(parse("Go on Bors, chop his head off!"))
     m1 = search.match("chop NP off", s, strict=False)
     m2 = search.match("chop NP+ off", s, strict=True)
     self.assertEqual(m1.constituents()[1].string, "his head")
     self.assertEqual(m2.constituents()[1].string, "his head")
     print "pattern.search.match()"
コード例 #4
0
ファイル: 03-lemmata.py プロジェクト: andreymeretsky/pattern3
from pattern.search import search, match
from pattern.en import parsetree

# This example demonstrates an interesting search pattern that mines for comparisons.
# Notice the use of the constraint "be".
# If the output from the parser includes word lemmas (e.g., "doing" => "do")
# these will also be matched. Using "be" then matches "is", "being", "are", ...
# and if underspecification is used "could be", "will be", "definitely
# was", ...

p = "NP be ADJP|ADVP than NP"

for s in ("the turtle was faster than the hare",
          "Arnold Schwarzenegger is more dangerous than Dolph Lundgren"):
    t = parsetree(s, lemmata=True)  # parse lemmas
    m = search(p, t)
    if m:
        # Constituents for the given constraint indices:
        # 0 = NP, 2 = ADJP|ADVP, 4 = NP
        print(m[0].constituents(constraint=[0, 2, 4]))
        print()

p = "NP be ADJP|ADVP than NP"
t = parsetree("the turtle was faster than the hare", lemmata=True)
m = match(p, t)
print(t)
print()
for w in m.words:
    print(w, " \t=>", m.constraint(w))
コード例 #5
0
ファイル: 08-group.py プロジェクト: Abhishek-1/temp
from pattern.search import match
from pattern.en import parsetree

# This example demonstrates how to create match groups.
# A match group is a number of consecutive constraints,
# for which matching words can easily be retrieved from a Match object.

# Suppose we are looking for adjectives preceding nouns.
# A simple pattern is: "JJ?+ NN",
# which matches nouns preceded by any number of adjectives.
# Since the number of nouns can be 0, 1 or 23 it is not so easy
# to fetch the adjectives from a Match. This can be achieved with a group:

s = "The big black cat"
t = parsetree(s)
print(match("{JJ?+} NN", t).group(1))
print("")

# Note the { } wrapper, indicating a group.
# The group can be retrieved from the match as a list of words.

# Suppose we are looking for prepositional noun phrases,
# e.g., on the mat, with a fork, under the hood, etc...
# The preposition is always one word (on, with, under),
# but the actual noun phrase can have many words (a shiny silver fork),
# so it is a hassle to retrieve it from the match.

# Normally, we would do it like this:

s = "The big black cat sat on the mat."
t = parsetree(s)
コード例 #6
0
ファイル: 03-lemmata.py プロジェクト: andres-root/pattern
from pattern.search import search, match
from pattern.en     import parsetree

# This example demonstrates an interesting search pattern that mines for comparisons.
# Notice the use of the constraint "be".
# If the output from the parser includes word lemmas (e.g., "doing" => "do")
# these will also be matched. Using "be" then matches "is", "being", "are", ...
# and if underspecification is used "could be", "will be", "definitely was", ...

p = "NP be ADJP|ADVP than NP"

for s in (
  "the turtle was faster than the hare",
  "Arnold Schwarzenegger is more dangerous than Dolph Lundgren"):
    t = parsetree(s, lemmata=True) # parse lemmas
    m = search(p, t)
    if m:
        # Constituents for the given constraint indices:
        # 0 = NP, 2 = ADJP|ADVP, 4 = NP
        print(m[0].constituents(constraint=[0,2,4]))
        print()
        
        
p = "NP be ADJP|ADVP than NP"
t = parsetree("the turtle was faster than the hare", lemmata=True)
m = match(p, t)
print(t)
print()
for w in m.words:
    print(w, " \t=>", m.constraint(w))
コード例 #7
0
ファイル: bot.py プロジェクト: rossgoodwin/itpbot
    def parse_message(self, text, usernick, channel):
        if channel != self.chan:
            userOrFalse = usernick
        else:
            userOrFalse = False

        words = re.findall(r"\b[\w]+\b", text.lower())
        tokens = text.lower().split()

        original_words = words[:]

        try:
            words.remove(self.nick)
        except:
            pass

        try:
            words.remove('hst')
        except:
            pass

        try:
            tree = parsetree(' '.join(words))
            firstNoun = match('NN|NNS|NNP|NNPS', tree)
        except:
            firstNoun = None

        # print original_words
        if self.nick in original_words:

            if set(words) & set(['help', 'commands']):
                commandsTemp = Template(self.commands)
                self.send_msg(
                    commandsTemp.substitute(usernick=usernick, botnick=self.nick),
                    channel=userOrFalse
                )
            elif '?' in text or (set(words) & set(['who', 'where', 'when', 'what', 'why', 'how'])):
                fileObj = open('weird_grammar.json', 'r')
                jsonObj = json.load(fileObj)
                fileObj.close()
                s = sentiment(text)[0]
                if s > 0:
                    print s * 2500 + 1
                    self.send_msg(
                        make_polar(jsonObj, int(s * 2500 + 1)),
                        channel=userOrFalse
                    )
                else:
                    print s * 2500 - 1
                    self.send_msg(
                        make_polar(jsonObj, int(s * -2500 - 1), sent=0),
                        channel=userOrFalse
                    )

            elif firstNoun is not None:
                print firstNoun.string.replace('_', ' ')
                s = sentiment(text)[0]
                sentences = sorted(
                    pf_sentences(abs(s*1000+3), firstNoun.string.replace('_', ' ')),
                    key = lambda x: sentiment(x)[0]
                )

                if s > 0:
                    # print s * 2500 + 1
                    self.send_msg(
                        ' '.join(sentences[-3:]),
                        channel=userOrFalse
                    )
                else:
                    # print s * 2500 - 1
                    self.send_msg(
                        ' '.join(sentences[:3]),
                        channel=userOrFalse
                    )
            else:
                snarkTemp = Template(rc(self.snarklist))
                self.send_msg(
                    snarkTemp.substitute(usernick=usernick, botnick=self.nick),
                    channel=userOrFalse
                )

        if tokens[0] == '.seen':
            tgt_user = tokens[1]
            if tgt_user in self.seen_dict:
                last_time, last_msg = self.seen_dict[tgt_user]
                self.send_msg(
                    "%s: %s last seen on %s saying: %s" % (usernick, tgt_user, last_time, last_msg),
                    channel=userOrFalse
                )
            else:
                self.send_msg(
                    "%s: I haven't seen %s." % (usernick, tgt_user),
                    channel=userOrFalse
                )

        elif tokens[0] == '.tell':
            tgt_user = tokens[1]
            if not tgt_user in self.tells_dict:
                self.tells_dict[tgt_user] = []
            self.tells_dict[tgt_user].append((usernick, ' '.join(tokens[2:])))
            self.send_msg(
                "%s: Ok, I'll tell %s that for you." % (usernick, tgt_user),
                channel=userOrFalse
            )
            with open('tells_dict.json', 'w') as outfile:
                json.dump(self.tells_dict, outfile)

        elif tokens[0] == '.showtells':
            if not usernick in self.tells_dict or not self.tells_dict[usernick]:
                self.send_msg("%s: I have nothing for you." % usernick, channel=usernick)
            else:
                while self.tells_dict[usernick]:
                    src_user, tell_msg = self.tells_dict[usernick].pop()
                    self.send_msg("%s said: %s" % (src_user, tell_msg), channel=usernick)
            with open('tells_dict.json', 'w') as outfile:
                json.dump(self.tells_dict, outfile)

        elif tokens[0] == '.gif':
            gif_url = get_gif(tokens[1:])
            self.send_msg("%s: %s" % (usernick, gif_url), channel=userOrFalse)

        elif tokens[0] == '.wiki':
            try:
                wiki_url, wiki_text = get_wiki_article(tokens[1:])
            except:
                self.send_msg(
                    "%s: I'm sorry, but something went wrong!" % usernick,
                    channel=userOrFalse
                )
            else:
                if wiki_text:
                    safe_wiki_text = ''.join(list(wiki_text)[:300]).replace('\n', ' ') + '...'
                    safe_wiki_text = safe_wiki_text.encode('ascii', 'ignore')
                    self.send_msg(
                        "%s: %s | %s" % (usernick, wiki_url, safe_wiki_text),
                        channel=userOrFalse
                    )
                else:
                    self.send_msg(
                        "%s: I'm sorry, but something went wrong!" % usernick,
                        channel=userOrFalse
                    )
                    
        elif tokens[0] == '.yt':
            try:
                result = youtube_search(tokens[1:])
                result = map(lambda x: x.encode('ascii', 'ignore'), result)
                title, desc, vidId = result
                self.send_msg(
                    "%s: %s | %s | https://www.youtube.com/watch?v=%s" % (usernick, title, desc, vidId),
                    channel=userOrFalse
                )
            except:
                self.send_msg(
                    "%s: I'm sorry, but something went wrong!" % usernick,
                    channel=userOrFalse
                )

        elif tokens[0] == '.hst':
            # self.send_msg('/nick drgonzo')
            if firstNoun is not None:
                lookupFile = open("hst_lookup.json", 'r')
                lookup = json.load( lookupFile )
                lookupFile.close()

                nounStem = stem(firstNoun, stemmer=PORTER)
                idHash = None

                print nounStem

                try:
                    # switch to descending
                    idHash = rc(lookup[nounStem])
                    print idHash
                except KeyError:
                    pass

                try:
                    idHash = rc(lookup[firstNoun])
                    print idHash
                except KeyError:
                    pass
                
                if idHash is not None:
                    bookFile = open("hst_text.json", 'r')
                    books = json.load( bookFile )
                    bookFile.close()

                    text = books[idHash].encode('ascii', 'ignore')
                    # print text

                    self.send_msg("%s: %s"%(usernick,text), channel=userOrFalse)

                else:
                    self.send_msg("%s: Can't say I know it." % usernick, channel=userOrFalse)

            else:
                self.send_msg("%s: Nothing to say about that." % usernick, channel=userOrFalse)
            # self.send_msg('/nick itpbot')


        if "ross" in words:
            self.send_msg("%s: I hope you're not speaking ill of my creator." % usernick, channel=userOrFalse)

        if "itp" in words:
            message = rand_itp_acronym()
            self.send_msg(message, channel=userOrFalse)