简体   繁体   中英

What is the syntax for an array to use in Azure Text Analytics key fraze extraction

I am trying to send a POST from my Angular application to Azure Text Analytics API for keyword extraction. I am getting an error telling me that my message body format is wrong. I need to format the message body to make sure Azure can understand it in this way:

{
    "documents": [
        {
            "language": "en",
            "id": "1",
            "text": "We love this trail and make the trip every year. The views are breathtaking and well worth the hike!"
        },
        {
            "language": "en",
            "id": "2",
            "text": "Poorly marked trails! I thought we were goners. Worst hike ever."
        },
        {
            "language": "en",
            "id": "3",
            "text": "Everyone in my family liked the trail but thought it was too challenging for the less athletic among us. Not necessarily recommended for small children."
        },
        {
            "language": "en",
            "id": "4",
            "text": "It was foggy so we missed the spectacular views, but the trail was ok. Worth checking out if you are in the area."
        },                
        {
            "language": "en",
            "id": "5",
            "text": "This is my favorite trail. It has beautiful views and many places to stop and rest"
        }
    ]
}

I have this data model:

export class KeyFraze {
public id: number;
public language: string;
public text: string;

constructor(id: number, language: string, text: string) {
    this.id = id;
    this.language = language;
    this.text = text;
}}

And this is code in the page where I send text to azure (I am sending the extractKeyFraze variable here):

    @Component({
  selector: 'app-bot',
  templateUrl: './bot.component.html',
  styleUrls: ['./bot.component.css']
})
export class BotComponent implements OnInit {
  allowSendMessage = false;
  message = '';
  messages: Message[] = [];
  extractKeywordsFraze: KeyFraze[] = [];

  constructor(private chatbotService: ChatbotService) {}

  ngOnInit() {
  }

  onSend() {
    if (this.message.length > 0) {
      this.messages.push(new Message('user', this.message))
      this.extractKeywordsFraze.push(new KeyFraze(1, 'en', this.message))
      this.message = '';
      this.chatbotService.extractKeywords(this.extractKeywordsFraze)
      .subscribe(
        (response) => console.log(response),
        (error) => console.log(error)
      );
    }

  }

Can someone tell me how to format the data so that is goes in the required format?

Ok, I figured it out!

I set up the message to be send like this:

this.body = {
    "documents": [
      {
        "id": "1",
        "language": "en",
        "text": this.message
      }
    ]
  }

And it works now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM