当前位置: 首页>>代码示例>>Python>>正文


Python ElementTree.parse方法代码示例

本文整理汇总了Python中nltk.corpus.reader.xmldocs.ElementTree.parse方法的典型用法代码示例。如果您正苦于以下问题:Python ElementTree.parse方法的具体用法?Python ElementTree.parse怎么用?Python ElementTree.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在nltk.corpus.reader.xmldocs.ElementTree的用法示例。


在下文中一共展示了ElementTree.parse方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _get_sex

# 需要导入模块: from nltk.corpus.reader.xmldocs import ElementTree [as 别名]
# 或者: from nltk.corpus.reader.xmldocs.ElementTree import parse [as 别名]
 def _get_sex(self, fileid, speaker):
     xmldoc = ElementTree.parse(fileid).getroot()
     for pat in xmldoc.findall('.//{%s}Participants/{%s}participant'
                               % (NS,NS)):
         try:
             if pat.get('id') == speaker:
                 sex = pat.get('sex')
                 return sex
         # some files don't have age data
         except (TypeError, AttributeError), e:
             return None
开发者ID:mikabr,项目名称:gender-input,代码行数:13,代码来源:childes.py

示例2: _get_age

# 需要导入模块: from nltk.corpus.reader.xmldocs import ElementTree [as 别名]
# 或者: from nltk.corpus.reader.xmldocs.ElementTree import parse [as 别名]
 def _get_age(self, fileid, speaker, month):
     xmldoc = ElementTree.parse(fileid).getroot()
     for pat in xmldoc.findall('.//{%s}Participants/{%s}participant' % (NS, NS)):
         try:
             if pat.get('id') == speaker:
                 age = pat.get('age')
                 if month:
                     age = self.convert_age(age)
                 return age
         # some files don't have age data
         except (TypeError, AttributeError) as e:
             return None
开发者ID:rmalouf,项目名称:nltk,代码行数:14,代码来源:childes.py

示例3: _get_participants

# 需要导入模块: from nltk.corpus.reader.xmldocs import ElementTree [as 别名]
# 或者: from nltk.corpus.reader.xmldocs.ElementTree import parse [as 别名]
    def _get_participants(self, fileid):
        # multidimensional dicts
        def dictOfDicts():
            return defaultdict(dictOfDicts)

        xmldoc = ElementTree.parse(fileid).getroot()
        # getting participants' data
        pat = dictOfDicts()
        for participant in xmldoc.findall('.//{%s}Participants/{%s}participant'
                                          % (NS,NS)):
            for (key,value) in participant.items():
                pat[participant.get('id')][key] = value
        return pat
开发者ID:langcog,项目名称:alignment,代码行数:15,代码来源:mychildes.py

示例4: _words

# 需要导入模块: from nltk.corpus.reader.xmldocs import ElementTree [as 别名]
# 或者: from nltk.corpus.reader.xmldocs.ElementTree import parse [as 别名]
    def _words(self, fileid, bracket_sent, tag, strip_space, stem):
        """
        Helper used to implement the view methods -- returns a list of
        words or a list of sentences, optionally tagged.

        :param fileid: The name of the underlying file.
        :param bracket_sent: If true, include sentence bracketing.
        :param tag: The name of the tagset to use, or None for no tags.
        :param strip_space: If true, strip spaces from word tokens.
        :param stem: If true, then substitute stems for words.
        """
        result = []

        xmldoc = ElementTree.parse(fileid).getroot()
        for xmlsent in xmldoc.findall('.//s'):
            sent = []
            for xmlword in _all_xmlwords_in(xmlsent):
                word = xmlword.text
                if not word:
                    word = ""  # fixes issue 337?
                if strip_space or stem:
                    word = word.strip()
                if stem:
                    word = xmlword.get('hw', word)
                if tag == 'c5':
                    word = (word, xmlword.get('c5'))
                elif tag == 'pos':
                    word = (word, xmlword.get('pos', xmlword.get('c5')))
                sent.append(word)
            if bracket_sent:
                result.append(BNCSentence(xmlsent.attrib['n'], sent))
            else:
                result.extend(sent)

        assert None not in result
        return result
开发者ID:Journo-App,项目名称:flask-by-example,代码行数:38,代码来源:bnc.py

示例5: _get_words

