main content

add part-凯发k8网页登录

add part-of-speech tags to documents

since r2018b

description

use addpartofspeechdetails to add part-of-speech tags to documents.

the function supports english, japanese, german, and korean text.

example

updateddocuments = addpartofspeechdetails(documents) detects parts of speech in documents and updates the token details. the function, by default, retokenizes the text for part-of-speech tagging. for example, the function splits the word "you're" into the tokens "you" and "'re". to get the part-of-speech details from updateddocuments, use .

updateddocuments = addpartofspeechdetails(documents,name,value) specifies additional options using one or more name-value pair arguments.

tip

use addpartofspeechdetails before using the lower, upper, erasepunctuation, normalizewords, removewords, and removestopwords functions as addpartofspeechdetails uses information that is removed by these functions.

examples

load the example data. the file sonnetspreprocessed.txt contains preprocessed versions of shakespeare's sonnets. the file contains one sonnet per line, with words separated by a space. extract the text from sonnetspreprocessed.txt, split the text into documents at newline characters, and then tokenize the documents.

filename = "sonnetspreprocessed.txt";
str = extractfiletext(filename);
textdata = split(str,newline);
documents = tokenizeddocument(textdata);

view the token details of the first few tokens.

tdetails = tokendetails(documents);
head(tdetails)
       token       documentnumber    linenumber     type      language
    ___________    ______________    __________    _______    ________
    "fairest"            1               1         letters       en   
    "creatures"          1               1         letters       en   
    "desire"             1               1         letters       en   
    "increase"           1               1         letters       en   
    "thereby"            1               1         letters       en   
    "beautys"            1               1         letters       en   
    "rose"               1               1         letters       en   
    "might"              1               1         letters       en   

add part-of-speech details to the documents using the addpartofspeechdetails function. this function first adds sentence information to the documents, and then adds the part-of-speech tags to the table returned by tokendetails. view the updated token details of the first few tokens.

documents = addpartofspeechdetails(documents);
tdetails = tokendetails(documents);
head(tdetails)
       token       documentnumber    sentencenumber    linenumber     type      language     partofspeech 
    ___________    ______________    ______________    __________    _______    ________    ______________
    "fairest"            1                 1               1         letters       en       adjective     
    "creatures"          1                 1               1         letters       en       noun          
    "desire"             1                 1               1         letters       en       noun          
    "increase"           1                 1               1         letters       en       noun          
    "thereby"            1                 1               1         letters       en       adverb        
    "beautys"            1                 1               1         letters       en       noun          
    "rose"               1                 1               1         letters       en       noun          
    "might"              1                 1               1         letters       en       auxiliary-verb

tokenize japanese text using tokenizeddocument.

str = [
    "恋に悩み、苦しむ。"
    "恋の悩みで 苦しむ。"
    "空に星が輝き、瞬いている。"
    "空の星が輝きを増している。"
    "駅までは遠くて、歩けない。"
    "遠くの駅まで歩けない。"
    "すもももももももものうち。"];
documents = tokenizeddocument(str);

for japanese text, you can get the part-of-speech details using tokendetails. for english text, you must first use addpartofspeechdetails.

tdetails = tokendetails(documents);
head(tdetails)
     token     documentnumber    linenumber       type        language    partofspeech     lemma       entity  
    _______    ______________    __________    ___________    ________    ____________    _______    __________
    "恋"             1               1         letters           ja       noun            "恋"       non-entity
    "に"             1               1         letters           ja       adposition      "に"       non-entity
    "悩み"           1               1         letters           ja       verb            "悩む"      non-entity
    "、"             1               1         punctuation       ja       punctuation     "、"       non-entity
    "苦しむ"          1               1         letters           ja       verb            "苦しむ"    non-entity
    "。"             1               1         punctuation       ja       punctuation     "。"       non-entity
    "恋"             2               1         letters           ja       noun            "恋"       non-entity
    "の"             2               1         letters           ja       adposition      "の"       non-entity

tokenize german text using tokenizeddocument.

str = [
    "guten morgen. wie geht es dir?"
    "heute wird ein guter tag."];
documents = tokenizeddocument(str)
documents = 
  2x1 tokenizeddocument:
    8 tokens: guten morgen . wie geht es dir ?
    6 tokens: heute wird ein guter tag .

to get the part of speech details for german text, first use addpartofspeechdetails.

documents = addpartofspeechdetails(documents);

to view the part of speech details, use the tokendetails function.

tdetails = tokendetails(documents);
head(tdetails)
     token      documentnumber    sentencenumber    linenumber       type        language    partofspeech
    ________    ______________    ______________    __________    ___________    ________    ____________
    "guten"           1                 1               1         letters           de       adjective   
    "morgen"          1                 1               1         letters           de       noun        
    "."               1                 1               1         punctuation       de       punctuation 
    "wie"             1                 2               1         letters           de       adverb      
    "geht"            1                 2               1         letters           de       verb        
    "es"              1                 2               1         letters           de       pronoun     
    "dir"             1                 2               1         letters           de       pronoun     
    "?"               1                 2               1         punctuation       de       punctuation 

input arguments

input documents, specified as a tokenizeddocument array.

name-value arguments

specify optional pairs of arguments as name1=value1,...,namen=valuen, where name is the argument name and value is the corresponding value. name-value arguments must appear after other arguments, but the order of the pairs does not matter.

before r2021a, use commas to separate each name and value, and enclose name in quotes.

example: 'discardknownvalues',true specifies to discard previously computed details and recompute them.

method to retokenize documents, specified as one of the following:

  • 'part-of-speech' – transform the tokens for part-of-speech tagging. the function performs these tasks:

    • split compound words. for example, split the compound word "wanna" into the tokens "want" and "to". this includes compound words containing apostrophes. for example, the function splits the word "don't" into the tokens "do" and "n't".

    • merge periods that do not end sentences with preceding tokens. for example, merge the tokens "mr" and "." into the token "mr.".

    • for german text, merge abbreviations that span multiple tokens. for example, merge the tokens "z", ".", "b", and "." into the single token "z. b.".

    • merge runs of periods into ellipses. for example, merge three instances of "." into the single token "...".

  • 'none' – do not retokenize the documents.

list of abbreviations for sentence detection, specified as a string array, character vector, cell array of character vectors, or a table.

if the input documents do not contain sentence details, then the function first runs the function and specifies the abbreviation list given by 'abbreviations'. to specify more options for sentence detection (for example, sentence starters) use the addsentencedetails function before using addpartofspeechdetails details.

if abbreviations is a string array, character vector, or cell array of character vectors, then the function treats these as regular abbreviations. if the next word is a capitalized sentence starter, then the function breaks at the trailing period. the function ignores any differences in the letter case of the abbreviations. specify the sentence starters using the name-value pair.

to specify different behaviors when splitting sentences at abbreviations, specify abbreviations as a table. the table must have variables named abbreviation and usage, where abbreviation contains the abbreviations, and usage contains the type of each abbreviation. the following table describes the possible values of usage, and the behavior of the function when passed abbreviations of these types.

usagebehaviorexample abbreviationexample textdetected sentences
regularif the next word is a capitalized sentence starter, then break at the trailing period. otherwise, do not break at the trailing period."appt.""book an appt. we'll meet then."

"book an appt."

"we'll meet then."

"book an appt. today.""book an appt. today."
innerdo not break after trailing period."dr.""dr. smith.""dr. smith."
referenceif the next token is not a number, then break at a trailing period. if the next token is a number, then do not break at the trailing period."fig.""see fig. 3.""see fig. 3."
"try a fig. they are nice."

"try a fig."

"they are nice."

unitif the previous word is a number and the following word is a capitalized sentence starter, then break at a trailing period."in.""the height is 30 in. the width is 10 in."

"the height is 30 in."

"the width is 10 in."

if the previous word is a number and the following word is not capitalized, then do not break at a trailing period."the item is 10 in. wide.""the item is 10 in. wide."
if the previous word is not a number, then break at a trailing period."come in. sit down."

"come in."

"sit down."

the default value is the output of the function. for japanese and korean text, abbreviations do not usually impact sentence detection.

tip

by default, the function treats single letter abbreviations, such as "v.", or tokens with mixed single letters and periods, such as "u.s.a." as regular abbreviations. you do not need to include these abbreviations in abbreviations.

data types: char | string | table | cell

option to discard previously computed details and recompute them, specified as true or false.

data types: logical

output arguments

updated documents, returned as a tokenizeddocument array. to get the token details from updateddocuments, use .

more about

part-of-speech tags

the addpartofspeechdetails function adds part-of-speech tags to the table returned by the function. the function tags each token with a categorical tag with one of the following class names:

  • adjective — adjective

  • adposition — adposition

  • adverb — adverb

  • auxiliary-verb — auxiliary verb

  • coord-conjunction — coordinating conjunction

  • determiner — determiner

  • interjection — interjection

  • noun — noun

  • numeral — numeral

  • particle — particle

  • pronoun — pronoun

  • proper-noun — proper noun

  • punctuation — punctuation

  • subord-conjunction — subordinating conjucntion

  • symbol — symbol

  • verb — verb

  • other — other

algorithms

if the input documents do not contain sentence details, then the function first runs .

version history

introduced in r2018b

网站地图