你能够帮我调整一下patterns 中的正则表达式吗,帮我修改和补充其中的表达式,已达到更好的效果 import nltk from nltk.corpus import…
- User
你能够帮我调整一下patterns 中的正则表达式吗,帮我修改和补充其中的表达式,已达到更好的效果 import nltk from nltk.corpus import brown
len(brown.words(categories='news'))
len(brown.words(categories='editorial'))
len(brown.words(categories='reviews'))
train = brown.tagged_sents(categories='news') validation = brown.tagged_sents(categories='editorial') test = brown.tagged_sents(categories='reviews')
patterns = [('.*ing', 'VBG'), # gerunds ('.*ed', 'VBD'), # simple past ('.es', 'VBZ'), # 3rd singular present ('.*ould', 'MD'), # modals ('.'s'), # possessive nouns ('.s', 'NNS'), # plural nouns ('^-?[0-9]+(.[0-9]+)?', 'CD'), # cardinal numbers ('.', 'NN')] # nouns (default)
- Assistant
这是一个已经调整过的patterns示例:
patterns = [ ('.*ing$', 'VBG'), # gerunds ('.*ed$', 'VBD'), # simple past ('.*es$', 'VBZ'), # 3rd singular present ('.*ould$', 'MD'), # modals ('.*\'s$', 'VBZ'), # 3rd singular present ('.*s$', 'NNS'), # plural nouns ('^-?[0-9]+(.[0-9]+)?$', 'CD'), # cardinal numbers ('(The|the|A|a|An|an)$', 'AT'), # articles ('(He|he|She|she|It|it|I|me|Me|You|you)$', 'PRP'), # pronouns ('(His|his|Her|her|Its|its|Your|your)$', 'PRP$'), # possessive pronouns ('(This|this|That|that)$', 'DT'), # demonstrative articles ('(\\.|!|\\?)$', '.'), # punctuation ('(,|;)$', ','), ('.*', 'NN') # nouns (default) ]以上修改对已有表达式的调整主要包括:
- 修改了关于“'s”结尾的词的表达式,将其从“NN$”改为了“VBZ”。
在此基础上,我也添加了一些新表达式:
- 对文章(a、the 等)进行了标注。
- 对人称代词进行了标注。
- 对所有格代词进行了标注。
- 对指示代词进行了标注。
- 对标点符号进行了标注。
- User
你展示的形式很奇怪,有奇怪的换行,可以重新展示一下吗
- Assistant
当然可以。以下是调整后的patterns示例,去除了奇怪的换行:
patterns = [ ('.*ing$', 'VBG'), # gerunds ('.*ed$', 'VBD'), # simple past ('.*es$', 'VBZ'), # 3rd singular present ('.*ould$', 'MD'), # modals ('.*\'s$', 'VBZ'), # 3rd singular present ('.*s$', 'NNS'), # plural nouns ('^-?[0-9]+(.[0-9]+)?$', 'CD'), # cardinal numbers ('(The|the|A|a|An|an)$', 'AT'), # articles ('(He|he|She|she|It|it|I|me|Me|You|you)$', 'PRP'), # pronouns ('(His|his|Her|her|Its|its|Your|your)$', 'PRP$'), # possessive pronouns ('(This|this|That|that)$', 'DT'), # demonstrative articles ('(\\.|!|\\?)$', '.'), # punctuation ('(,|;)$', ','), ('.*', 'NN') # nouns (default) ] - User
使用上面的代码,准确率反而下降了,能够重新给我生成一下正则表达式吗
- Assistant
抱歉导致准确率下降。以下是一个经过重新调整的patterns示例:
patterns = [ (r'.*ing$', 'VBG'), # gerunds (r'.*ed$', 'VBD'), # simple past (r'.*es$', 'VBZ'), # 3rd singular present (r'.*ould$', 'MD'), # modals (r'.*\'s$', 'POS'), # possessive nouns (r'.*s$', 'NNS'), # plural nouns (r'^-?[0-9]+(.[0-9]+)?$', 'CD'), # cardinal numbers (r'(The|the|A|a|An|an)$', 'DT'), # determiners (r'.*able$', 'JJ'), # adjectives (r'.*ness$', 'NN'), # nouns formed from adjectives (r'.*ly$', 'RB'), # adverbs (r'(He|he|She|she|It|it|I|me|Me|You|you)$', 'PRP'), # pronouns (r'(His|his|Her|her|Its|its|Your|your)$', 'PRP$'), # possessive pronouns (r'.*\'$', '.'), # punctuation (r'.*$', 'NN') # nouns (default) ]上述正则表达式虽然较简单,但可能提高测试集的准确率。当然,适用范围仍有限,您可能需要基于不同的文本进行调整。有关改进的部分,主要包括:
- 替换了关于“'s”结尾的词的表达式,将其从“VBZ”改为了“POS”。
- 添加了形容词、由形容词衍生的名词以及副词的正则表达式。
注意:对于正则表达式匹配,需要在每个表达式字符串前面添加一个小写的“r”,用于指示这是一个原始字符串。这可以避免在正则表达式中出现转义字符的错误。
- User
请再为我生成几个正则表达式,我还想继续提高效果
- Assistant
当然可以。以下是一些额外的正则表达式供您参考:
patterns = [ (r'.*ing$', 'VBG'), # gerunds (r'.*ed$', 'VBD'), # simple past (r'.*es$', 'VBZ'), # 3rd singular present (r'.*ould$', 'MD'), # modals (r'.*\'s$', 'POS'), # possessive nouns (r'.*s$', 'NNS'), # plural nouns (r'^-?[0-9]+(.[0-9]+)?$', 'CD'), # cardinal numbers (r'(The|the|A|a|An|an)$', 'DT'), # determiners (r'.*able$', 'JJ'), # adjectives (r'.*est$', 'JJ'), # superlative adjectives (r'.*ly$', 'RB'), # adverbs (r'.*wise$', 'RB'), # adverbs (r'(He|he|She|she|It|it|I|Me|me|You|you)$', 'PRP'), # pronouns (r'(His|his|Her|her|Its|its|Your|your)$', 'PRP$'), # possessive pronouns (r'.*\'$', '.'), # punctuation (r'.*$', 'NN') # nouns (default) ]在上一个patterns的基础上,我做了一些改进:
- 添加了超级形容词的正则表达式(以
est结尾的词)。
注意:根据不同的文本和场景,以上表达式可能仍然需要微调。如果您希望有更高的准确率,可以进一步根据训练和测试数据设计特定的正则表达式。您还可以尝试其他的NLP技术,例如基于隐马尔可夫模型(HMM)的词性标注器、条件随机场(CRF)等。
- 添加了超级形容词的正则表达式(以
- User
请帮我生成几个正则表达式标记词性的代码
- Assistant
当然可以。以下是一些用于词性标注的正则表达式示例:
patterns = [ (r'.*ing$', 'VBG'), # gerunds (r'.*ed$', 'VBD'), # simple past verbs (r'.*es$', 'VBZ'), # 3rd singular present verbs (r'.*ould$', 'MD'), # modals (r'.*\'s$', 'POS'), # possessive nouns (r'.*s$', 'NNS'), # plural nouns (r'^-?[0-9]+(.[0-9]+)?$', 'CD'), # cardinal numbers (r'(The|the|A|a|An|an)$', 'DT'), # determiners (r'.*able$', 'JJ'), # adjectives (r'.*est$', 'JJS'), # superlative adjectives (r'.*er$', 'JJR'), # comparative adjectives (r'.*ly$', 'RB'), # adverbs (r'.*wise$', 'RB'), # adverbs ending in -wise (r'(He|he|She|she|It|it|I|Me|me|You|you)$', 'PRP'), # pronouns (r'(His|his|Her|her|Its|its|Your|your)$', 'PRP$'), # possessive pronouns (r'.*ing$', 'VBG'), # gerunds and present participles (r'.*ed$', 'VBD'), # past tense verbs (r'.*es$', 'VBZ'), # 3rd person singular present verbs (r'.*ould$', 'MD'), # modal verbs (r'.*\'s$', 'POS'), # possessive nouns (e.g. "John's") ]在前面提供的patterns基础上,我做了以下改进:
- User
请继续丰富正则表达式以匹配更多的词性
- Assistant
根据您的要求,以下是一些带有更丰富正则表达式的examples,用于扩大词性标注范围:
patterns = [ (r'.*ing$', 'VBG'), # gerunds (r'.*ed$', 'VBD'), # simple past verbs (r'.*es$', 'VBZ'), # 3rd singular present verbs (r'.*ould$', 'MD'), # modals (r'.*\'s$', 'POS'), # possessive nouns (r'.*s$', 'NNS'), # plural nouns (r'^-?[0-9]+(.[0-9]+)?$', 'CD'), # cardinal numbers (r'(The|the|A|a|An|an)$', 'DT'), # determiners (r'.*(ful|less|ic)$', 'JJ'), # adjectives (r'.*est$', 'JJS'), # superlative adjectives (r'.*er$', 'JJR'), # comparative adjectives (r'.*ly$', 'RB'), # adverbs (r'.*wise$', 'RB'), # adverbs ending in -wise (r'(He|he|She|she|It|it|I|Me|me|You|you)$', 'PRP'), # pronouns (r'(His|his|Her|her|Its|its|Your|your)$', 'PRP$'), # possessive pronouns (r'^W.*[ph]$', 'WH'), # Wh-questions words (r'^(if|as|where|while)$', 'IN'), # subordinating conjunctions (r'^and|or|but|nor$', 'CC'), # coordinating conjunctions (r'^\!|\?$', '.'), # exclamation or question mark punctuation (r'^[^\!\?\.,]*$', 'NN'), # default => any word that doesn't hit another pattern ] - User
请继续以这种形式再帮我写一些正则表达式以匹配更多的词性
- Assistant
当然可以。以下是一些进一步扩展正则表达式范围的示例:
patterns = [ (r'.*ing$', 'VBG'), # gerunds (r'.*ed$', 'VBD'), # simple past verbs (r'.*es$', 'VBZ'), # 3rd singular present verbs (r'.*ould$', 'MD'), # modals (r'.*\'s$', 'POS'), # possessive nouns (r'.*s$', 'NNS'), # plural nouns (r'^-?[0-9]+(.[0-9]+)?$', 'CD'), # cardinal numbers (r'(The|the|A|a|An|an)$', 'DT'), # determiners (r'.*(ful|less|ic|ive|ous)$', 'JJ'), # adjectives (r'.*est$', 'JJS'), # superlative adjectives (r'.*er$', 'JJR'), # comparative adjectives (r'.*ly$', 'RB'), # adverbs (r'.*wise$', 'RB'), # adverbs ending in -wise (r'(He|he|She|she|It|it|I|Me|me|You|you)$', 'PRP'), # pronouns (r'(His|his|Her|her|Its|its|Your|your)$', 'PRP$'), # possessive pronouns (r'(This|This|These|these|That|that|Those|those)$', 'DT'), # demonstratives (r'^W.*[ph]$', 'WH'), # Wh-questions words (r'^(if|as|where|while)$', 'IN'), # subordinating conjunctions (r'^(And|and|Or|or|But|but|Nor|nor)$', 'CC'), # coordinating conjunctions (r'^(To|to)$', 'TO'), # infinitival "to" (r'^(Not|not)$', 'RB'), # negations (r'^(Is|is|Am|am|Are|are)$', 'VBZ'), # 'to be' verbs in present (r'^(Had|had|Has|has)$', 'VBD'), # 'to have' verbs in past (r'^\!|\?$', '.'), # exclamation or question mark punctuation (r'^[^\!\?\.,]*$', 'NN'), # default => any word that doesn't hit another pattern ]