# 需要导入模块: from nltk.corpus.reader.xmldocs import ElementTree [as 别名]
# 或者: from nltk.corpus.reader.xmldocs.ElementTree import parse [as 别名]
 def _get_words(self, fileid, speaker, sent, stem, relation, pos,
         strip_space, replace):
     if isinstance(speaker, string_types) and speaker != 'ALL':  # ensure we have a list of speakers
         speaker = [ speaker ]
     xmldoc = ElementTree.parse(fileid).getroot()
     # processing each xml doc
     results = []
     for xmlsent in xmldoc.findall('.//{%s}u' % NS):
         sents = [xmlsent.get('who')]
         # select speakers
         if speaker == 'ALL' or xmlsent.get('who') in speaker:
             for xmlword in xmlsent.findall('.//{%s}w' % NS):
                 infl = None ; suffixStem = None
                 # getting replaced words
                 if replace and xmlsent.find('.//{%s}w/{%s}replacement'
                                             % (NS,NS)):
                     xmlword = xmlsent.find('.//{%s}w/{%s}replacement/{%s}w'
                                            % (NS,NS,NS))
                 elif replace and xmlsent.find('.//{%s}w/{%s}wk' % (NS,NS)):
                     xmlword = xmlsent.find('.//{%s}w/{%s}wk' % (NS,NS))
                 # get text
                 if xmlword.text:
                     word = xmlword.text
                 else:
                     word = ''
                 # strip tailing space
                 if strip_space:
                     word = word.strip()
                 # stem
                 if relation or stem:
                     try:
                         xmlstem = xmlword.find('.//{%s}stem' % NS)
                         word = xmlstem.text
                     except AttributeError as e:
                         pass
                     # if there is an inflection
                     try:
                         xmlinfl = xmlword.find('.//{%s}mor/{%s}mw/{%s}mk'
                                                % (NS,NS,NS))
                         word += '-' + xmlinfl.text
                     except:
                         pass
                     # if there is a suffix
                     try:
                         xmlsuffix = xmlword.find('.//{%s}mor/{%s}mor-post/{%s}mw/{%s}stem'
                                                  % (NS,NS,NS,NS))
                         suffixStem = xmlsuffix.text
                     except AttributeError:
                         suffixStem = ""
                 # pos
                 if relation or pos:
                     try:
                         xmlpos = xmlword.findall(".//{%s}c" % NS)
                         xmlpos2 = xmlword.findall(".//{%s}s" % NS)
                         if xmlpos2 != []:
                             tag = xmlpos[0].text+":"+xmlpos2[0].text
                         else:
                             tag = xmlpos[0].text
                         word = (word,tag)
                     except (AttributeError,IndexError) as e:
                         word = (word,None)
                         if suffixStem:
                             suffixStem = (suffixStem,None)
                 # relational
                 # the gold standard is stored in
                 # <mor></mor><mor type="trn"><gra type="grt">
                 if relation == True:
                     for xmlstem_rel in xmlword.findall('.//{%s}mor/{%s}gra'
                                                        % (NS,NS)):
                         if not xmlstem_rel.get('type') == 'grt':
                             word = (word[0], word[1],
                                     xmlstem_rel.get('index')
                                     + "|" + xmlstem_rel.get('head')
                                     + "|" + xmlstem_rel.get('relation'))
                         else:
                             word = (word[0], word[1], word[2],
                                     word[0], word[1],
                                     xmlstem_rel.get('index')
                                     + "|" + xmlstem_rel.get('head')
                                     + "|" + xmlstem_rel.get('relation'))
                     try:
                         for xmlpost_rel in xmlword.findall('.//{%s}mor/{%s}mor-post/{%s}gra'
                                                            % (NS,NS,NS)):
                             if not xmlpost_rel.get('type') == 'grt':
                                 suffixStem = (suffixStem[0],
                                               suffixStem[1],
                                               xmlpost_rel.get('index')
                                               + "|" + xmlpost_rel.get('head')
                                               + "|" + xmlpost_rel.get('relation'))
                             else:
                                 suffixStem = (suffixStem[0], suffixStem[1],
                                               suffixStem[2], suffixStem[0],
                                               suffixStem[1],
                                               xmlpost_rel.get('index')
                                               + "|" + xmlpost_rel.get('head')
                                               + "|" + xmlpost_rel.get('relation'))
                     except:
                         pass
                 sents.append(word)
                 if suffixStem:
#.........这里部分代码省略.........
开发者ID:langcog,项目名称:alignment,代码行数:103,代码来源:mychildes.py

示例6: _get_corpus

# 需要导入模块: from nltk.corpus.reader.xmldocs import ElementTree [as 别名]
# 或者: from nltk.corpus.reader.xmldocs.ElementTree import parse [as 别名]
 def _get_corpus(self, fileid):
     results = dict()
     xmldoc = ElementTree.parse(fileid).getroot()
     for key, value in xmldoc.items():
         results[key] = value
     return results
开发者ID:langcog,项目名称:alignment,代码行数:8,代码来源:mychildes.py

示例7: _get_words

# 需要导入模块: from nltk.corpus.reader.xmldocs import ElementTree [as 别名]
# 或者: from nltk.corpus.reader.xmldocs.ElementTree import parse [as 别名]
    def _get_words(self, fileid, speaker, sent, stem, relation, pos,
            strip_space, replace):
        if isinstance(speaker, str) and speaker != 'ALL':  # ensure we have a list of speakers
            speaker = [ speaker ]
        xmldoc = ElementTree.parse(fileid).getroot()
        # processing each xml doc
        results = []
        for xmlsent in xmldoc.findall('.//{%s}u' % NS):
            sentID  = xmlsent.get('uID')
            sents = []
            # select speakers
            if speaker == 'ALL' or xmlsent.get('who') in speaker:
                for xmlword in xmlsent.findall('.//{%s}w' % NS):

                    if xmlword.get('type') == 'omission':
                        continue

                    infl = None ; suffixStem = None

                    # getting replaced words
                    xstr = lambda s: "" if s is None else unicode(s)
                    if replace and xmlword.find('.//{%s}replacement' % (NS)):
                        continue

                    if xmlword.find('.//{%s}langs' % (NS)):
                        xmlword.text = xmlword.find('.//{%s}langs' % (NS)).tail

                    text_tags = ["{%s}wk" % NS, "{%s}p" % NS, "{%s}shortening" % NS]
                    if xmlword.findall('*'):
                        word_tags = xmlword.findall('*')
                        text = xstr(xmlword.text)
                        for word_tag in word_tags:
                            if word_tag.tag in text_tags:
                                if word_tag.tag == "{%s}wk" % NS:
                                    text += "+"
                                text += xstr(word_tag.text) + xstr(word_tag.tail)
                        xmlword.text = text

                    if xmlword.text:
                        word = xmlword.text
                    else:
                        print 'empty word in sentence %s' % sentID
                        word = ''

                    # strip tailing space
                    if strip_space:
                        word = word.strip()

                    # stem
                    if relation or stem:
                        try:
                            xmlstem = xmlword.find('.//{%s}stem' % NS)
                            word = xmlstem.text
                        except AttributeError, e:
                            pass
                        # if there is an inflection
                        try:
                            xmlinfl = xmlword.find('.//{%s}mor/{%s}mw/{%s}mk'
                                                   % (NS,NS,NS))
                            word += '-' + xmlinfl.text
                        except:
                            pass
                        # if there is a suffix
                        try:
                            xmlsuffix = xmlword.find('.//{%s}mor/{%s}mor-post/{%s}mw/{%s}stem'
                                                     % (NS,NS,NS,NS))
                            suffixStem = xmlsuffix.text
                        except AttributeError:
                            suffixStem = ""
                    # pos
                    if relation or pos:
                        try:
                            xmlpos = xmlword.findall(".//{%s}c" % NS)
                            word = (word,xmlpos[0].text)
                            if len(xmlpos) != 1 and suffixStem:
                                suffixStem = (suffixStem,xmlpos[1].text)
                        except (AttributeError,IndexError), e:
                            word = (word,None)
                            if suffixStem:
                                suffixStem = (suffixStem,None)
                    # relational
                    # the gold standard is stored in
                    # <mor></mor><mor type="trn"><gra type="grt">
                    if relation == True:
                        for xmlstem_rel in xmlword.findall('.//{%s}mor/{%s}gra'
                                                           % (NS,NS)):
                            if not xmlstem_rel.get('type') == 'grt':
                                word = (word[0], word[1],
                                        xmlstem_rel.get('index')
                                        + "|" + xmlstem_rel.get('head')
                                        + "|" + xmlstem_rel.get('relation'))
                            else:
                                word = (word[0], word[1], word[2],
                                        word[0], word[1],
                                        xmlstem_rel.get('index')
                                        + "|" + xmlstem_rel.get('head')
                                        + "|" + xmlstem_rel.get('relation'))
                        try:
                            for xmlpost_rel in xmlword.findall('.//{%s}mor/{%s}mor-post/{%s}gra'
                                                               % (NS,NS,NS)):
#.........这里部分代码省略.........
开发者ID:mikabr,项目名称:gender-input,代码行数:103,代码来源:childes.py

示例8: str

# 需要导入模块: from nltk.corpus.reader.xmldocs import ElementTree [as 别名]
# 或者: from nltk.corpus.reader.xmldocs.ElementTree import parse [as 别名]
import nltk
from nltk.corpus.reader.xmldocs import ElementTree

NS = "http://www.talkbank.org/ns/talkbank"
xstr = lambda s: "" if s is None else str(s)
corpus_root = nltk.data.find("corpora/childes/data-xml/English/Eng-NA-MOR/")

reload(childes)
from childes import CHILDESCorpusReader

corpus_reader = CHILDESCorpusReader(corpus_root, r"Providence/William/wil11.xml")
words = corpus_reader.words(replace=True)
freqs = nltk.FreqDist(words)

fileid = corpus_reader.fileids()[0]
xmldoc = ElementTree.parse(corpus_root + "/" + fileid).getroot()
xmlsents = xmldoc.findall(".//{%s}u" % NS)

xmlsent = xmlsents[173]
xmlwords = xmlsent.findall(".//{%s}w" % NS)
xmlword = xmlwords[2]

shortenings = xmlword.findall(".//{%s}shortening" % (NS))
text = xstr(xmlword.text) + "".join([xstr(short.text) + xstr(short.tail) for short in shortenings])
开发者ID:mikabr,项目名称:gender-input,代码行数:26,代码来源:childes_testing.py


注:本文中的nltk.corpus.reader.xmldocs.ElementTree.parse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。